Add bench for match_url and clean up some gross benchmark boilerplate

This commit is contained in:
Thom Chiovoloni 2019-02-01 13:04:09 -08:00 коммит произвёл Thom
Родитель d1b0d7b05f
Коммит 2b6bc29821
3 изменённых файлов: 100 добавлений и 80 удалений

Просмотреть файл

@ -55,5 +55,5 @@ name = "match_impl"
harness = false
[[bench]]
name = "search_frecent"
name = "search"
harness = false

Просмотреть файл

@ -0,0 +1,99 @@
use criterion::{criterion_group, criterion_main, Criterion};
use places::api::matcher::{match_url, search_frecent, SearchParams};
use places::PlacesDb;
use std::rc::Rc;
use tempdir::TempDir;
#[derive(Clone, Debug, serde_derive::Deserialize)]
struct DummyHistoryEntry {
url: String,
title: String,
}
fn init_db(db: &mut PlacesDb) -> places::Result<()> {
let dummy_data = include_str!("../fixtures/dummy_urls.json");
let entries: Vec<DummyHistoryEntry> = serde_json::from_str(dummy_data)?;
let tx = db.db.transaction()?;
let day_ms = 24 * 60 * 60 * 1000;
let now: places::Timestamp = std::time::SystemTime::now().into();
for entry in entries {
let url = url::Url::parse(&entry.url).unwrap();
for i in 0..20 {
let obs = places::VisitObservation::new(url.clone())
.with_title(entry.title.clone())
.with_is_remote(i < 10)
.with_visit_type(places::VisitTransition::Link)
.with_at(places::Timestamp(now.0 - day_ms * (1 + i)));
places::storage::history::apply_observation_direct(&tx, obs)?;
}
}
tx.commit()?;
Ok(())
}
pub struct TestDb {
// Needs to be here so that the dir isn't deleted.
_dir: TempDir,
pub db: PlacesDb,
}
impl TestDb {
pub fn new() -> Rc<Self> {
let dir = TempDir::new("placesbench").unwrap();
let file = dir.path().join("places.sqlite");
let mut db = PlacesDb::open(&file, None).unwrap();
println!("Populating test database...");
init_db(&mut db).unwrap();
println!("Done populating test db");
Rc::new(Self { _dir: dir, db })
}
}
macro_rules! db_bench {
($c:expr, $name:literal, |$db:ident = $test_db_name:ident| $expr:expr) => {{
let $test_db_name = $test_db_name.clone();
$c.bench_function($name, move |b| {
let $db = &$test_db_name.db;
b.iter(|| $expr)
});
}};
}
fn bench_search_frecent(c: &mut Criterion) {
let test_db = TestDb::new();
db_bench!(c, "search_frecent string", |db = test_db| {
search_frecent(&db, SearchParams {
search_string: "mozilla".into(),
limit: 10,
}).unwrap()
});
db_bench!(c, "search_frecent origin", |db = test_db| {
search_frecent(&db, SearchParams {
search_string: "blog.mozilla.org".into(),
limit: 10,
}).unwrap()
});
db_bench!(c, "search_frecent url", |db = test_db| {
search_frecent(&db, SearchParams {
search_string: "https://hg.mozilla.org/mozilla-central".into(),
limit: 10,
}).unwrap()
});
}
fn bench_match_url(c: &mut Criterion) {
let test_db = TestDb::new();
db_bench!(c, "match_url string", |db = test_db| {
match_url(&db, "mozilla").unwrap()
});
db_bench!(c, "match_url origin", |db = test_db| {
match_url(&db, "blog.mozilla.org").unwrap()
});
db_bench!(c, "match_url url", |db = test_db| {
match_url(&db, "https://hg.mozilla.org/mozilla-central").unwrap()
});
}
criterion_group!(benches, bench_search_frecent, bench_match_url);
criterion_main!(benches);

Просмотреть файл

@ -1,79 +0,0 @@
use criterion::{criterion_group, criterion_main, Criterion};
use places::api::matcher::{search_frecent, SearchParams};
use places::PlacesDb;
use std::rc::Rc;
use tempdir::TempDir;
#[derive(Clone, Debug, serde_derive::Deserialize)]
struct DummyHistoryEntry {
url: String,
title: String,
}
fn init_db(db: &mut PlacesDb) -> places::Result<()> {
let dummy_data = include_str!("../fixtures/dummy_urls.json");
let entries: Vec<DummyHistoryEntry> = serde_json::from_str(dummy_data)?;
let tx = db.db.transaction()?;
let day_ms = 24 * 60 * 60 * 1000;
let now: places::Timestamp = std::time::SystemTime::now().into();
for entry in entries {
let url = url::Url::parse(&entry.url).unwrap();
for i in 0..20 {
let obs = places::VisitObservation::new(url.clone())
.with_title(entry.title.clone())
.with_is_remote(i < 10)
.with_visit_type(places::VisitTransition::Link)
.with_at(places::Timestamp(now.0 - day_ms * (1 + i)));
places::storage::history::apply_observation_direct(&tx, obs)?;
}
}
tx.commit()?;
Ok(())
}
fn bench_search_frecent(c: &mut Criterion) {
let dbdir = Rc::new(TempDir::new("placesbench").unwrap());
let dbfile = dbdir.path().join("places.sqlite");
let mut db = PlacesDb::open(&dbfile, None).unwrap();
init_db(&mut db).unwrap();
let db = Rc::new(db);
{
let db = db.clone();
let dir = dbdir.clone();
c.bench_function("search_frecent string", move |b| {
let _dir = dir.clone(); // ensure it stays alive...
let params = SearchParams {
search_string: "mozilla".into(),
limit: 10,
};
b.iter(|| search_frecent(&db, params.clone()).unwrap())
});
}
{
let db = db.clone();
let dir = dbdir.clone();
c.bench_function("search_frecent origin", move |b| {
let _dir = dir.clone(); // ensure it stays alive...
let params = SearchParams {
search_string: "blog.mozilla.org".into(),
limit: 10,
};
b.iter(|| search_frecent(&db, params.clone()).unwrap())
});
}
{
let db = db.clone();
let dir = dbdir.clone();
c.bench_function("search_frecent url", move |b| {
let _dir = dir.clone(); // ensure it stays alive...
let params = SearchParams {
search_string: "https://hg.mozilla.org/mozilla-central".into(),
limit: 10,
};
b.iter(|| search_frecent(&db, params.clone()).unwrap())
});
}
}
criterion_group!(benches, bench_search_frecent);
criterion_main!(benches);