-
Notifications
You must be signed in to change notification settings - Fork 110
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix iteration problem with criterion, pre-creation of database outsid…
…e bench for hyperfine
- Loading branch information
Showing
6 changed files
with
301 additions
and
186 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,81 +1,110 @@ | ||
//! Benchmarks for evaluating how we fare against sqlite | ||
|
||
use criterion::measurement::WallTime; | ||
use criterion::{ | ||
criterion_group, criterion_main, BatchSize, BenchmarkGroup, BenchmarkId, Criterion, SamplingMode, Throughput, | ||
}; | ||
use criterion::{criterion_group, criterion_main, BatchSize, BenchmarkGroup, BenchmarkId, Criterion, Throughput}; | ||
use spacetimedb_bench::prelude::*; | ||
use std::time::Duration; | ||
|
||
// IMPORTANT!: It needs this option to run the setup once per `.iter`! | ||
const SIZE: BatchSize = BatchSize::PerIteration; | ||
|
||
fn build_group<'a>(c: &'a mut Criterion, named: &str, run: Runs) -> BenchmarkGroup<'a, WallTime> { | ||
let mut group = c.benchmark_group(named); | ||
// We need to restrict the amount of iterations and set the benchmark for "large" operations. | ||
|
||
group.throughput(Throughput::Elements(run as u64)); | ||
group.sample_size(DB_POOL as usize); | ||
group.sampling_mode(SamplingMode::Flat); | ||
group.measurement_time(Duration::from_secs(5)); | ||
|
||
group | ||
} | ||
|
||
fn bench_insert_tx_per_row(c: &mut Criterion) { | ||
let run = Runs::Tiny; | ||
let mut group = build_group(c, "insert_row", run); | ||
|
||
// Factor out the db creation because is IO that generate noise | ||
group.bench_function(BenchmarkId::new(SQLITE, 1), |b| { | ||
let mut db_instance = 0; | ||
b.iter_batched( | ||
|| { | ||
let path = sqlite::create_db(db_instance).unwrap(); | ||
db_instance += 1; | ||
path | ||
}, | ||
|data| { | ||
let mut conn = sqlite::open_conn(&data).unwrap(); | ||
|| sqlite::create_db(0).unwrap(), | ||
|path| { | ||
let mut conn = sqlite::open_conn(&path).unwrap(); | ||
sqlite::insert_tx_per_row(&mut conn, run).unwrap(); | ||
}, | ||
BatchSize::NumBatches(DB_POOL as u64), | ||
SIZE, | ||
); | ||
}); | ||
group.bench_function(BenchmarkId::new(SPACETIME, 1), |b| { | ||
b.iter_batched( | ||
|| spacetime::create_db(0).unwrap(), | ||
|path| { | ||
let (conn, _, table_id) = spacetime::open_conn(&path).unwrap(); | ||
spacetime::insert_tx_per_row(&conn, table_id, run).unwrap(); | ||
}, | ||
SIZE, | ||
); | ||
}); | ||
|
||
group.finish(); | ||
} | ||
|
||
fn bench_insert_tx(c: &mut Criterion) { | ||
let run = Runs::Small; | ||
let mut group = build_group(c, "insert_bulk_rows", run); | ||
|
||
// Factor out the db creation because is IO that generate noise | ||
group.bench_function(BenchmarkId::new(SQLITE, 2), |b| { | ||
b.iter_batched( | ||
|| sqlite::create_db(0).unwrap(), | ||
|path| { | ||
let mut conn = sqlite::open_conn(&path).unwrap(); | ||
sqlite::insert_tx(&mut conn, run).unwrap(); | ||
}, | ||
SIZE, | ||
); | ||
}); | ||
group.bench_function(BenchmarkId::new(SPACETIME, 2), |b| { | ||
b.iter_batched( | ||
|| spacetime::create_db(0).unwrap(), | ||
|path| { | ||
let (conn, _, table_id) = spacetime::open_conn(&path).unwrap(); | ||
spacetime::insert_tx(&conn, table_id, run).unwrap(); | ||
}, | ||
SIZE, | ||
); | ||
}); | ||
|
||
group.finish(); | ||
} | ||
|
||
fn bench_select_no_index(c: &mut Criterion) { | ||
let run = Runs::Tiny; | ||
let mut group = build_group(c, "select_index_no", run); | ||
|
||
// Factor out the db creation because is IO that generate noise | ||
group.bench_function(BenchmarkId::new(SQLITE, 3), |b| { | ||
b.iter_batched( | ||
|| sqlite::create_db(0).unwrap(), | ||
|path| { | ||
let mut conn = sqlite::open_conn(&path).unwrap(); | ||
sqlite::select_no_index(&mut conn, run).unwrap(); | ||
}, | ||
SIZE, | ||
); | ||
}); | ||
group.bench_function(BenchmarkId::new(SPACETIME, 3), |b| { | ||
b.iter_batched( | ||
|| spacetime::create_db(0).unwrap(), | ||
|path| { | ||
let (conn, _, table_id) = spacetime::open_conn(&path).unwrap(); | ||
spacetime::select_no_index(&conn, table_id, run).unwrap(); | ||
}, | ||
SIZE, | ||
); | ||
}); | ||
// group.bench_function(BenchmarkId::new(SPACETIME, 1), |b| { | ||
// let mut pool = Pool::new(false).unwrap(); | ||
// b.iter_with_setup(|| spacetime::insert_tx_per_row(&mut pool, run).unwrap()) | ||
// }); | ||
|
||
group.finish(); | ||
} | ||
// | ||
// fn bench_insert_tx(c: &mut Criterion) { | ||
// let run = Runs::Small; | ||
// let mut group = build_group(c, "insert_bulk_rows", run); | ||
// | ||
// group.bench_function(BenchmarkId::new(SQLITE, 2), |b| { | ||
// let mut pool = Pool::new(true).unwrap(); | ||
// b.iter(|| sqlite::insert_tx(&mut pool, run)) | ||
// }); | ||
// group.bench_function(BenchmarkId::new(SPACETIME, 2), |b| { | ||
// let mut pool = Pool::new(true).unwrap(); | ||
// b.iter(|| spacetime::insert_tx(&mut pool, run)) | ||
// }); | ||
// | ||
// group.finish(); | ||
// } | ||
// | ||
// fn bench_select_no_index(c: &mut Criterion) { | ||
// let run = Runs::Tiny; | ||
// let mut group = build_group(c, "select_index_no", run); | ||
// | ||
// group.bench_function(BenchmarkId::new(SQLITE, 3), |b| { | ||
// let mut pool = Pool::new(true).unwrap(); | ||
// b.iter(|| sqlite::select_no_index(&mut pool, run).unwrap()) | ||
// }); | ||
// group.bench_function(BenchmarkId::new(SPACETIME, 3), |b| { | ||
// let mut pool = Pool::new(true).unwrap(); | ||
// b.iter(|| spacetime::select_no_index(&mut pool, run).unwrap()) | ||
// }); | ||
// | ||
// group.finish(); | ||
// } | ||
|
||
// Note: Reflex this same benchmarks in `main.rs` | ||
//, bench_insert_tx, bench_select_no_index | ||
criterion_group!(benches, bench_insert_tx_per_row); | ||
// Note: Mirror this benchmarks in `main.rs` | ||
criterion_group!(benches, bench_insert_tx_per_row, bench_insert_tx, bench_select_no_index); | ||
criterion_main!(benches); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.