Skip to content

Commit d69c4f5

Browse files
committed
Add SPEEDTEST
1 parent 15ed281 commit d69c4f5

File tree

2 files changed

+64
-6
lines changed

2 files changed

+64
-6
lines changed

book/src/development/speedtest.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# Speedtest
2+
`SPEEDTEST` is the tool we use to measure lint's performance, it works by executing the same test several times.
3+
4+
It's useful for measuring changes to current lints and deciding if the performance changes too much. `SPEEDTEST` is
5+
accessed by the `SPEEDTEST` (and `SPEEDTEST_*`) environment variables.
6+
7+
## Checking Speedtest
8+
9+
To do a simple speed test of a lint (e.g. `allow_attributes`), use this command.
10+
11+
```sh
12+
$ SPEEDTEST=ui TESTNAME="allow_attributes" cargo uitest -- --nocapture
13+
```
14+
15+
This will test all `ui` tests (`SPEEDTEST=ui`) whose names start with `allow_attributes`. By default, `SPEEDTEST` will
16+
iterate your test 1000 times. But you can change this with `SPEEDTEST_ITERATIONS`.
17+
18+
```sh
19+
$ SPEEDTEST=toml SPEEDTEST_ITERATIONS=100 TESTNAME="semicolon_block" cargo uitest -- --nocapture
20+
```
21+
22+
> **WARNING**: Be sure to use `-- --nocapture` at the end of the command to see the average test time. If you don't
23+
> use `-- --nocapture` (e.g. `SPEEDTEST=ui` `TESTNAME="let_underscore_untyped" cargo uitest -- --nocapture`), this
24+
> will not show up.

tests/compile-test.rs

Lines changed: 40 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -213,12 +213,46 @@ fn run_ui_cargo() {
213213

214214
fn main() {
215215
set_var("CLIPPY_DISABLE_DOCS_LINKS", "true");
216-
run_ui();
217-
run_ui_toml();
218-
run_ui_cargo();
219-
run_internal_tests();
220-
rustfix_coverage_known_exceptions_accuracy();
221-
ui_cargo_toml_metadata();
216+
// The SPEEDTEST_* env variables can be used to check Clippy's performance on your PR. It runs the
217+
// affected test 1000 times and gets the average.
218+
if let Ok(speedtest) = std::env::var("SPEEDTEST") {
219+
println!("----------- STARTING SPEEDTEST -----------");
220+
let f;
221+
match speedtest.as_str() {
222+
"ui" => f = run_ui as fn(),
223+
"cargo" => f = run_ui_cargo as fn(),
224+
"toml" => f = run_ui_toml as fn(),
225+
"internal" => f = run_internal_tests as fn(),
226+
"rustfix-coverage-known-exceptions-accuracy" => f = rustfix_coverage_known_exceptions_accuracy as fn(),
227+
"ui-cargo-toml-metadata" => f = ui_cargo_toml_metadata() as fn(),
228+
229+
_ => panic!("unknown speedtest: {speedtest} || accepted speedtests are: [ui, cargo, toml, internal]"),
230+
}
231+
232+
let iterations;
233+
if let Ok(iterations_str) = std::env::var("SPEEDTEST_ITERATIONS") {
234+
iterations = iterations_str
235+
.parse::<u64>()
236+
.unwrap_or_else(|_| panic!("Couldn't parse `{}`, please use a valid u64", iterations_str));
237+
} else {
238+
iterations = 1000;
239+
}
240+
241+
let mut sum = 0;
242+
for _ in 0..iterations {
243+
let start = std::time::Instant::now();
244+
f();
245+
sum += start.elapsed().as_millis();
246+
}
247+
println!("average {} time: {} millis.", speedtest.to_uppercase(), sum / 1000);
248+
} else {
249+
run_ui();
250+
run_ui_toml();
251+
run_ui_cargo();
252+
run_internal_tests();
253+
rustfix_coverage_known_exceptions_accuracy();
254+
ui_cargo_toml_metadata();
255+
}
222256
}
223257

224258
const RUSTFIX_COVERAGE_KNOWN_EXCEPTIONS: &[&str] = &[

0 commit comments

Comments
 (0)