diff --git a/benches/common.rs b/benches/common.rs index 593c8c9a..9fb37e6f 100644 --- a/benches/common.rs +++ b/benches/common.rs @@ -65,3 +65,45 @@ pub fn remote_loopback_deploy( .unwrap_or_else(|e| panic!("Remote worker for host {host_id} crashed: {e:?}")); } } + +pub struct NoirBenchBuilder +where + F: Fn() -> StreamEnvironment, + G: Fn(&mut StreamEnvironment) -> R, +{ + make_env: F, + make_network: G, + _result: PhantomData, +} + +impl NoirBenchBuilder +where + F: Fn() -> StreamEnvironment, + G: Fn(&mut StreamEnvironment) -> R, +{ + pub fn new(make_env: F, make_network: G) -> Self { + Self { + make_env, + make_network, + _result: Default::default(), + } + } + + pub fn bench(&self, n: u64) -> Duration { + let mut time = Duration::default(); + for _ in 0..n { + let mut env = (self.make_env)(); + let _result = (self.make_network)(&mut env); + let start = Instant::now(); + env.execute_blocking(); + time += start.elapsed(); + black_box(_result); + } + time + } +} + +pub fn noir_bench_default(b: &mut Bencher, logic: impl Fn(&mut StreamEnvironment)) { + let builder = NoirBenchBuilder::new(StreamEnvironment::default, logic); + b.iter_custom(|n| builder.bench(n)); +}