-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
misc: add compare test runs tool (#4495)
- Loading branch information
Showing
6 changed files
with
137 additions
and
4 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
# Tools | ||
|
||
The `tools` crate contains various utilities useful for development. | ||
|
||
## Benchmarking | ||
|
||
Run the hardhat test command in a repo 5 times and report the times it took: | ||
|
||
```bash | ||
# From the repo root | ||
cargo run --bin tools benchmark -i 5 /repo/path -t "npx hardhat test" | ||
``` | ||
|
||
## Compare test run execution times | ||
|
||
Create a provider test execution report for the base branch: | ||
|
||
```bash | ||
# From packages/hardhat-core in the base branch | ||
yarn build && yarn test:provider --reporter json | tee base-test-provider-logs.json | ||
``` | ||
|
||
Create a provider test execution report for the candidate branch: | ||
|
||
```bash | ||
# From packages/hardhat-core in the candidate branch | ||
yarn build && yarn test:provider --reporter json | tee candidate-test-provider-logs.json | ||
``` | ||
|
||
Generate a comparison report that will list slower tests in the candidate branch: | ||
|
||
```bash | ||
# From the repo root | ||
cargo run --bin tools compare-test-runs base-test-provider-logs.json candidate-test-provider-logs.json > comparisions.txt | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
use std::collections::BTreeMap; | ||
use std::path::Path; | ||
|
||
use anyhow::Result; | ||
|
||
#[derive(Debug, PartialEq, Eq, serde::Deserialize)] | ||
#[serde(rename_all = "camelCase")] | ||
struct TestRun { | ||
title: String, | ||
full_title: String, | ||
file: String, | ||
duration: Option<u32>, | ||
current_retry: u32, | ||
speed: Option<String>, | ||
err: serde_json::Value, | ||
} | ||
|
||
#[derive(Debug, PartialEq, Eq, serde::Deserialize)] | ||
struct TestRuns { | ||
tests: Vec<TestRun>, | ||
} | ||
|
||
#[derive(Debug)] | ||
struct Diff { | ||
test_title: String, | ||
absolute_diff: u32, | ||
increase_percent: f64, | ||
} | ||
|
||
pub(crate) fn compare(baseline: &Path, candidate: &Path) -> Result<()> { | ||
let baseline_runs = read_test_runs(baseline)?; | ||
let candidate_runs = read_test_runs(candidate)?; | ||
let mut diffs = Vec::new(); | ||
for (key, candidate_run) in candidate_runs.iter() { | ||
if let Some(baseline_run) = baseline_runs.get(key) { | ||
match (baseline_run.duration, candidate_run.duration) { | ||
(Some(base_duration), Some(candidate_duration)) => { | ||
if base_duration < candidate_duration { | ||
let absolute_diff = candidate_duration - base_duration; | ||
let increase_percent = if base_duration == 0 { | ||
100.0 | ||
} else { | ||
f64::from(candidate_duration) / f64::from(base_duration) * 100.0 - 100.0 | ||
}; | ||
diffs.push(Diff { | ||
test_title: candidate_run.title.clone(), | ||
absolute_diff, | ||
increase_percent, | ||
}); | ||
} | ||
} | ||
(Some(_), None) => println!("No candidate duration for `{key}`"), | ||
(None, Some(_)) => println!("No baseline duration for `{key}`"), | ||
(None, None) => {} | ||
} | ||
} else { | ||
println!("`{}` is a new test", candidate_run.title); | ||
} | ||
} | ||
|
||
diffs.sort_by(|a, b| b.absolute_diff.cmp(&a.absolute_diff)); | ||
for diff in &diffs { | ||
println!( | ||
"`{}` is slower than baseline. Absolute diff: {} ms. Increase: {:.2} %", | ||
diff.test_title, diff.absolute_diff, diff.increase_percent | ||
); | ||
} | ||
|
||
Ok(()) | ||
} | ||
|
||
fn read_test_runs(json_path: &Path) -> Result<BTreeMap<String, TestRun>> { | ||
let runs: TestRuns = serde_json::from_reader(std::fs::File::open(json_path)?)?; | ||
let result = runs | ||
.tests | ||
.into_iter() | ||
.map(|run| (run.full_title.clone(), run)) | ||
.collect(); | ||
Ok(result) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters