|
| 1 | +use std::path::PathBuf; |
| 2 | + |
| 3 | +use bevy::utils::HashMap; |
| 4 | +use criterion::{ |
| 5 | + criterion_group, criterion_main, measurement::Measurement, BenchmarkGroup, Criterion, |
| 6 | +}; |
| 7 | +use script_integration_test_harness::{run_lua_benchmark, run_rhai_benchmark}; |
| 8 | +use test_utils::{discover_all_tests, Test}; |
| 9 | + |
| 10 | +extern crate bevy_mod_scripting; |
| 11 | +extern crate script_integration_test_harness; |
| 12 | +extern crate test_utils; |
| 13 | + |
| 14 | +pub trait BenchmarkExecutor { |
| 15 | + fn benchmark_group(&self) -> String; |
| 16 | + fn benchmark_name(&self) -> String; |
| 17 | + fn execute<M: Measurement>(&self, criterion: &mut BenchmarkGroup<M>); |
| 18 | +} |
| 19 | + |
| 20 | +impl BenchmarkExecutor for Test { |
| 21 | + fn benchmark_group(&self) -> String { |
| 22 | + // we want to use OS agnostic paths |
| 23 | + // use the file path from `benchmarks` onwards using folders as groupings |
| 24 | + // replace file separators with `/` |
| 25 | + // replace _ with spaces |
| 26 | + let path = self.path.to_string_lossy(); |
| 27 | + let path = path.split("benchmarks").collect::<Vec<&str>>()[1] |
| 28 | + .replace(std::path::MAIN_SEPARATOR, "/"); |
| 29 | + let first_folder = path.split("/").collect::<Vec<&str>>()[1]; |
| 30 | + first_folder.replace("_", " ") |
| 31 | + } |
| 32 | + |
| 33 | + fn benchmark_name(&self) -> String { |
| 34 | + // use just the file stem |
| 35 | + let name = self |
| 36 | + .path |
| 37 | + .file_stem() |
| 38 | + .unwrap() |
| 39 | + .to_string_lossy() |
| 40 | + .to_string() |
| 41 | + .replace("_", " "); |
| 42 | + |
| 43 | + let language = self.kind.to_string(); |
| 44 | + |
| 45 | + format!("{name} {language}") |
| 46 | + } |
| 47 | + |
| 48 | + fn execute<M: Measurement>(&self, criterion: &mut BenchmarkGroup<M>) { |
| 49 | + match self.kind { |
| 50 | + test_utils::TestKind::Lua => run_lua_benchmark( |
| 51 | + &self.path.to_string_lossy(), |
| 52 | + &self.benchmark_name(), |
| 53 | + criterion, |
| 54 | + ) |
| 55 | + .expect("Benchmark failed"), |
| 56 | + test_utils::TestKind::Rhai => run_rhai_benchmark( |
| 57 | + &self.path.to_string_lossy(), |
| 58 | + &self.benchmark_name(), |
| 59 | + criterion, |
| 60 | + ) |
| 61 | + .expect("benchmark failed"), |
| 62 | + } |
| 63 | + } |
| 64 | +} |
| 65 | + |
| 66 | +fn script_benchmarks(criterion: &mut Criterion) { |
| 67 | + // find manifest dir |
| 68 | + let manifest_dir = PathBuf::from(env!("CARGO_MANIFEST_DIR")); |
| 69 | + let tests = discover_all_tests(manifest_dir, |p| p.starts_with("benchmarks")); |
| 70 | + |
| 71 | + // group by benchmark group |
| 72 | + let mut grouped: HashMap<String, Vec<Test>> = |
| 73 | + tests.into_iter().fold(HashMap::default(), |mut acc, t| { |
| 74 | + acc.entry(t.benchmark_group()).or_default().push(t); |
| 75 | + acc |
| 76 | + }); |
| 77 | + |
| 78 | + // sort within groups by benchmark name |
| 79 | + for (_, tests) in grouped.iter_mut() { |
| 80 | + tests.sort_by_key(|a| a.benchmark_name()); |
| 81 | + } |
| 82 | + |
| 83 | + for (group, tests) in grouped { |
| 84 | + let mut benchmark_group = criterion.benchmark_group(group); |
| 85 | + |
| 86 | + for t in tests { |
| 87 | + t.execute(&mut benchmark_group); |
| 88 | + } |
| 89 | + |
| 90 | + benchmark_group.finish(); |
| 91 | + } |
| 92 | +} |
| 93 | + |
| 94 | +criterion_group!(benches, script_benchmarks); |
| 95 | +criterion_main!(benches); |
0 commit comments