Skip to main content

streaming_crypto/core_api/benchmarks/
bench_runner.rs

1
2
3use std::future::Future;
4use std::pin::Pin;
5
6use crate::{
7    benchmarks::{
8        bench_persists::save_json, bench_results::{BenchmarkResult, print_results}, bench_utils::dummy_master_key
9    }, 
10    stream_v2::core::MasterKey
11};
12
13// Sync benchmarks
14pub fn run_sync_benchmarks(_master_key: &MasterKey) -> Vec<BenchmarkResult> {
15    let results = Vec::new();
16    results
17}
18
19// Async benchmarks
20pub async fn run_async_benchmarks(_master_key: &MasterKey) -> Vec<BenchmarkResult> {
21    let results = Vec::new();
22    results
23}
24
25// Type alias for async future
26// pub type BenchFuture<'a> =
27//     Pin<Box<dyn Future<Output = Vec<BenchmarkResult>> + Send + 'a>>;
28
29pub type BenchFuture =
30    Pin<Box<dyn Future<Output = Vec<BenchmarkResult>> + Send>>;
31
32// Main orchestration
33pub fn bench_v2_main(
34    sync_benchmark_fn: Option<Box<dyn Fn() -> Vec<BenchmarkResult>>>,
35    async_benchmark_fn: Option<Box<dyn Fn() -> BenchFuture + Send>>,
36) {
37    let master_key = dummy_master_key(); // own it
38
39    let sync_fn = sync_benchmark_fn.unwrap_or_else(|| {
40        let key = master_key.clone();
41        Box::new(move || run_sync_benchmarks(&key))
42    });
43
44    let async_fn = async_benchmark_fn.unwrap_or_else(|| {
45        let key = master_key.clone();
46        Box::new(move || {
47            let key = key.clone();
48            Box::pin(async move {
49                run_async_benchmarks(&key).await
50            })
51        })
52    });
53
54    println!("Running sync benchmarks...");
55    let sync_results = sync_fn();
56    print_results(&sync_results, "Sync benchmarks");
57
58    let mut all_results = sync_results;
59
60    println!("\nRunning async benchmarks...");
61    let async_results = futures::executor::block_on(async_fn());
62    print_results(&async_results, "Async benchmarks");
63    
64    all_results.extend(async_results);
65
66    save_json(&all_results, None, "results_v2".into());
67}
68
69// ### 🔑 Key Notes
70// - `BenchmarkResult` is a placeholder; adapt fields to our actual benchmark struct.
71// - `run_sync_benchmarks` and `run_async_benchmarks` currently return empty vectors — we’ll plug in our real benchmark logic.
72// - `bench_v3_main` orchestrates sync and async runs, prints results, and saves JSON.
73// - Uses `tokio::Runtime::block_on` to run async benchmarks inside a sync context, just like Python’s `asyncio.run`.