From c9407cf7aeba132657011d955f3ea99e63c32f7b Mon Sep 17 00:00:00 2001 From: imDema Date: Fri, 19 Jan 2024 10:33:27 +0100 Subject: [PATCH] Add criterion harness for internal stream measurements --- benches/common.rs | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) 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)); +}