mirror of
https://github.com/servo/servo.git
synced 2025-08-06 22:15:33 +01:00
Auto merge of #12327 - malisas:profile-stats, r=jdm
Profile stats <!-- Please describe your changes on the following line: --> Pulled out profiler statistics calculation into its own function in time.rs, added tests for it, and simplified min and max calculation for pre-sorted data. --- <!-- Thank you for contributing to Servo! Please replace each `[ ]` by `[X]` when the step is complete, and replace `__` with appropriate data: --> - [X] `./mach build -d` does not report any errors - [X] `./mach test-tidy` does not report any errors - [ ] These changes fix #__ (github issue number if applicable). <!-- Either: --> - [X] There are tests for these changes OR - [ ] These changes do not require tests because _____ <!-- Pull requests that do not address these steps are welcome, but they will require additional verification as part of the review process. --> <!-- Reviewable:start --> --- This change is [<img src="https://reviewable.io/review_button.svg" height="34" align="absmiddle" alt="Reviewable"/>](https://reviewable.io/reviews/servo/servo/12327) <!-- Reviewable:end -->
This commit is contained in:
commit
b9af49f120
2 changed files with 56 additions and 10 deletions
|
@ -327,6 +327,23 @@ impl Profiler {
|
||||||
true
|
true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Get tuple (mean, median, min, max) for profiler statistics.
|
||||||
|
pub fn get_statistics(data: &[f64]) -> (f64, f64, f64, f64) {
|
||||||
|
data.iter().fold(-f64::INFINITY, |a, &b| {
|
||||||
|
debug_assert!(a < b, "Data must be sorted");
|
||||||
|
b
|
||||||
|
});
|
||||||
|
|
||||||
|
let data_len = data.len();
|
||||||
|
debug_assert!(data_len > 0);
|
||||||
|
let (mean, median, min, max) =
|
||||||
|
(data.iter().sum::<f64>() / (data_len as f64),
|
||||||
|
data[data_len / 2],
|
||||||
|
data[0],
|
||||||
|
data[data_len - 1]);
|
||||||
|
(mean, median, min, max)
|
||||||
|
}
|
||||||
|
|
||||||
fn print_buckets(&mut self) {
|
fn print_buckets(&mut self) {
|
||||||
match self.output {
|
match self.output {
|
||||||
Some(OutputOptions::FileName(ref filename)) => {
|
Some(OutputOptions::FileName(ref filename)) => {
|
||||||
|
@ -349,11 +366,7 @@ impl Profiler {
|
||||||
});
|
});
|
||||||
let data_len = data.len();
|
let data_len = data.len();
|
||||||
if data_len > 0 {
|
if data_len > 0 {
|
||||||
let (mean, median, min, max) =
|
let (mean, median, min, max) = Self::get_statistics(data);
|
||||||
(data.iter().sum::<f64>() / (data_len as f64),
|
|
||||||
data[data_len / 2],
|
|
||||||
data.iter().fold(f64::INFINITY, |a, &b| a.min(b)),
|
|
||||||
data.iter().fold(-f64::INFINITY, |a, &b| a.max(b)));
|
|
||||||
write!(file, "{}\t{}\t{:15.4}\t{:15.4}\t{:15.4}\t{:15.4}\t{:15}\n",
|
write!(file, "{}\t{}\t{:15.4}\t{:15.4}\t{:15.4}\t{:15.4}\t{:15}\n",
|
||||||
category.format(&self.output), meta.format(&self.output),
|
category.format(&self.output), meta.format(&self.output),
|
||||||
mean, median, min, max, data_len).unwrap();
|
mean, median, min, max, data_len).unwrap();
|
||||||
|
@ -378,11 +391,7 @@ impl Profiler {
|
||||||
});
|
});
|
||||||
let data_len = data.len();
|
let data_len = data.len();
|
||||||
if data_len > 0 {
|
if data_len > 0 {
|
||||||
let (mean, median, min, max) =
|
let (mean, median, min, max) = Self::get_statistics(data);
|
||||||
(data.iter().sum::<f64>() / (data_len as f64),
|
|
||||||
data[data_len / 2],
|
|
||||||
data.iter().fold(f64::INFINITY, |a, &b| a.min(b)),
|
|
||||||
data.iter().fold(-f64::INFINITY, |a, &b| a.max(b)));
|
|
||||||
writeln!(&mut lock, "{:-35}{} {:15.4} {:15.4} {:15.4} {:15.4} {:15}",
|
writeln!(&mut lock, "{:-35}{} {:15.4} {:15.4} {:15.4} {:15.4} {:15}",
|
||||||
category.format(&self.output), meta.format(&self.output), mean, median, min, max,
|
category.format(&self.output), meta.format(&self.output), mean, median, min, max,
|
||||||
data_len).unwrap();
|
data_len).unwrap();
|
||||||
|
|
|
@ -15,3 +15,40 @@ fn time_profiler_smoke_test() {
|
||||||
chan.send(ProfilerMsg::Exit(ipcchan));
|
chan.send(ProfilerMsg::Exit(ipcchan));
|
||||||
assert!(true, "Can tell the profiler thread to exit");
|
assert!(true, "Can tell the profiler thread to exit");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn time_profiler_stats_test() {
|
||||||
|
let even_data = vec![1.234, 3.24567, 3.54578, 5.0, 5.324, 7.345,
|
||||||
|
9.2345, 10.2342345, 13.2599, 15.0];
|
||||||
|
let (even_mean, even_median, even_min, even_max) = time::Profiler::get_statistics(&even_data);
|
||||||
|
|
||||||
|
assert_eq!(7.34230845, even_mean);
|
||||||
|
assert_eq!(7.345, even_median);
|
||||||
|
assert_eq!(1.234, even_min);
|
||||||
|
assert_eq!(15.0, even_max);
|
||||||
|
|
||||||
|
let odd_data = vec![1.234, 3.24567, 3.54578, 5.0, 5.324, 7.345,
|
||||||
|
9.2345, 10.2342345, 13.2599];
|
||||||
|
let (odd_mean, odd_median, odd_min, odd_max) = time::Profiler::get_statistics(&odd_data);
|
||||||
|
|
||||||
|
assert_eq!(6.491453833333334, odd_mean);
|
||||||
|
assert_eq!(5.324, odd_median);
|
||||||
|
assert_eq!(1.234, odd_min);
|
||||||
|
assert_eq!(13.2599, odd_max);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(debug_assertions)]
|
||||||
|
#[test]
|
||||||
|
#[should_panic]
|
||||||
|
fn time_profiler_unsorted_stats_test() {
|
||||||
|
let unsorted_data = vec![5.0, 7.5, 1.0, 8.9];
|
||||||
|
time::Profiler::get_statistics(&unsorted_data);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(debug_assertions)]
|
||||||
|
#[test]
|
||||||
|
#[should_panic]
|
||||||
|
fn time_profiler_data_len_zero() {
|
||||||
|
let zero_data = vec![];
|
||||||
|
time::Profiler::get_statistics(&zero_data);
|
||||||
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue