diff --git a/tests/all/main.rs b/tests/all/main.rs index 37bb68d7a6..a1f3314d64 100644 --- a/tests/all/main.rs +++ b/tests/all/main.rs @@ -24,6 +24,7 @@ mod pooling_allocator; mod stack_overflow; mod store; mod table; +mod thread_eager_init; mod traps; mod wast; diff --git a/tests/all/thread_eager_init.rs b/tests/all/thread_eager_init.rs new file mode 100644 index 0000000000..16f23fb4fe --- /dev/null +++ b/tests/all/thread_eager_init.rs @@ -0,0 +1,72 @@ +use anyhow::Result; +use std::thread; +use std::time::{Duration, Instant}; +use wasmtime::*; + +#[test] +fn measure_execution_time() -> Result<()> { + let iterations = 1000; + + let mut config = Config::new(); + config.allocation_strategy(InstanceAllocationStrategy::Pooling { + strategy: PoolingAllocationStrategy::NextAvailable, + module_limits: ModuleLimits { + memory_pages: 1, + table_elements: 10, + ..Default::default() + }, + instance_limits: InstanceLimits { + count: iterations * 2, + memory_reservation_size: 1, + }, + }); + + let engine = Engine::new(&config)?; + let module = Module::new(&engine, r#"(module (memory 1) (table 10 funcref))"#)?; + + let lazy_total_time: Duration = (0..iterations) + .into_iter() + .map(|_| lazy_thread_instantiate(engine.clone(), module.clone())) + .sum(); + + let (eager_init_total, eager_inst_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 total: {:?}, eager init: {:?}, eager inst: {:?}", + lazy_total_time, eager_init_total, eager_inst_total + ); + + Ok(()) +} + +fn lazy_thread_instantiate(engine: Engine, module: Module) -> Duration { + thread::spawn(move || { + let mut store = Store::new(&engine, ()); + let start = Instant::now(); + Instance::new(&mut store, &module, &[]).expect("instantiate"); + start.elapsed() + }) + .join() + .expect("thread joins") +} + +fn eager_thread_instantiate(engine: Engine, module: Module) -> (Duration, Duration) { + thread::spawn(move || { + let init_start = Instant::now(); + Engine::tls_eager_initialize().expect("eager init"); + let init_duration = init_start.elapsed(); + + let mut store = Store::new(&engine, ()); + let inst_start = Instant::now(); + Instance::new(&mut store, &module, &[]).expect("instantiate"); + (init_duration, inst_start.elapsed()) + }) + .join() + .expect("thread joins") +}