Files
hyperfine/src/main.rs
T

76 lines
2.0 KiB
Rust
Raw Normal View History

2022-08-08 16:37:39 +02:00
#![cfg_attr(
all(windows, feature = "windows_process_extensions_main_thread_handle"),
feature(windows_process_extensions_main_thread_handle)
)]
2018-03-24 18:30:52 -04:00
use std::env;
use std::io::{self, Write};
2018-01-13 16:50:02 +01:00
2022-02-07 21:03:23 +01:00
use benchmark::scheduler::Scheduler;
2022-02-22 14:04:07 +01:00
use cli::get_cli_arguments;
2022-02-06 21:02:15 +01:00
use command::Commands;
use error::ConsoleOutputError;
2022-02-06 18:22:55 +01:00
use export::ExportManager;
2022-02-06 20:10:40 +01:00
use options::Options;
2018-01-14 10:48:47 +01:00
2022-02-06 20:10:40 +01:00
use anyhow::Result;
2022-02-06 19:49:31 +01:00
use colored::*;
pub mod benchmark;
2022-02-22 14:04:07 +01:00
pub mod cli;
2022-02-06 19:49:31 +01:00
pub mod command;
pub mod error;
pub mod export;
pub mod options;
pub mod outlier_detection;
pub mod output;
pub mod parameter;
2026-10-05 11:46:49 +02:00
pub mod quantity;
2022-02-06 19:49:31 +01:00
pub mod timer;
2022-02-06 20:01:41 +01:00
pub mod util;
2018-01-14 11:41:27 +13:00
2022-02-06 17:44:28 +01:00
fn run() -> Result<()> {
// Enabled ANSI colors on Windows 10
#[cfg(windows)]
colored::control::set_virtual_terminal(true).unwrap();
2022-02-06 21:02:15 +01:00
let cli_arguments = get_cli_arguments(env::args_os());
2024-11-11 22:18:19 +01:00
let mut options = Options::from_cli_arguments(&cli_arguments)?;
2022-02-06 21:02:15 +01:00
let commands = Commands::from_cli_arguments(&cli_arguments)?;
options.validate_against_command_list(&commands)?;
2024-12-29 13:57:18 +01:00
let export_manager = ExportManager::from_cli_arguments(
&cli_arguments,
options.time_unit,
options.sort_order_exports,
)?;
2018-03-24 18:30:52 -04:00
2022-02-07 21:03:23 +01:00
let mut scheduler = Scheduler::new(&commands, &options, &export_manager);
scheduler.run_benchmarks()?;
scheduler.print_relative_speed_comparison()?;
scheduler.final_export()?;
2022-02-07 21:03:23 +01:00
Ok(())
2022-02-06 17:44:28 +01:00
}
2018-03-24 18:30:52 -04:00
fn caused_by_broken_console_pipe(error: &anyhow::Error) -> bool {
error
.downcast_ref::<ConsoleOutputError>()
.is_some_and(|e| e.0.kind() == io::ErrorKind::BrokenPipe)
}
2022-02-06 17:44:28 +01:00
fn main() {
match run() {
2020-10-16 10:47:26 +02:00
Ok(_) => {}
2022-02-06 17:44:28 +01:00
Err(e) => {
// Exit quietly when a reader stops consuming console output early.
if caused_by_broken_console_pipe(&e) {
std::process::exit(0);
}
// The write to stderr can itself fail if stderr is closed; the
// error message cannot be shown in that case anyway.
let _ = writeln!(io::stderr(), "{} {:#}", "Error:".red(), e);
2022-02-06 17:44:28 +01:00
std::process::exit(1);
}
2018-03-24 18:30:52 -04:00
}
}