Skip to content

Commit 9b0099f

Browse files
committed
Add SPEEDTEST
1 parent d44ea7c commit 9b0099f

File tree

2 files changed

+59
-4
lines changed

2 files changed

+59
-4
lines changed

book/src/development/speedtest.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
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 will not show up.

tests/compile-test.rs

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -355,10 +355,42 @@ fn run_ui_cargo() {
355355
#[test]
356356
fn compile_test() {
357357
set_var("CLIPPY_DISABLE_DOCS_LINKS", "true");
358-
run_ui();
359-
run_ui_toml();
360-
run_ui_cargo();
361-
run_internal_tests();
358+
// The SPEEDTEST_* env variables can be used to check Clippy's performance on your PR. It runs the
359+
// affected test 1000 times and gets the average.
360+
if let Ok(speedtest) = std::env::var("SPEEDTEST") {
361+
println!("----------- STARTING SPEEDTEST -----------");
362+
let f;
363+
match speedtest.as_str() {
364+
"ui" => f = run_ui as fn(),
365+
"cargo" => f = run_ui_cargo as fn(),
366+
"toml" => f = run_ui_toml as fn(),
367+
"internal" => f = run_internal_tests as fn(),
368+
369+
_ => panic!("unknown speedtest: {speedtest} || accepted speedtests are: [ui, cargo, toml, internal]"),
370+
}
371+
372+
let iterations;
373+
if let Ok(iterations_str) = std::env::var("SPEEDTEST_ITERATIONS") {
374+
iterations = iterations_str
375+
.parse::<u64>()
376+
.unwrap_or_else(|_| panic!("Couldn't parse `{}`, please use a valid u64", iterations_str));
377+
} else {
378+
iterations = 1000;
379+
}
380+
381+
let mut sum = 0;
382+
for _ in 0..iterations {
383+
let start = std::time::Instant::now();
384+
f();
385+
sum += start.elapsed().as_millis();
386+
}
387+
println!("average {} time: {} millis.", speedtest.to_uppercase(), sum / 1000);
388+
} else {
389+
run_ui();
390+
run_ui_toml();
391+
run_ui_cargo();
392+
run_internal_tests();
393+
}
362394
}
363395

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

0 commit comments

Comments
 (0)