switch eager vs lazy instantiation to a criterion bench

This commit is contained in:
Pat Hickey
2021-06-04 12:24:44 -07:00
parent 0a96b6b60a
commit 2a4c51b77d
3 changed files with 51 additions and 28 deletions

View File

@@ -120,3 +120,7 @@ debug = false # FIXME(#1813)
[[bench]] [[bench]]
name = "instantiation" name = "instantiation"
harness = false harness = false
[[bench]]
name = "thread_eager_init"
harness = false

View File

@@ -1,11 +1,47 @@
use anyhow::Result; use criterion::{criterion_group, criterion_main, Criterion};
use std::thread; use std::thread;
use std::time::{Duration, Instant}; use std::time::{Duration, Instant};
use wasmtime::*; use wasmtime::*;
#[test] fn measure_execution_time(c: &mut Criterion) {
fn measure_execution_time() -> Result<()> { c.bench_function("lazy initialization at call", move |b| {
let iterations = 1000; let (engine, module) = test_engine();
b.iter_custom(move |iters| {
(0..iters)
.into_iter()
.map(|_| lazy_thread_instantiate(engine.clone(), module.clone()))
.sum()
})
});
c.bench_function("eager initialization", move |b| {
let (engine, module) = test_engine();
b.iter_custom(move |iters| {
(0..iters)
.into_iter()
.map(|_| {
let (init, _call) = eager_thread_instantiate(engine.clone(), module.clone());
init
})
.sum()
})
});
c.bench_function("call after eager initialization", move |b| {
let (engine, module) = test_engine();
b.iter_custom(move |iters| {
(0..iters)
.into_iter()
.map(|_| {
let (_init, call) = eager_thread_instantiate(engine.clone(), module.clone());
call
})
.sum()
})
});
}
fn test_engine() -> (Engine, Module) {
let pool_count = 1000;
let mut config = Config::new(); let mut config = Config::new();
config.allocation_strategy(InstanceAllocationStrategy::Pooling { config.allocation_strategy(InstanceAllocationStrategy::Pooling {
@@ -15,33 +51,14 @@ fn measure_execution_time() -> Result<()> {
..Default::default() ..Default::default()
}, },
instance_limits: InstanceLimits { instance_limits: InstanceLimits {
count: iterations * 2, count: pool_count,
memory_reservation_size: 1, memory_reservation_size: 1,
}, },
}); });
let engine = Engine::new(&config)?; let engine = Engine::new(&config).unwrap();
let module = Module::new(&engine, r#"(module (memory 1) (func (export "f")))"#)?; let module = Module::new(&engine, r#"(module (memory 1) (func (export "f")))"#).unwrap();
(engine, module)
let lazy_call_time: Duration = (0..iterations)
.into_iter()
.map(|_| lazy_thread_instantiate(engine.clone(), module.clone()))
.sum();
let (eager_init_total, eager_call_total): (Duration, Duration) = (0..iterations)
.into_iter()
.map(|_| eager_thread_instantiate(engine.clone(), module.clone()))
.fold(
(Duration::default(), Duration::default()),
|(s1, s2), (d1, d2)| (s1 + d1, s2 + d2),
);
println!(
"lazy call: {:?}, eager init: {:?}, eager call: {:?}",
lazy_call_time, eager_init_total, eager_call_total
);
Ok(())
} }
fn lazy_thread_instantiate(engine: Engine, module: Module) -> Duration { fn lazy_thread_instantiate(engine: Engine, module: Module) -> Duration {
@@ -77,3 +94,6 @@ fn eager_thread_instantiate(engine: Engine, module: Module) -> (Duration, Durati
.join() .join()
.expect("thread joins") .expect("thread joins")
} }
criterion_group!(benches, measure_execution_time);
criterion_main!(benches);

View File

@@ -24,7 +24,6 @@ mod pooling_allocator;
mod stack_overflow; mod stack_overflow;
mod store; mod store;
mod table; mod table;
mod thread_eager_init;
mod traps; mod traps;
mod wast; mod wast;