-
Notifications
You must be signed in to change notification settings - Fork 157
Create bench_cmp
command
#1955
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Create bench_cmp
command
#1955
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,126 @@ | ||
use std::sync::Arc; | ||
|
||
use database::{ | ||
metric::Metric, | ||
selector::{BenchmarkQuery, CompileBenchmarkQuery}, | ||
ArtifactId, Connection, | ||
}; | ||
use tabled::{Table, Tabled}; | ||
|
||
/// Compare 2 artifacts and print the result. | ||
pub async fn compare_artifacts( | ||
mut conn: Box<dyn Connection>, | ||
base: String, | ||
modified: String, | ||
) -> anyhow::Result<()> { | ||
let index = database::Index::load(&mut *conn).await; | ||
|
||
let query = CompileBenchmarkQuery::default() | ||
.metric(database::selector::Selector::One(Metric::InstructionsUser)); | ||
let resp = query | ||
.execute( | ||
&mut *conn, | ||
&index, | ||
Arc::new(vec![ArtifactId::Tag(base), ArtifactId::Tag(modified)]), | ||
) | ||
.await | ||
.unwrap(); | ||
|
||
let tuple_pstats = resp | ||
.into_iter() | ||
.map(|resp| { | ||
let points = resp.series.points.collect::<Vec<_>>(); | ||
(points[0], points[1]) | ||
}) | ||
.collect::<Vec<_>>(); | ||
|
||
#[derive(Tabled)] | ||
struct Regression { | ||
count: usize, | ||
#[tabled(display_with = "display_range")] | ||
range: (Option<f64>, Option<f64>), | ||
#[tabled(display_with = "display_mean")] | ||
mean: Option<f64>, | ||
} | ||
|
||
fn format_value(value: Option<f64>) -> String { | ||
match value { | ||
Some(value) => format!("{:+.2}%", value), | ||
None => "-".to_string(), | ||
} | ||
} | ||
|
||
fn display_range(&(min, max): &(Option<f64>, Option<f64>)) -> String { | ||
format!("[{}, {}]", &format_value(min), &format_value(max)) | ||
} | ||
|
||
fn display_mean(value: &Option<f64>) -> String { | ||
match value { | ||
Some(value) => format!("{:+.2}%", value), | ||
None => "-".to_string(), | ||
} | ||
} | ||
|
||
impl From<&Vec<f64>> for Regression { | ||
fn from(value: &Vec<f64>) -> Self { | ||
let min = value.iter().copied().min_by(|a, b| a.total_cmp(b)); | ||
let max = value.iter().copied().max_by(|a, b| a.total_cmp(b)); | ||
let count = value.len(); | ||
|
||
Regression { | ||
range: (min, max), | ||
count, | ||
mean: if count == 0 { | ||
None | ||
} else { | ||
Some(value.iter().sum::<f64>() / count as f64) | ||
}, | ||
} | ||
} | ||
} | ||
|
||
let change = tuple_pstats | ||
.iter() | ||
.filter_map(|&(a, b)| match (a, b) { | ||
(Some(a), Some(b)) => { | ||
if a == 0.0 { | ||
s7tya marked this conversation as resolved.
Show resolved
Hide resolved
|
||
None | ||
} else { | ||
Some((b - a) / a) | ||
} | ||
} | ||
(_, _) => None, | ||
}) | ||
.collect::<Vec<_>>(); | ||
let negative_change = change | ||
.iter() | ||
.copied() | ||
.filter(|&c| c < 0.0) | ||
.collect::<Vec<_>>(); | ||
let positive_change = change | ||
.iter() | ||
.copied() | ||
.filter(|&c| c > 0.0) | ||
.collect::<Vec<_>>(); | ||
|
||
#[derive(Tabled)] | ||
struct NamedRegression { | ||
name: String, | ||
#[tabled(inline)] | ||
regression: Regression, | ||
} | ||
|
||
let regressions = [negative_change, positive_change, change] | ||
.into_iter() | ||
.map(|c| Regression::from(&c)) | ||
.zip(["❌", "✅", "✅, ❌"]) | ||
.map(|(c, label)| NamedRegression { | ||
name: label.to_string(), | ||
regression: c, | ||
}) | ||
.collect::<Vec<_>>(); | ||
|
||
println!("{}", Table::new(regressions)); | ||
|
||
Ok(()) | ||
} |
This file contains hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.