| 12 | } |
| 13 | |
| 14 | fn bench_multi_threaded_traps(c: &mut Criterion) { |
| 15 | let mut group = c.benchmark_group("multi-threaded-traps"); |
| 16 | |
| 17 | for num_bg_threads in vec![0, 1, 2, 4, 8, 16] { |
| 18 | group.throughput(Throughput::Elements(num_bg_threads)); |
| 19 | group.bench_with_input( |
| 20 | BenchmarkId::from_parameter(num_bg_threads), |
| 21 | &num_bg_threads, |
| 22 | |b, &num_bg_threads| { |
| 23 | let engine = Engine::default(); |
| 24 | let module = module(&engine, 10).unwrap(); |
| 25 | |
| 26 | b.iter_custom(|iters| { |
| 27 | let (started_sender, started_receiver) = std::sync::mpsc::channel(); |
| 28 | |
| 29 | // Spawn threads in the background doing infinite work. |
| 30 | let threads = (0..num_bg_threads) |
| 31 | .map(|_| { |
| 32 | let (done_sender, done_receiver) = std::sync::mpsc::channel(); |
| 33 | let handle = std::thread::spawn({ |
| 34 | let engine = engine.clone(); |
| 35 | let module = module.clone(); |
| 36 | let started_sender = started_sender.clone(); |
| 37 | move || { |
| 38 | let mut store = Store::new(&engine, ()); |
| 39 | let instance = Instance::new(&mut store, &module, &[]).unwrap(); |
| 40 | let f = |
| 41 | instance.get_typed_func::<(), ()>(&mut store, "").unwrap(); |
| 42 | |
| 43 | // Notify the parent thread that we are |
| 44 | // doing background work now. |
| 45 | started_sender.send(()).unwrap(); |
| 46 | |
| 47 | // Keep doing background work until the |
| 48 | // parent tells us to stop. |
| 49 | loop { |
| 50 | if let Ok(()) = done_receiver.try_recv() { |
| 51 | return; |
| 52 | } |
| 53 | assert!(f.call(&mut store, ()).is_err()); |
| 54 | } |
| 55 | } |
| 56 | }); |
| 57 | (handle, done_sender) |
| 58 | }) |
| 59 | .collect::<Vec<_>>(); |
| 60 | |
| 61 | // Wait on all the threads to start up. |
| 62 | for _ in 0..num_bg_threads { |
| 63 | let _ = started_receiver.recv().unwrap(); |
| 64 | } |
| 65 | |
| 66 | let mut store = Store::new(&engine, ()); |
| 67 | let instance = Instance::new(&mut store, &module, &[]).unwrap(); |
| 68 | let f = instance.get_typed_func::<(), ()>(&mut store, "").unwrap(); |
| 69 | |
| 70 | // Measure how long it takes to do `iters` worth of traps |
| 71 | // while there is a bunch of background work going on. |