-
-
Notifications
You must be signed in to change notification settings - Fork 55
Expand file tree
/
Copy pathbenchmarks.rs
More file actions
301 lines (270 loc) · 9.46 KB
/
benchmarks.rs
File metadata and controls
301 lines (270 loc) · 9.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
extern crate bevy_mod_scripting;
extern crate script_integration_test_harness;
extern crate test_utils;
use std::{collections::HashMap, path::PathBuf, sync::LazyLock, time::Duration};
use bevy::{
log::{
tracing, tracing::span, tracing_subscriber, tracing_subscriber::layer::SubscriberExt, Level,
},
reflect::Reflect,
};
use bevy_mod_scripting_core::bindings::{
FromScript, IntoScript, Mut, Ref, ReflectReference, ScriptValue, Val,
};
use criterion::{
criterion_main, measurement::Measurement, BatchSize, BenchmarkFilter, BenchmarkGroup, Criterion,
};
use regex::Regex;
use script_integration_test_harness::{
make_test_lua_plugin, make_test_rhai_plugin, perform_benchmark_with_generator,
run_lua_benchmark, run_plugin_script_load_benchmark, run_rhai_benchmark,
test_functions::rand::Rng,
};
use test_utils::{discover_all_tests, Test};
static ENABLE_PROFILING: LazyLock<bool> =
LazyLock::new(|| std::env::var("ENABLE_PROFILING").is_ok());
pub trait BenchmarkExecutor {
fn benchmark_group(&self) -> String;
fn benchmark_name(&self) -> String;
fn execute<M: Measurement>(&self, criterion: &mut BenchmarkGroup<M>);
}
impl BenchmarkExecutor for Test {
fn benchmark_group(&self) -> String {
// we want to use OS agnostic paths
// use the file path from `benchmarks` onwards using folders as groupings
// replace file separators with `/`
// replace _ with spaces
let path = self.path.to_string_lossy();
let path = path.split("benchmarks").collect::<Vec<&str>>()[1]
.replace(std::path::MAIN_SEPARATOR, "/");
let first_folder = path.split("/").collect::<Vec<&str>>()[1];
first_folder.replace("_", " ")
}
fn benchmark_name(&self) -> String {
// use just the file stem
let name = self
.path
.file_stem()
.unwrap()
.to_string_lossy()
.to_string()
.replace("_", " ");
let language = self.kind.to_string();
format!("{name} {language}")
}
fn execute<M: Measurement>(&self, criterion: &mut BenchmarkGroup<M>) {
match self.kind {
test_utils::TestKind::Lua => run_lua_benchmark(
&self.path.to_string_lossy(),
&self.benchmark_name(),
criterion,
)
.expect("Benchmark failed"),
test_utils::TestKind::Rhai => run_rhai_benchmark(
&self.path.to_string_lossy(),
&self.benchmark_name(),
criterion,
)
.expect("benchmark failed"),
}
}
}
fn script_benchmarks(criterion: &mut Criterion, filter: Option<Regex>) {
// find manifest dir
let manifest_dir = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
let tests = discover_all_tests(manifest_dir, |p| {
p.path.starts_with("benchmarks")
&& if let Some(filter) = &filter {
let matching = filter.is_match(&p.benchmark_name());
if !matching {
println!(
"Skipping benchmark: '{}'. due to filter: '{filter}'",
p.benchmark_name()
);
};
matching
} else {
true
}
});
// group by benchmark group
let mut grouped: HashMap<String, Vec<Test>> =
tests.into_iter().fold(HashMap::default(), |mut acc, t| {
acc.entry(t.benchmark_group()).or_default().push(t);
acc
});
// sort within groups by benchmark name
for (_, tests) in grouped.iter_mut() {
tests.sort_by_key(|a| a.benchmark_name());
}
for (group, tests) in grouped {
println!("Running benchmarks for group: {}", group);
let mut benchmark_group = criterion.benchmark_group(group);
for t in tests {
println!("Running benchmark: {}", t.benchmark_name());
span!(
Level::INFO,
"Benchmark harness for test",
test_name = &t.benchmark_name()
);
t.execute(&mut benchmark_group);
}
benchmark_group.finish();
}
}
fn maybe_with_profiler(f: impl Fn(bool)) {
if *ENABLE_PROFILING {
println!("profiling enabled, make sure to run tracy. If using it across windows/WSL you can use something like `tracy-capture.exe -o output.tracy -a localhost` on windows");
// set global tracing subscriber so bevy doesn't set it itself first
let subscriber = tracing_subscriber::Registry::default();
let tracy_layer = tracing_tracy::TracyLayer::default();
let subscriber = subscriber.with(tracy_layer);
tracing::subscriber::set_global_default(subscriber).unwrap();
f(true);
} else {
f(false);
}
}
/// benchmarks measuring conversion time for script values and other things
fn conversion_benchmarks(criterion: &mut Criterion) {
let mut group = criterion.benchmark_group("conversions");
#[derive(Reflect)]
struct ReflectyVal(pub u32);
perform_benchmark_with_generator(
"ScriptValue::List",
&|rng, _| {
let mut array = Vec::new();
for _ in 0..10 {
array.push(ScriptValue::Integer(rng.random()));
}
ScriptValue::List(array)
},
&|w, i| {
let i = i.into_script(w.clone()).unwrap();
let _ = Vec::<ScriptValue>::from_script(i, w).unwrap();
},
&mut group,
BatchSize::SmallInput,
);
perform_benchmark_with_generator(
"ScriptValue::Map",
&|rng, _| {
let mut map = HashMap::default();
for _ in 0..10 {
map.insert(
rng.random::<u32>().to_string(),
ScriptValue::Integer(rng.random()),
);
}
ScriptValue::Map(map)
},
&|w, i| {
let i = i.into_script(w.clone()).unwrap();
let _ = HashMap::<String, ScriptValue>::from_script(i, w).unwrap();
},
&mut group,
BatchSize::SmallInput,
);
perform_benchmark_with_generator(
"ScriptValue::Reference::from_into",
&|rng, world| {
let allocator = world.allocator();
let mut allocator = allocator.write();
ReflectReference::new_allocated(ReflectyVal(rng.random()), &mut allocator)
},
&|w, i| {
let i = i.into_script(w.clone()).unwrap();
let _ = ReflectReference::from_script(i, w).unwrap();
},
&mut group,
BatchSize::SmallInput,
);
perform_benchmark_with_generator(
"Val<T>::from_into",
&|rng, _| Val::new(ReflectyVal(rng.random::<u32>())),
&|w, i| {
let v = i.into_script(w.clone()).unwrap();
Val::<ReflectyVal>::from_script(v, w).unwrap();
},
&mut group,
BatchSize::SmallInput,
);
perform_benchmark_with_generator(
"Ref<T>::from",
&|rng, w| {
Val::new(ReflectyVal(rng.random::<u32>()))
.into_script(w)
.unwrap()
},
&|w, i| {
Ref::<ReflectyVal>::from_script(i, w).unwrap();
},
&mut group,
BatchSize::SmallInput,
);
perform_benchmark_with_generator(
"Mut<T>::from",
&|rng, w| {
Val::new(ReflectyVal(rng.random::<u32>()))
.into_script(w)
.unwrap()
},
&|w, i| {
Mut::<ReflectyVal>::from_script(i, w).unwrap();
},
&mut group,
BatchSize::SmallInput,
);
}
fn script_load_benchmarks(criterion: &mut Criterion) {
let mut group = criterion.benchmark_group("loading");
let reload_probability = 0.5;
// lua
let plugin = make_test_lua_plugin();
let content = include_str!("../assets/macro_benchmarks/loading/empty.lua");
run_plugin_script_load_benchmark(
plugin,
"empty Lua",
content,
&mut group,
|rand| format!("{rand}.lua"),
reload_probability,
);
// rhai
let plugin = make_test_rhai_plugin();
let content = include_str!("../assets/macro_benchmarks/loading/empty.rhai");
run_plugin_script_load_benchmark(
plugin,
"empty Rhai",
content,
&mut group,
|rand| format!("{rand}.rhai"),
reload_probability,
);
}
pub fn benches() {
maybe_with_profiler(|_profiler| {
let mut criterion: criterion::Criterion<_> = (criterion::Criterion::default())
.configure_from_args()
.measurement_time(Duration::from_secs(10));
let arguments = std::env::args()
.skip(1) // the executable name
.filter(|a| !a.starts_with("-"))
.collect::<Vec<String>>();
// take first argument as .*<val>.* regex for benchmarks
// criterion will already have that as a filter, but we want to make sure we're on the same page
let filter = if let Some(n) = arguments.first() {
println!("using filter: '{n}'");
let regex = Regex::new(n).unwrap();
let filter = BenchmarkFilter::Regex(regex.clone());
criterion = criterion.with_benchmark_filter(filter);
Some(regex)
} else {
None
};
script_benchmarks(&mut criterion, filter);
conversion_benchmarks(&mut criterion);
script_load_benchmarks(&mut criterion);
});
}
criterion_main!(benches);