Skip to content
Myr

main.rs

This file

Path

src/main.rs

102 lines · 3.4 KiB · Rust

Last changed by

Add --env for benchmark environment overridesDavid Peterbefore adoption6dddb02

History of this file

Take it

1#![cfg_attr(2    all(windows, feature = "windows_process_extensions_main_thread_handle"),3    feature(windows_process_extensions_main_thread_handle)4)]56use std::env;7use std::io::{self, Write};89use benchmark::scheduler::Scheduler;10use cli::get_cli_arguments;11use command::Commands;12use error::ConsoleOutputError;13use export::ExportManager;14use options::{ExecutorKind, Options};1516use anyhow::{bail, Result};17use colored::*;1819pub mod benchmark;20pub mod cli;21pub mod command;22pub mod error;23pub mod export;24pub mod metric;25pub mod options;26pub mod outlier_detection;27pub mod output;28pub mod parameter;29pub mod quantity;30pub mod shell_syntax;31pub mod timer;32pub mod util;3334fn run() -> Result<()> {35    // Enabled ANSI colors on Windows 1036    #[cfg(windows)]37    colored::control::set_virtual_terminal(true).unwrap();3839    let cli_arguments = get_cli_arguments(env::args_os());40    let mut options = Options::from_cli_arguments(&cli_arguments)?;41    let commands = Commands::from_cli_arguments(&cli_arguments)?;42    options.validate_against_command_list(&commands)?;4344    if matches!(options.executor_kind, ExecutorKind::Raw)45        && !cli_arguments.get_flag("no-shell")46        && cli_arguments.get_one::<String>("shell").is_none()47    {48        for command in commands.iter() {49            let command_line = command.get_command_line();50            if shell_syntax::starts_with_assignment(&command_line) {51                bail!(52                    "Cannot run '{command_line}' directly because it starts with a variable assignment.\n\53                     Use the --env option to set environment variables, for example:\n\n  \54                       hyperfine --env 'OMP_NUM_THREADS=8 CFLAGS=\"-O3 -march=native\"' 'my_command'\n\n\55                     Alternatively, use -S / --shell=default to run the command in a shell."56                );57            }58            if let Some(operator) = shell_syntax::first_unquoted_operator(&command_line) {59                bail!(60                    concat!(61                        "Command '{command_line}' contains unquoted shell syntax ('{operator}').\n",62                        "Explicitly choose how to execute it:\n",63                        "  -S / --shell=default  Interpret shell syntax.\n",64                        "  -N / --shell=none     Run directly, passing '{operator}' as a literal argument.",65                    ),66                    command_line = command_line,67                    operator = operator,68                );69            }70        }71    }7273    let export_manager = ExportManager::from_cli_arguments(&cli_arguments, &options.metrics)?;7475    let mut scheduler = Scheduler::new(&commands, &options, &export_manager);76    scheduler.run_benchmarks()?;77    scheduler.final_export()?;7879    Ok(())80}8182fn caused_by_broken_console_pipe(error: &anyhow::Error) -> bool {83    error84        .downcast_ref::<ConsoleOutputError>()85        .is_some_and(|e| e.0.kind() == io::ErrorKind::BrokenPipe)86}8788fn main() {89    match run() {90        Ok(_) => {}91        Err(e) => {92            // Exit quietly when a reader stops consuming console output early.93            if caused_by_broken_console_pipe(&e) {94                std::process::exit(0);95            }96            // The write to stderr can itself fail if stderr is closed; the97            // error message cannot be shown in that case anyway.98            let _ = writeln!(io::stderr(), "{} {:#}", "Error:".red(), e);99            std::process::exit(1);100        }101    }102}