Multithreaded benchmark
This commit is contained in:
+38
-9
@@ -1,14 +1,24 @@
|
||||
use std::sync::Arc;
|
||||
use std::{
|
||||
collections::{HashMap, HashSet},
|
||||
sync::{Arc, Mutex},
|
||||
};
|
||||
|
||||
use osui::prelude::*;
|
||||
|
||||
pub fn main() {
|
||||
let engine = Benchmark::new(Console::new());
|
||||
let mut benchmark_result = Vec::new();
|
||||
let engine = Arc::new(Benchmark::new(Console::new()));
|
||||
let benchmark_result = Arc::new(Mutex::new(HashMap::new()));
|
||||
let benchmark_running = Arc::new(Mutex::new(HashSet::new()));
|
||||
|
||||
for items in 0..10 {
|
||||
benchmark_result.push(
|
||||
engine
|
||||
for items in 0..1000 {
|
||||
let engine = engine.clone();
|
||||
let benchmark_result = benchmark_result.clone();
|
||||
let benchmark_running = benchmark_running.clone();
|
||||
|
||||
std::thread::spawn(move || {
|
||||
benchmark_running.lock().unwrap().insert(items);
|
||||
|
||||
let res = engine
|
||||
.run(move |cx| {
|
||||
rsx! {
|
||||
for _ in (0..items) {
|
||||
@@ -17,9 +27,28 @@ pub fn main() {
|
||||
}
|
||||
.view(cx.clone())
|
||||
})
|
||||
.expect("Failed to run engine"),
|
||||
);
|
||||
.expect("Failed to run engine");
|
||||
|
||||
benchmark_result.lock().unwrap().insert(items, res);
|
||||
benchmark_running.lock().unwrap().remove(&items);
|
||||
});
|
||||
}
|
||||
|
||||
println!("{benchmark_result:?}");
|
||||
while !benchmark_running.lock().unwrap().is_empty() {
|
||||
std::thread::sleep(std::time::Duration::from_millis(500));
|
||||
}
|
||||
|
||||
let r = benchmark_result.lock().unwrap().clone();
|
||||
let mut bench_results = r.iter().collect::<Vec<_>>();
|
||||
|
||||
bench_results.sort_by_key(|(_, bench)| bench.total_render);
|
||||
|
||||
println!(
|
||||
"{}",
|
||||
bench_results
|
||||
.iter()
|
||||
.map(|(key, bench)| format!("Results for {key} items:\n{bench}"))
|
||||
.collect::<Vec<_>>()
|
||||
.join("\n")
|
||||
);
|
||||
}
|
||||
|
||||
@@ -87,3 +87,17 @@ impl<T: Engine> Engine<BenchmarkResult> for Benchmark<T> {
|
||||
self.0.executor()
|
||||
}
|
||||
}
|
||||
|
||||
impl std::fmt::Display for BenchmarkResult {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
write!(
|
||||
f,
|
||||
"- Average: {} ns\n\
|
||||
- Min: {} ns\n\
|
||||
- Max: {} ns\n\
|
||||
- Total Render: {} ns\n\
|
||||
- Total: {} ns",
|
||||
self.average, self.min, self.max, self.total_render, self.total
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user