Chunked threads
This commit is contained in:
+22
-27
@@ -1,23 +1,21 @@
|
|||||||
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 max_threads = 128;
|
||||||
|
let mut handles = vec![];
|
||||||
|
|
||||||
|
for chunk in (0..500).collect::<Vec<_>>().chunks(max_threads) {
|
||||||
|
for &items in chunk {
|
||||||
let engine = engine.clone();
|
let engine = engine.clone();
|
||||||
let benchmark_result = benchmark_result.clone();
|
let tx = tx.clone();
|
||||||
let benchmark_running = benchmark_running.clone();
|
|
||||||
|
|
||||||
std::thread::spawn(move || {
|
|
||||||
benchmark_running.lock().unwrap().insert(items);
|
|
||||||
|
|
||||||
|
let handle = thread::spawn(move || {
|
||||||
let res = engine
|
let res = engine
|
||||||
.run(move |cx| {
|
.run(move |cx| {
|
||||||
rsx! {
|
rsx! {
|
||||||
@@ -29,26 +27,23 @@ pub fn main() {
|
|||||||
})
|
})
|
||||||
.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);
|
||||||
}
|
}
|
||||||
|
|
||||||
while !benchmark_running.lock().unwrap().is_empty() {
|
for handle in handles.drain(..) {
|
||||||
std::thread::sleep(std::time::Duration::from_millis(500));
|
handle.join().expect("Thread panicked");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
let r = benchmark_result.lock().unwrap().clone();
|
drop(tx);
|
||||||
let mut bench_results = r.iter().collect::<Vec<_>>();
|
|
||||||
|
|
||||||
bench_results.sort_by_key(|(_, bench)| bench.total_render);
|
let mut results: Vec<(usize, BenchmarkResult)> = rx.iter().collect();
|
||||||
|
results.sort_by_key(|(items, _)| *items);
|
||||||
|
|
||||||
println!(
|
for (items, bench) in results {
|
||||||
"{}",
|
println!("Results for {items} items:\n{bench}");
|
||||||
bench_results
|
}
|
||||||
.iter()
|
|
||||||
.map(|(key, bench)| format!("Results for {key} items:\n{bench}"))
|
|
||||||
.collect::<Vec<_>>()
|
|
||||||
.join("\n")
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user