Chunked threads

This commit is contained in:
2026-01-30 18:07:28 +01:00
parent 5e2e9952d9
commit 1526daaba2
+35 -40
View File
@@ -1,54 +1,49 @@
use std::{ use std::{sync::mpsc, sync::Arc, thread};
collections::{HashMap, HashSet},
sync::{Arc, Mutex},
};
use osui::prelude::*; use osui::prelude::*;
pub fn main() { pub fn main() {
let engine = Arc::new(Benchmark::new(Console::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..1000 { let (tx, rx) = mpsc::channel();
let engine = engine.clone();
let benchmark_result = benchmark_result.clone();
let benchmark_running = benchmark_running.clone();
std::thread::spawn(move || { let max_threads = 128;
benchmark_running.lock().unwrap().insert(items); let mut handles = vec![];
let res = engine for chunk in (0..500).collect::<Vec<_>>().chunks(max_threads) {
.run(move |cx| { for &items in chunk {
rsx! { let engine = engine.clone();
for _ in (0..items) { let tx = tx.clone();
"Hello, world!"
let handle = thread::spawn(move || {
let res = engine
.run(move |cx| {
rsx! {
for _ in (0..items) {
"Hello, world!"
}
} }
} .view(cx.clone())
.view(cx.clone()) })
}) .expect("Failed to run engine");
.expect("Failed to run engine");
benchmark_result.lock().unwrap().insert(items, res); tx.send((items, res)).expect("Failed to send result");
benchmark_running.lock().unwrap().remove(&items); });
});
handles.push(handle);
}
for handle in handles.drain(..) {
handle.join().expect("Thread panicked");
}
} }
while !benchmark_running.lock().unwrap().is_empty() { drop(tx);
std::thread::sleep(std::time::Duration::from_millis(500));
let mut results: Vec<(usize, BenchmarkResult)> = rx.iter().collect();
results.sort_by_key(|(items, _)| *items);
for (items, bench) in results {
println!("Results for {items} items:\n{bench}");
} }
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")
);
} }