Merge pull request #581 from devonhollowood/elapsed
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Add elapsed time to progress bar
authored by David Peter · committed by GitHub · parents 79aea88, 8745487 · Browse files · 2 files·+63 added−10 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
2 files+63 added−10 removed
- other:+63 added−10 removed
Provenance
- Commit
5d501b6- Tree
e8ead69- Author
David Peter <sharkdp@users.noreply.github.com>- Committer
GitHub <noreply@github.com>- Parent 1
79aea88- Parent 2
8745487
2 files changed+63 added−10 removed
modified:src/benchmark/mod.rs+28 added-7 removed
4 unmodified lines5 pub mod scheduler;6 7 use std::cmp;8 use std::io::{self, Write};9+use std::time::Instant;10 11 use crate::benchmark::executor::BenchmarkIteration;12 use crate::command::Command;13 use crate::options::{14 CmdFailureAction, CommandOutputPolicy, ExecutorKind, Options, OutputStyleOption,15 };16 use crate::outlier_detection::OUTLIER_THRESHOLD;17 use crate::output::console_writeln;17-use crate::output::progress_bar::get_progress_bar;18+use crate::output::progress_bar::{19+ finish_initial_measurement, get_progress_bar, start_initial_measurement,20+};21 use crate::output::warnings::{OutlierWarningOptions, Warnings};22 use crate::parameter::ParameterNameAndValue;23 use crate::quantity::{const_time_from_seconds, ratio, FormatQuantity, Time, Zero};24 use benchmark_result::{BenchmarkResult, Parameter};221 unmodified lines246 // Set up progress bar (and spinner for initial measurement)247 let progress_bar = if self.options.output_style != OutputStyleOption::Disabled {248 Some(get_progress_bar(249 self.options.run_bounds.min,247- "Initial time measurement",250+ if preparation_command.is_some() {251+ "Running preparation command"252+ } else {253+ "Initial run"254+ },255 self.options.output_style,256 ))257 } else {258 None5 unmodified lines264 res.time_wall_clock + self.executor.time_overhead()265 });266 267 // Initial timing run268+ if let Some(bar) = progress_bar.as_ref() {269+ start_initial_measurement(bar, Instant::now());270+ }271 let res = self.executor.run_command_and_measure(272 self.command,273 benchmark_iteration,274 None,275 output_policy,276 )?;277 let success = res.exit_status.success();278 279+ if let Some(bar) = progress_bar.as_ref() {280+ let time_unit = self281+ .options282+ .time_unit283+ .unwrap_or(res.time_wall_clock.suitable_unit());284+ let estimate = res.time_wall_clock.format(time_unit);285+ finish_initial_measurement(286+ bar,287+ format!("Current estimate: {}", estimate.to_string().green()),288+ );289+ }290+291 let conclusion_result = run_conclusion_command(benchmark_iteration)?;292 let conclusion_overhead = conclusion_result.map_or(Time::zero(), |res| {293 res.time_wall_clock + self.executor.time_overhead()294 });25 unmodified lines320 all_succeeded = all_succeeded && success;321 322 // Re-configure the progress bar323 if let Some(bar) = progress_bar.as_ref() {302- bar.set_length(count)324+ bar.set_length(count);325+ bar.inc(1);326 }304- if let Some(bar) = progress_bar.as_ref() {305- bar.inc(1)306- }327 328 // Gather statistics (perform the actual benchmark)329 for i in 0..count_remaining {330 let benchmark_iteration = BenchmarkIteration::Benchmark(i + 1);311- run_preparation_command(benchmark_iteration)?;331 332 let msg = {333 let t_wall_clock_mean = measurements.time_wall_clock_mean();334 let time_unit = self7 unmodified lines342 if let Some(bar) = progress_bar.as_ref() {343 bar.set_message(msg.to_owned())344 }345 346+ run_preparation_command(benchmark_iteration)?;347+348 let res = self.executor.run_command_and_measure(349 self.command,350 benchmark_iteration,351 None,153 unmodified linesmodified:src/output/progress_bar.rs+35 added-3 removed
1-use indicatif::{ProgressBar, ProgressStyle};2-use std::time::Duration;1+use indicatif::{FormattedDuration, ProgressBar, ProgressState, ProgressStyle};2+use std::fmt;3+use std::time::{Duration, Instant};4 5 use crate::options::OutputStyleOption;6 7 #[cfg(not(windows))]1 unmodified line9 10 #[cfg(windows)]11 const TICK_SETTINGS: (&str, u64) = (r"+-x| ", 200);12 13+// Keep the message area 30 columns wide across preparation, the initial run,14+// and estimates so that switching between them does not move the bar.15+const DEFAULT_TEMPLATE: &str = " {spinner} {msg:<30} {wide_bar} ETA {eta_precise} ";16+const INITIAL_TEMPLATE: &str =17+ " {spinner} Initial run: {initial_elapsed:<17} {wide_bar} ETA {eta_precise} ";18+19+/// Show the time elapsed since the initial benchmark command started.20+pub fn start_initial_measurement(bar: &ProgressBar, started: Instant) {21+ let style = bar22+ .style()23+ .template(INITIAL_TEMPLATE)24+ .expect("no template error")25+ .with_key(26+ "initial_elapsed",27+ move |_: &ProgressState, w: &mut dyn fmt::Write| {28+ write!(w, "{}", FormattedDuration(started.elapsed())).unwrap();29+ },30+ );31+ bar.set_style(style);32+}33+34+/// Stop showing the initial timer and display the next phase's message.35+pub fn finish_initial_measurement(bar: &ProgressBar, message: String) {36+ bar.set_message(message);37+ bar.set_style(38+ bar.style()39+ .template(DEFAULT_TEMPLATE)40+ .expect("no template error"),41+ );42+}43+44 /// Return a pre-configured progress bar45 pub fn get_progress_bar(length: u64, msg: &str, option: OutputStyleOption) -> ProgressBar {46 let progressbar_style = match option {47 OutputStyleOption::Basic | OutputStyleOption::Color => ProgressStyle::default_bar(),48 _ => ProgressStyle::default_spinner()49 .tick_chars(TICK_SETTINGS.0)18- .template(" {spinner} {msg:<30} {wide_bar} ETA {eta_precise} ")50+ .template(DEFAULT_TEMPLATE)51 .expect("no template error"),52 };53 54 let progress_bar = match option {9 unmodified lines