Skip to content

Commit

Permalink
fixing rustfmt
Browse files Browse the repository at this point in the history
  • Loading branch information
Nikolai Schimke authored and Nikolai Schimke committed Feb 1, 2024
1 parent 39ea169 commit 358bf1d
Show file tree
Hide file tree
Showing 5 changed files with 130 additions and 122 deletions.
129 changes: 71 additions & 58 deletions benches/browser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,102 +5,115 @@ use headless_chrome::Browser;

fn create_connection() {
let browser = Browser::new({
headless_chrome::LaunchOptions { headless: false,
sandbox: false,
ignore_certificate_errors: true,
..Default::default() }
headless_chrome::LaunchOptions {
headless: false,
sandbox: false,
ignore_certificate_errors: true,
..Default::default()
}
});

browser.unwrap()
.get_tabs()
.lock()
.unwrap()
.iter()
.for_each(|e| {
e.close(false).unwrap();
})
browser
.unwrap()
.get_tabs()
.lock()
.unwrap()
.iter()
.for_each(|e| {
e.close(false).unwrap();
})
}

fn create_connection_headless() {
let browser = Browser::new({
headless_chrome::LaunchOptions { headless: true,
sandbox: false,
ignore_certificate_errors: true,
..Default::default() }
headless_chrome::LaunchOptions {
headless: true,
sandbox: false,
ignore_certificate_errors: true,
..Default::default()
}
});

browser.unwrap()
.get_tabs()
.lock()
.unwrap()
.iter()
.for_each(|e| {
e.close(false).unwrap();
})
browser
.unwrap()
.get_tabs()
.lock()
.unwrap()
.iter()
.for_each(|e| {
e.close(false).unwrap();
})
}

fn create_connection_with_sandbox() {
let browser = Browser::new({
headless_chrome::LaunchOptions { headless: false,
sandbox: true,
ignore_certificate_errors: true,
..Default::default() }
headless_chrome::LaunchOptions {
headless: false,
sandbox: true,
ignore_certificate_errors: true,
..Default::default()
}
});

browser.unwrap()
.get_tabs()
.lock()
.unwrap()
.iter()
.for_each(|e| {
e.close(false).unwrap();
})
browser
.unwrap()
.get_tabs()
.lock()
.unwrap()
.iter()
.for_each(|e| {
e.close(false).unwrap();
})
}

fn create_connection_with_extensions() {
let browser = Browser::new({
headless_chrome::LaunchOptions { headless: false,
sandbox: true,
ignore_certificate_errors: true,
extensions: vec![OsStr::new("./chrome-ext/adblock")],
..Default::default() }
headless_chrome::LaunchOptions {
headless: false,
sandbox: true,
ignore_certificate_errors: true,
extensions: vec![OsStr::new("./chrome-ext/adblock")],
..Default::default()
}
});

browser.unwrap()
.get_tabs()
.lock()
.unwrap()
.iter()
.for_each(|e| {
e.close(false).unwrap();
})
browser
.unwrap()
.get_tabs()
.lock()
.unwrap()
.iter()
.for_each(|e| {
e.close(false).unwrap();
})
}

fn criterion_benchmark(c: &mut Criterion) {
let mut group = c.benchmark_group("browser_open");
// Configure Criterion.rs to detect smaller differences and increase sample size to improve
// precision and counteract the resulting noise.
group.significance_level(0.1)
.sample_size(10)
.measurement_time(std::time::Duration::from_secs(20));
group
.significance_level(0.1)
.sample_size(10)
.measurement_time(std::time::Duration::from_secs(20));

// 1.54 seconds
group.bench_function("Create new browser basic", |b| b.iter(create_connection));

// 0.943 seconds
group.bench_function("Create new browser headless", |b| {
b.iter(create_connection_headless)
});
b.iter(create_connection_headless)
});

// 1.48 seconds
group.bench_function("Create new browser sandbox", |b| {
b.iter(create_connection_with_sandbox)
});
b.iter(create_connection_with_sandbox)
});

// 1.69 seconds
group.bench_function("Create new browser adblock ext", |b| {
b.iter(create_connection_with_extensions)
});
b.iter(create_connection_with_extensions)
});

group.finish();
}
Expand Down
32 changes: 18 additions & 14 deletions benches/db.rs
Original file line number Diff line number Diff line change
@@ -1,30 +1,34 @@
use criterion::{criterion_group, criterion_main, Criterion};
use scraper::db;

fn create_connection() { db::helpers::establish_connection().unwrap(); }
fn create_connection() {
db::helpers::establish_connection().unwrap();
}

fn criterion_benchmark(c: &mut Criterion) {
// 43.6 nano seconds
c.bench_function("Create new db connection", |b| b.iter(create_connection));

// 228.3 nano seconds
c.bench_function("Create a new record", |b| {
let mut conn = db::helpers::establish_connection().unwrap();
let new_stream = db::models::StreamNew { away: "Away",
home: "Home",
league: "League",
country: "Country",
start_time: "Start Time",
url: "Url",
stream_link: "https://www.test.com" };
b.iter(|| db::helpers::create_stream(&mut conn, &new_stream))
});
let mut conn = db::helpers::establish_connection().unwrap();
let new_stream = db::models::StreamNew {
away: "Away",
home: "Home",
league: "League",
country: "Country",
start_time: "Start Time",
url: "Url",
stream_link: "https://www.test.com",
};
b.iter(|| db::helpers::create_stream(&mut conn, &new_stream))
});

// 356 nano seconds
c.bench_function("Get all streams", |b| {
let mut conn = db::helpers::establish_connection().unwrap();
b.iter(|| db::helpers::get_streams(&mut conn))
});
let mut conn = db::helpers::establish_connection().unwrap();
b.iter(|| db::helpers::get_streams(&mut conn))
});
}

criterion_group!(benches, criterion_benchmark);
Expand Down
10 changes: 1 addition & 9 deletions rustfmt.toml
Original file line number Diff line number Diff line change
@@ -1,17 +1,9 @@
array_width = 50
blank_lines_upper_bound = 2
chain_width = 40
combine_control_expr = false
edition = "2021"
fn_single_line = true
force_multiline_blocks = true
format_code_in_doc_comments = true
format_generated_files = false
format_strings = true
imports_layout = "HorizontalVertical"
indent_style = "Visual"
match_arm_leading_pipes = "Always"
imports_granularity = "Crate"
group_imports = "StdExternalCrate"
struct_lit_single_line = false
use_field_init_shorthand = true
group_imports = "StdExternalCrate"
12 changes: 5 additions & 7 deletions src/bin/sportshub.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,9 @@ use scraper::{db, scrape_utils, web_server_utils};
pub const MIGRATIONS: EmbeddedMigrations = embed_migrations!("migrations");

fn run_migrations(connection: &mut impl MigrationHarness<Sqlite>) -> Result<(), Error> {
connection.revert_all_migrations(MIGRATIONS)
.unwrap();
connection.revert_all_migrations(MIGRATIONS).unwrap();
println!("Reverted all migrations");
connection.run_pending_migrations(MIGRATIONS)
.unwrap();
connection.run_pending_migrations(MIGRATIONS).unwrap();

Ok(())
}
Expand Down Expand Up @@ -45,14 +43,14 @@ async fn main() {
let cli = Cli::parse();

match cli.command {
| Some(Commands::Parse { tabs }) => {
Some(Commands::Parse { tabs }) => {
run_migrations(&mut conn).unwrap();
scrape_utils::start_scraping(tabs).unwrap();
}
| Some(Commands::Server { port }) => {
Some(Commands::Server { port }) => {
web_server_utils::run(port).await;
}
| None => {
None => {
println!("use sportshub -h for help");
}
}
Expand Down
69 changes: 35 additions & 34 deletions src/db/models.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ pub struct StreamNew<'a> {

impl Serialize for Stream {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where S: serde::ser::Serializer
where
S: serde::ser::Serializer,
{
let split_streams: Vec<&str> = self.stream_link.split(',').collect();
let mut stream = serializer.serialize_struct("Stream", 8)?;
Expand All @@ -50,55 +51,55 @@ mod tests {

#[test]
fn test_serialise_streamlink() {
let stream = Stream { id: Some(1),
home: "home".to_string(),
away: "away".to_string(),
start_time: "start_time".to_string(),
league: "league".to_string(),
country: "country".to_string(),
url: "url".to_string(),
stream_link: "stream_link".to_string() };
let stream = Stream {
id: Some(1),
home: "home".to_string(),
away: "away".to_string(),
start_time: "start_time".to_string(),
league: "league".to_string(),
country: "country".to_string(),
url: "url".to_string(),
stream_link: "stream_link".to_string(),
};

let serialised = serde_json::to_string(&stream).unwrap();
assert_eq!(serialised,
"{\"id\":1,\"home\":\"home\",\"away\":\"away\",\"start_time\":\"start_time\",\\
"league\":\"league\",\"country\":\"country\",\"url\":\"url\",\"stream_link\":\
[\"stream_link\"]}");
"{\"id\":1,\"home\":\"home\",\"away\":\"away\",\"start_time\":\"start_time\",\"league\":\"league\",\"country\":\"country\",\"url\":\"url\",\"stream_link\":[\"stream_link\"]}");
}

#[test]
fn test_serialise_streamlink_multiple() {
let stream = Stream { id: Some(1),
home: "home".to_string(),
away: "away".to_string(),
start_time: "start_time".to_string(),
league: "league".to_string(),
country: "country".to_string(),
url: "url".to_string(),
stream_link: "stream_link,stream_link2".to_string() };
let stream = Stream {
id: Some(1),
home: "home".to_string(),
away: "away".to_string(),
start_time: "start_time".to_string(),
league: "league".to_string(),
country: "country".to_string(),
url: "url".to_string(),
stream_link: "stream_link,stream_link2".to_string(),
};

let serialised = serde_json::to_string(&stream).unwrap();
assert_eq!(serialised,
"{\"id\":1,\"home\":\"home\",\"away\":\"away\",\"start_time\":\"start_time\",\\
"league\":\"league\",\"country\":\"country\",\"url\":\"url\",\"stream_link\":\
[\"stream_link\",\"stream_link2\"]}");
"{\"id\":1,\"home\":\"home\",\"away\":\"away\",\"start_time\":\"start_time\",\"league\":\"league\",\"country\":\"country\",\"url\":\"url\",\"stream_link\":[\"stream_link\",\"stream_link2\"]}");
}

#[test]
fn test_serialise_streamlink_empty() {
let stream = Stream { id: Some(1),
home: "home".to_string(),
away: "away".to_string(),
start_time: "start_time".to_string(),
league: "league".to_string(),
country: "country".to_string(),
url: "url".to_string(),
stream_link: "".to_string() };
let stream = Stream {
id: Some(1),
home: "home".to_string(),
away: "away".to_string(),
start_time: "start_time".to_string(),
league: "league".to_string(),
country: "country".to_string(),
url: "url".to_string(),
stream_link: "".to_string(),
};

let serialised = serde_json::to_string(&stream).unwrap();
assert_eq!(serialised,
"{\"id\":1,\"home\":\"home\",\"away\":\"away\",\"start_time\":\"start_time\",\\
"league\":\"league\",\"country\":\"country\",\"url\":\"url\",\"stream_link\":\
[\"\"]}");
"{\"id\":1,\"home\":\"home\",\"away\":\"away\",\"start_time\":\"start_time\",\"league\":\"league\",\"country\":\"country\",\"url\":\"url\",\"stream_link\":[\"\"]}");
}
}

0 comments on commit 358bf1d

Please sign in to comment.