### Setup and Cleanup Commands Source: https://context7.com/sharkdp/hyperfine/llms.txt Defines commands to run once before all benchmark runs (setup) and once after all runs (cleanup) for a specific command. This is useful for tasks like installing dependencies or removing temporary files, ensuring a clean environment. ```bash # Setup runs once before all timing runs, cleanup runs once after hyperfine --setup 'npm install' --cleanup 'rm -rf node_modules' \ 'npm test' # Multiple commands with different setup/cleanup hyperfine --setup 'cargo build --release' \ './target/release/app --mode fast' \ './target/release/app --mode slow' # Setup and cleanup are NOT timed ``` -------------------------------- ### Install Hyperfine on OpenBSD Source: https://github.com/sharkdp/hyperfine/blob/master/README.md Instructions for installing hyperfine on OpenBSD systems using the `pkg_add` command. ```sh doas pkg_add hyperfine ``` -------------------------------- ### Install Hyperfine on Void Linux Source: https://github.com/sharkdp/hyperfine/blob/master/README.md Instructions for installing hyperfine on Void Linux using the `xbps-install` command. ```sh xbps-install -S hyperfine ``` -------------------------------- ### Install Hyperfine on openSUSE Source: https://github.com/sharkdp/hyperfine/blob/master/README.md Instructions for installing hyperfine on openSUSE systems using the `zypper` package manager. ```sh zypper install hyperfine ``` -------------------------------- ### Install Hyperfine on NixOS Source: https://github.com/sharkdp/hyperfine/blob/master/README.md Instructions for installing hyperfine on NixOS using the `nix-env` command. ```sh nix-env -i hyperfine ``` -------------------------------- ### Install Hyperfine on Funtoo Linux Source: https://github.com/sharkdp/hyperfine/blob/master/README.md Instructions for installing hyperfine on Funtoo Linux using the `emerge` command with the core-kit. ```sh emerge app-benchmarks/hyperfine ``` -------------------------------- ### Install Hyperfine with Cargo Source: https://github.com/sharkdp/hyperfine/blob/master/README.md Installs the Hyperfine benchmarking tool from source using the cargo build tool. This method requires Rust 1.76 or newer to be installed on the system. It's suitable for users who develop with Rust or prefer installing from source. ```bash cargo install --locked hyperfine ``` -------------------------------- ### Install Hyperfine on Windows Source: https://github.com/sharkdp/hyperfine/blob/master/README.md Details how to install hyperfine on Windows using package managers Chocolatey, Scoop, or Winget. ```powershell choco install hyperfine # Or using Scoop: scoop install hyperfine # Or using Winget: winget install hyperfine ``` -------------------------------- ### Install Hyperfine on FreeBSD Source: https://github.com/sharkdp/hyperfine/blob/master/README.md Instructions for installing hyperfine on FreeBSD systems using the `pkg` package manager. ```sh pkg install hyperfine ``` -------------------------------- ### Install Hyperfine on Ubuntu Source: https://github.com/sharkdp/hyperfine/blob/master/README.md Provides instructions for installing hyperfine on Ubuntu systems using both the default repositories via `apt` and by downloading a `.deb` package for the latest version. ```bash apt install hyperfine # Or for the latest version: wget https://github.com/sharkdp/hyperfine/releases/download/v1.20.0/hyperfine_1.20.0_amd64.deb sudo dpkg -i hyperfine_1.20.0_amd64.deb ``` -------------------------------- ### Install Hyperfine on Flox Source: https://github.com/sharkdp/hyperfine/blob/master/README.md Instructions for installing hyperfine on Flox, noting that its version aligns with Nix. ```sh flox install hyperfine ``` -------------------------------- ### Install Hyperfine on Fedora Source: https://github.com/sharkdp/hyperfine/blob/master/README.md Instructions for installing hyperfine on Fedora systems using the `dnf` package manager. ```sh dnf install hyperfine ``` -------------------------------- ### Install Hyperfine on Alpine Linux Source: https://github.com/sharkdp/hyperfine/blob/master/README.md Instructions for installing hyperfine on Alpine Linux systems using the `apk` package manager. ```sh apk add hyperfine ``` -------------------------------- ### Install Hyperfine on macOS Source: https://github.com/sharkdp/hyperfine/blob/master/README.md Provides instructions for installing hyperfine on macOS using either Homebrew or MacPorts. ```shell brew install hyperfine # Or using MacPorts: sudo port selfupdate sudo port install hyperfine ``` -------------------------------- ### Install Hyperfine on Arch Linux Source: https://github.com/sharkdp/hyperfine/blob/master/README.md Instructions for installing hyperfine on Arch Linux systems using the `pacman` package manager. ```sh pacman -S hyperfine ``` -------------------------------- ### Benchmark Shell Functions and Aliases with Hyperfine (Shell) Source: https://github.com/sharkdp/hyperfine/blob/master/README.md Provides examples of benchmarking shell functions and aliases by inlining them directly into the hyperfine command or by sourcing them from a script file. This method is useful when direct export is not feasible. ```sh hyperfine 'my_function() { sleep 1; }; my_function' echo 'alias my_alias="sleep 1"' > /tmp/my_alias.sh hyperfine '. /tmp/my_alias.sh; my_alias' ``` -------------------------------- ### Install Hyperfine on Debian Linux Source: https://github.com/sharkdp/hyperfine/blob/master/README.md Instructions for installing hyperfine on Debian Linux systems using the `apt` package manager. ```bash apt install hyperfine ``` -------------------------------- ### Control hyperfine Output Styling and Color Source: https://context7.com/sharkdp/hyperfine/llms.txt This example covers controlling the output style and color of hyperfine. Options range from disabling all output for CI/CD environments to forcing color output even when not connected to a TTY. It highlights the use of `--style none` for automated pipelines. ```bash # Disable all output (for CI/CD) hyperfine --style none 'command' # Basic output without progress bar hyperfine --style basic 'command' # Full output with progress bar (default) hyperfine --style full 'command' # Disable colors hyperfine --style nocolor 'command' # Force colors even when not a TTY hyperfine --style color 'command' # Useful in CI pipelines: hyperfine --style none --export-json results.json 'npm test' ``` -------------------------------- ### Install Hyperfine on Exherbo Linux Source: https://github.com/sharkdp/hyperfine/blob/master/README.md Instructions for installing hyperfine on Exherbo Linux using `cave` commands, resolving from the rust repository. ```sh cave resolve -x repository/rust cave resolve -x hyperfine ``` -------------------------------- ### Install Hyperfine with Conda Source: https://github.com/sharkdp/hyperfine/blob/master/README.md Installs the Hyperfine benchmarking tool using the conda package manager from the conda-forge channel. This is a convenient method for users already managing their environments with conda. ```bash conda install -c conda-forge hyperfine ``` -------------------------------- ### Export Benchmark Results to AsciiDoc with hyperfine Source: https://context7.com/sharkdp/hyperfine/llms.txt This example illustrates how to export benchmark results into AsciiDoc table format, suitable for integration into technical documentation. The output is structured for direct inclusion in AsciiDoc documents. ```bash # Export to AsciiDoc file hyperfine --export-asciidoc benchmark.adoc 'cmd1' 'cmd2' ``` -------------------------------- ### Preparation Commands for Cold Cache Source: https://context7.com/sharkdp/hyperfine/llms.txt Executes a preparation command before each timing run to ensure a consistent starting state, such as clearing filesystem caches. The preparation command itself is not timed, ensuring only the target command's performance is measured. ```bash # Clear filesystem cache before each run (requires root) hyperfine --prepare 'sync; echo 3 | sudo tee /proc/sys/vm/drop_caches' \ 'grep -R TODO /usr/src' # Rebuild from clean state hyperfine --prepare 'make clean' 'make -j4' # Reset database state hyperfine --prepare 'psql -c "DROP DATABASE test; CREATE DATABASE test;"' \ 'psql test < import.sql' # The prepare command is NOT timed, only the main command ``` -------------------------------- ### Export Benchmark Results to Markdown with hyperfine Source: https://context7.com/sharkdp/hyperfine/llms.txt This example shows how to generate Markdown tables from benchmark results, which are ideal for documentation and reports. It covers exporting to a file and combining with custom names for commands to improve clarity in the generated table. ```bash # Export to markdown file hyperfine --export-markdown comparison.md 'cmd1' 'cmd2' 'cmd3' # Combine with custom names hyperfine -n 'Fast' 'cmd1' -n 'Slow' 'cmd2' --export-markdown results.md ``` -------------------------------- ### Install Dependencies via Pip (Bash) Source: https://github.com/sharkdp/hyperfine/blob/master/scripts/README.md This command installs the necessary Python packages (numpy, matplotlib, scipy) using pip. These packages are required for the plotting scripts to function correctly. Ensure you use 'pip3' if you are using Python 3. ```bash pip install numpy matplotlib scipy # pip3, if you are using python3 ``` -------------------------------- ### Export Benchmark Results to CSV with hyperfine Source: https://context7.com/sharkdp/hyperfine/llms.txt This snippet demonstrates exporting benchmark results to CSV format, which is easily importable into spreadsheet software for further analysis. It includes examples for basic export and when using parameter scanning. ```bash # Export to CSV file hyperfine --export-csv results.csv 'command1' 'command2' # With parameters hyperfine -L size 100,1000,10000 \ --export-csv scaling.csv \ 'process-data --size {size}' ``` -------------------------------- ### Run Script with PEP-723 Requirements (Bash) Source: https://github.com/sharkdp/hyperfine/blob/master/scripts/README.md This command executes a Python script using 'uv run', which handles inline script requirements defined by PEP-723. This is an alternative to manually installing dependencies if your package manager supports it. ```bash uv run plot_whisker.py sleep.json ``` -------------------------------- ### Benchmark Execution with Rust API Source: https://context7.com/sharkdp/hyperfine/llms.txt Demonstrates how to use the hyperfine Rust API to programmatically set up and run a single benchmark. It covers creating commands, configuring benchmark options, using an executor, and accessing benchmark results like mean, standard deviation, min, and max times. Dependencies include hyperfine, std::error, and hyperfine::util::min_max. ```rust use hyperfine::benchmark::{Benchmark, BenchmarkResult}; use hyperfine::command::Command; use hyperfine::executor::ShellExecutor; use hyperfine::options::Options; fn main() -> Result<(), Box> { // Create command to benchmark let command = Command::new(Some("test"), "sleep 0.1"); // Configure options let options = Options { warmup_count: 3, run_bounds: hyperfine::util::min_max::MinMax::new(10, 100), output_style: hyperfine::options::OutputStyleOption::Full, ..Default::default() }; // Create executor let mut executor = ShellExecutor::default(); executor.calibrate()?; // Create and run benchmark let benchmark = Benchmark::new(1, &command, &options, &executor); let result: BenchmarkResult = benchmark.run()?; // Access results println!("Mean: {:.3}s", result.mean); println!("StdDev: {:.3}s", result.stddev.unwrap_or(0.0)); println!("Min: {:.3}s, Max: {:.3}s", result.min, result.max); Ok(()) } ``` -------------------------------- ### Parameterized Commands with Rust API Source: https://context7.com/sharkdp/hyperfine/llms.txt Illustrates how to create commands with parameters using the hyperfine Rust API for systematic benchmarking. This allows for benchmarking variations of a command by substituting placeholder values. It utilizes hyperfine::command::{Command, ParameterNameAndValue} and hyperfine::parameter::ParameterValue. ```rust use hyperfine::command::{Command, ParameterNameAndValue}; use hyperfine::parameter::ParameterValue; // Create command with parameters let params = vec![ ("threads", ParameterValue::Numeric(4.0)), ("input", ParameterValue::Text("data.txt".to_string())), ]; let command = Command::new_parametrized( Some("parallel-processor"), "process --threads {threads} --input {input}", params ); // Get expanded command line println!("Command: {}", command.get_command_line()); // Output: process --threads 4 --input data.txt // Access parameters for (name, value) in command.get_parameters() { println!("Parameter {}: {:?}", name, value); } ``` -------------------------------- ### Rust API: Command with Parameters Source: https://context7.com/sharkdp/hyperfine/llms.txt Shows how to create parameterized commands for systematic benchmarking, allowing you to benchmark a command with different sets of arguments. ```APIDOC ## Rust API: Command with Parameters ### Description Create parameterized commands for systematic benchmarking. ### Method `Command::new_parametrized(...)` ### Endpoint N/A (Rust Library Function) ### Parameters N/A ### Request Example ```rust use hyperfine::command::{Command, ParameterNameAndValue}; use hyperfine::parameter::ParameterValue; // Create command with parameters let params = vec![ ("threads", ParameterValue::Numeric(4.0)), ("input", ParameterValue::Text("data.txt".to_string())), ]; let command = Command::new_parametrized( Some("parallel-processor"), "process --threads {threads} --input {input}", params ); // Get expanded command line println!("Command: {}", command.get_command_line()); // Output: process --threads 4 --input data.txt // Access parameters for (name, value) in command.get_parameters() { println!("Parameter {}: {:?}", name, value); } ``` ### Response #### Success Response (200) N/A (Prints to console) #### Response Example ``` Command: process --threads 4 --input data.txt Parameter threads: Numeric(4.0) Parameter input: Text("data.txt") ``` ``` -------------------------------- ### Scheduling Multiple Benchmarks with Rust API Source: https://context7.com/sharkdp/hyperfine/llms.txt Shows how to use the Scheduler from the hyperfine Rust API to run and compare multiple benchmarks programmatically. This involves creating commands, configuring options, setting up an export manager, and then running the scheduler. Key components include hyperfine::benchmark::scheduler::Scheduler and hyperfine::export::ExportManager. ```rust use hyperfine::benchmark::scheduler::Scheduler; use hyperfine::command::Commands; use hyperfine::export::ExportManager; use hyperfine::options::Options; fn benchmark_multiple() -> Result<(), Box> { // Create commands let commands = Commands::from_cli_arguments(&matches)?; // Configure options let options = Options::default(); // Setup export let mut export_manager = ExportManager::new(None, Default::default()); export_manager.add_exporter( hyperfine::export::ExportType::Json, "results.json" )?; // Create scheduler and run let mut scheduler = Scheduler::new(&commands, &options, &export_manager); scheduler.run_benchmarks()?; // Print comparison scheduler.print_relative_speed_comparison(); // Export results scheduler.final_export()?; Ok(()) } ``` -------------------------------- ### Export Benchmark Results to Markdown with Hyperfine Source: https://github.com/sharkdp/hyperfine/blob/master/README.md Illustrates how to export benchmark results into a Markdown table format using the `--export-markdown` option. This is useful for embedding benchmark summaries into documentation. ```shell hyperfine --export-markdown results.md 'command1' # Example output table structure: # | Command | Mean [s] | Min [s] | Max [s] | Relative | # |:---|---:|---:|---:|---:| # | `command1` | 1.234 ± 0.05 | 1.100 | 1.400 | 1.00 | ``` -------------------------------- ### Library Usage (Rust API) Source: https://context7.com/sharkdp/hyperfine/llms.txt Demonstrates the basic usage of the hyperfine Rust library to perform programmatic benchmarking. It covers creating commands, configuring options, executing benchmarks, and accessing results. ```APIDOC ## Library Usage (Rust API) ### Description Use hyperfine as a Rust library for programmatic benchmarking. ### Method `fn main() -> Result<(), Box>` ### Endpoint N/A (Rust Library Function) ### Parameters N/A ### Request Example ```rust use hyperfine::benchmark::{Benchmark, BenchmarkResult}; use hyperfine::command::Command; use hyperfine::executor::ShellExecutor; use hyperfine::options::Options; fn main() -> Result<(), Box> { // Create command to benchmark let command = Command::new(Some("test"), "sleep 0.1"); // Configure options let options = Options { warmup_count: 3, run_bounds: hyperfine::util::min_max::MinMax::new(10, 100), output_style: hyperfine::options::OutputStyleOption::Full, ..Default::default() }; // Create executor let mut executor = ShellExecutor::default(); executor.calibrate()?; // Create and run benchmark let benchmark = Benchmark::new(1, &command, &options, &executor); let result: BenchmarkResult = benchmark.run()?; // Access results println!("Mean: {:.3}s", result.mean); println!("StdDev: {:.3}s", result.stddev.unwrap_or(0.0)); println!("Min: {:.3}s, Max: {:.3}s", result.min, result.max); Ok(()) } ``` ### Response #### Success Response (200) N/A (Prints to console) #### Response Example ``` Mean: 0.105s StdDev: 0.002s Min: 0.103s, Max: 0.108s ``` ``` -------------------------------- ### Rust API: Scheduler for Multiple Benchmarks Source: https://context7.com/sharkdp/hyperfine/llms.txt This section details how to use the `Scheduler` to run and compare multiple benchmarks programmatically, including setting up export options. ```APIDOC ## Rust API: Scheduler for Multiple Benchmarks ### Description Run and compare multiple benchmarks programmatically. ### Method `Scheduler::new(...)` and `Scheduler::run_benchmarks()` ### Endpoint N/A (Rust Library Function) ### Parameters N/A ### Request Example ```rust use hyperfine::benchmark::scheduler::Scheduler; use hyperfine::command::Commands; use hyperfine::export::ExportManager; use hyperfine::options::Options; fn benchmark_multiple() -> Result<(), Box> { // Create commands let commands = Commands::from_cli_arguments(&matches)?; // Assuming 'matches' is defined elsewhere // Configure options let options = Options::default(); // Setup export let mut export_manager = ExportManager::new(None, Default::default()); export_manager.add_exporter( hyperfine::export::ExportType::Json, "results.json" )?; // Create scheduler and run let mut scheduler = Scheduler::new(&commands, &options, &export_manager); scheduler.run_benchmarks()?; // Print comparison scheduler.print_relative_speed_comparison(); // Export results scheduler.final_export()?; Ok(()) } ``` ### Response #### Success Response (200) N/A (Prints comparison to console and exports results) #### Response Example ``` # Relative speed comparison # Benchmark 'command1' is X times faster than 'command2' ``` ``` -------------------------------- ### Warmup Runs for Stabilization Source: https://context7.com/sharkdp/hyperfine/llms.txt Performs warmup iterations before actual measurements to stabilize system state, such as caches. This leads to more consistent and reliable benchmark results, especially for I/O-bound tasks. The number of warmup runs can be specified. ```bash # Run command 3 times before starting measurements hyperfine --warmup 3 'grep -R TODO src/' # Useful for disk cache effects hyperfine --warmup 5 'find . -name "*.rs"' # Output indicates warmup was performed: # Benchmark 1: grep -R TODO src/ # Time (mean ± σ): 142.3 ms ± 3.1 ms [User: 98.2 ms, System: 43.5 ms] # Range (min … max): 137.8 ms … 148.9 ms 20 runs # # Warning: Statistical outliers were detected. Consider re-running with --warmup. ``` -------------------------------- ### Benchmark Shell Functions with Hyperfine (Bash) Source: https://github.com/sharkdp/hyperfine/blob/master/README.md Demonstrates how to benchmark shell functions directly using hyperfine in a bash environment. This involves exporting the function using `export -f` and then calling it with hyperfine specifying the bash shell. ```bash my_function() { sleep 1; } export -f my_function hyperfine --shell=bash my_function ``` -------------------------------- ### Rust API: Export Results Programmatically Source: https://context7.com/sharkdp/hyperfine/llms.txt Learn how to export benchmark results in various formats (JSON, Markdown, CSV) using the library's API. ```APIDOC ## Rust API: Export Results Programmatically ### Description Export benchmark results in various formats using the library API. ### Method `ExportManager::new(...)`, `add_exporter(...)`, `write_results(...)` ### Endpoint N/A (Rust Library Function) ### Parameters N/A ### Request Example ```rust use hyperfine::benchmark::benchmark_result::BenchmarkResult; use hyperfine::export::{ExportManager, ExportType}; fn export_results(results: &[BenchmarkResult]) -> Result<(), Box> { let mut manager = ExportManager::new( Some(hyperfine::util::units::Unit::MilliSecond), Default::default() ); // Add multiple export targets manager.add_exporter(ExportType::Json, "results.json")?; manager.add_exporter(ExportType::Markdown, "results.md")?; manager.add_exporter(ExportType::Csv, "results.csv")?; // Write results to all targets manager.write_results(results, false)?; println!("Results exported to JSON, Markdown, and CSV"); Ok(()) } ``` ### Response #### Success Response (200) N/A (Exports files and prints a confirmation message) #### Response Example ``` Results exported to JSON, Markdown, and CSV ``` ``` -------------------------------- ### Input from File for Stdin Source: https://context7.com/sharkdp/hyperfine/llms.txt Feed the contents of a file as standard input (stdin) to benchmarked commands using the `--input` flag. This is ideal for testing commands that process data streams or require input from stdin. ```bash # Provide input data from file hyperfine --input data.txt 'grep "pattern"' # Useful for stdin-based tools hyperfine --input large-data.json 'jq ".results[]"' # Multiple commands with same input hyperfine --input input.txt 'wc -l' 'grep -c "^"' ``` -------------------------------- ### Designate and Name Reference Commands in hyperfine Source: https://context7.com/sharkdp/hyperfine/llms.txt This snippet demonstrates how to set a baseline command for comparison and assign a clear name to it for better readability in the summary output. It's useful for understanding performance improvements relative to a known good or default state. ```bash hyperfine --reference 'baseline-cmd' 'optimized-v1' 'optimized-v2' hyperfine --reference 'gcc -O0 main.c && ./a.out' \ --reference-name 'No Optimization' \ 'gcc -O2 main.c && ./a.out' \ 'gcc -O3 main.c && ./a.out' ``` -------------------------------- ### Cartesian Product for Multiple Parameters Source: https://context7.com/sharkdp/hyperfine/llms.txt Combine multiple parameter lists to test all possible combinations. This feature is powerful for exploring the performance impact of varying multiple independent parameters simultaneously. The output will show benchmarks for each unique combination. ```bash # Test all combinations of language and optimization level hyperfine -L lang cpp,rust -L opt 0,2,3 \ --setup 'build-{lang}-O{opt}.sh' \ './{lang}-benchmark-O{opt}' ``` -------------------------------- ### Minimum Benchmarking Time Source: https://context7.com/sharkdp/hyperfine/llms.txt Set a minimum total time to spend benchmarking each command with the `--min-benchmarking-time` flag. This ensures that even very fast commands are run enough times to achieve statistical significance, providing more reliable results. ```bash # Benchmark for at least 10 seconds hyperfine --min-benchmarking-time 10 'quick-command' ``` -------------------------------- ### Parameterized Benchmark with Decimal Scan Source: https://context7.com/sharkdp/hyperfine/llms.txt Supports benchmarking with floating-point parameter values. You can specify a range and a step size for decimal values, allowing for fine-grained testing of performance with continuous parameters. The step size can be customized. ```bash # Scan delay from 0.3 to 0.7 with step 0.2 hyperfine --parameter-scan delay 0.3 0.7 -D 0.2 \ 'sleep {delay}' # Tests: sleep 0.3, sleep 0.5, sleep 0.7 ``` -------------------------------- ### Export Benchmark Results to JSON with hyperfine Source: https://context7.com/sharkdp/hyperfine/llms.txt Learn how to export detailed benchmark results to a JSON file for programmatic analysis. The JSON output includes comprehensive timing data such as mean, standard deviation, user time, system time, and individual execution times. It also shows how to export to stdout and combine with parameter scanning. ```bash # Export to JSON file hyperfine --export-json results.json 'command1' 'command2' # Export to stdout hyperfine --export-json - 'command' # Combine with parameters hyperfine --parameter-scan threads 1 8 \ --export-json scaling.json \ 'make -j {threads}' ``` -------------------------------- ### Export Benchmark Results to JSON with Hyperfine Source: https://github.com/sharkdp/hyperfine/blob/master/README.md Shows how to export detailed benchmark results in JSON format using hyperfine. The JSON output is suitable for programmatic analysis and visualization, often used with accompanying scripts. ```shell hyperfine --export-json results.json 'command1' # The output will be a JSON object containing detailed benchmark data. ``` -------------------------------- ### Exporting Benchmark Results Programmatically with Rust API Source: https://context7.com/sharkdp/hyperfine/llms.txt Explains how to programmatically export benchmark results in various formats (JSON, Markdown, CSV) using the hyperfine Rust library API. It involves creating an ExportManager, adding desired exporters with file paths, and then writing the results. Key components include hyperfine::benchmark::benchmark_result::BenchmarkResult and hyperfine::export::{ExportManager, ExportType}. ```rust use hyperfine::benchmark::benchmark_result::BenchmarkResult; use hyperfine::export::{ExportManager, ExportType}; fn export_results(results: &[BenchmarkResult]) -> Result<(), Box> { let mut manager = ExportManager::new( Some(hyperfine::util::units::Unit::MilliSecond), Default::default() ); // Add multiple export targets manager.add_exporter(ExportType::Json, "results.json")?; manager.add_exporter(ExportType::Markdown, "results.md")?; manager.add_exporter(ExportType::Csv, "results.csv")?; // Write results to all targets manager.write_results(results, false)?; println!("Results exported to JSON, Markdown, and CSV"); Ok(()) } ``` -------------------------------- ### Parameterized Benchmark with Integer Scan Source: https://context7.com/sharkdp/hyperfine/llms.txt Enables benchmarking with varying integer parameter values. Hyperfine scans through a specified range of integers, allowing you to test how your command scales with different inputs. The parameter values are substituted into the command string. ```bash # Scan thread count from 1 to 12 hyperfine --parameter-scan num_threads 1 12 \ 'make -j {num_threads}' # Output shows all parameter values tested: # Benchmark 1: make -j 1 # Time (mean ± σ): 45.234 s ± 0.423 s # Benchmark 2: make -j 2 # Time (mean ± σ): 23.891 s ± 0.312 s # ... # Benchmark 12: make -j 12 # Time (mean ± σ): 5.123 s ± 0.089 s ``` -------------------------------- ### Shell Selection for Command Execution Source: https://context7.com/sharkdp/hyperfine/llms.txt Choose the specific shell interpreter for executing commands using the `--shell` flag, or opt for raw execution without a shell using `-N`. Raw execution can offer a performance boost for simple commands by bypassing shell overhead. ```bash # Use specific shell hyperfine --shell zsh 'for i in {1..1000}; do echo test; done' # Use bash explicitly hyperfine --shell bash 'echo ${BASH_VERSION}' # No shell (raw execution) - faster for simple commands hyperfine -N '/usr/bin/grep TODO /home/user/file.txt' # Custom shell with arguments hyperfine --shell 'bash -x' 'my-script.sh' ``` -------------------------------- ### Rust API: Custom Executor Implementation Source: https://context7.com/sharkdp/hyperfine/llms.txt Explains how to implement a custom execution strategy by creating a struct that implements the `Executor` trait. ```APIDOC ## Rust API: Custom Executor Implementation ### Description Implement custom execution strategy with the Executor trait. ### Method Implement the `Executor` trait. ### Endpoint N/A (Rust Library Trait Implementation) ### Parameters N/A ### Request Example ```rust use hyperfine::benchmark::executor::{Executor, TimingResult}; use hyperfine::command::Command; use hyperfine::benchmark::relative_speed::Second; use std::process::ExitStatus; struct CustomExecutor { overhead: Second, } impl Executor for CustomExecutor { fn run_command_and_measure( &self, command: &Command<'_>, iteration: usize, command_failure_action: Option, output_policy: &CommandOutputPolicy, ) -> Result<(TimingResult, ExitStatus)> { // Custom timing implementation let start = std::time::Instant::now(); let mut cmd = command.get_command()?; let result = cmd.status()?; let elapsed = start.elapsed().as_secs_f64(); let timing = TimingResult { time_real: elapsed, time_user: 0.0, time_system: 0.0, }; Ok((timing, result)) } fn calibrate(&mut self) -> Result<()> { // Measure executor overhead self.overhead = 0.001; // 1ms overhead Ok(()) } fn time_overhead(&self) -> Second { self.overhead } } ``` ### Response #### Success Response (200) N/A (This is an implementation guide) #### Response Example N/A ``` -------------------------------- ### Compare Multiple Commands Source: https://context7.com/sharkdp/hyperfine/llms.txt Benchmarks and compares the performance of multiple shell commands side-by-side. This feature helps in identifying the most efficient command for a given task. The output provides a summary table showing relative performance differences. ```bash hyperfine 'hexdump file.bin' 'xxd file.bin' 'od -t x1 file.bin' # Output includes comparison table: # Summary # 'hexdump file.bin' ran # 1.47 ± 0.12 times faster than 'xxd file.bin' # 2.23 ± 0.18 times faster than 'od -t x1 file.bin' ``` -------------------------------- ### Showing Command Output Source: https://context7.com/sharkdp/hyperfine/llms.txt Display the standard output (stdout) and standard error (stderr) from benchmarked commands using the `--show-output` flag. This is helpful for debugging or verifying the behavior of the commands being benchmarked, although it may impact performance measurements. ```bash # Show output after each run hyperfine --show-output 'echo "Processing..."; process-data' ``` -------------------------------- ### Custom Executor Implementation with Rust API Source: https://context7.com/sharkdp/hyperfine/llms.txt Demonstrates how to implement a custom execution strategy by implementing the Executor trait in the hyperfine Rust API. This allows for custom timing and execution logic. The implementation involves defining a struct and implementing methods like `run_command_and_measure`, `calibrate`, and `time_overhead`. Dependencies include hyperfine::benchmark::executor::{Executor, TimingResult} and std::process::ExitStatus. ```rust use hyperfine::benchmark::executor::{Executor, TimingResult}; use hyperfine::command::Command; use hyperfine::benchmark::relative_speed::Second; use std::process::ExitStatus; struct CustomExecutor { overhead: Second, } impl Executor for CustomExecutor { fn run_command_and_measure( &self, command: &Command<'_>, iteration: usize, command_failure_action: Option, output_policy: &CommandOutputPolicy, ) -> Result<(TimingResult, ExitStatus)> { // Custom timing implementation let start = std::time::Instant::now(); let mut cmd = command.get_command()?; let result = cmd.status()?; let elapsed = start.elapsed().as_secs_f64(); let timing = TimingResult { time_real: elapsed, time_user: 0.0, time_system: 0.0, }; Ok((timing, result)) } fn calibrate(&mut self) -> Result<()> { // Measure executor overhead self.overhead = 0.001; // 1ms overhead Ok(()) } fn time_overhead(&self) -> Second { self.overhead } } ``` -------------------------------- ### Export Benchmark Results to Org-mode with hyperfine Source: https://context7.com/sharkdp/hyperfine/llms.txt This snippet shows how to generate benchmark results in Org-mode table format, which is useful for users of Emacs for documentation and note-taking. The output is structured for easy integration into Org files. ```bash # Export to Org-mode file hyperfine --export-orgmode benchmark.org 'cmd1' 'cmd2' ``` -------------------------------- ### Custom Command Names for Clarity Source: https://context7.com/sharkdp/hyperfine/llms.txt Assign custom, human-readable names to benchmarked commands using the `-n` or `--command-name` flags. This improves the clarity of the summary output, making it easier to understand which command corresponds to which performance result. ```bash # Use -n or --command-name for clarity hyperfine -n 'Python sort' 'python sort.py' \ -n 'Rust sort' './rust-sort' \ -n 'C++ sort' './cpp-sort' ``` -------------------------------- ### Parameter Lists for Testing Variations Source: https://context7.com/sharkdp/hyperfine/llms.txt Test commands with explicit lists of parameter values. This is useful for iterating through different settings like compilers, optimization levels, or algorithms to find the best performing option. The `-L` flag defines the parameter name and its possible values. ```bash # Test different compilers hyperfine -L compiler gcc,clang,icc \ '{compiler} -O2 -o program main.c && ./program' # Test different optimization levels hyperfine -L opt 0,1,2,3,s \ --setup 'gcc -O{opt} -o program-{opt} main.c' \ './program-{opt}' # Multiple string values hyperfine -L algorithm bubble,quick,merge,heap \ 'python sort_{algorithm}.py data.txt' ``` -------------------------------- ### Ignoring Command Failures Source: https://context7.com/sharkdp/hyperfine/llms.txt Continue benchmarking even when commands exit with non-zero status codes using the `--ignore-failure` flag. This is particularly useful for commands that might legitimately fail, such as search commands that don't find a match, or for testing flaky scripts. ```bash # Ignore all failures hyperfine --ignore-failure 'flaky-test.sh' # Ignore specific exit codes (1 and 2) hyperfine --ignore-failure=1,2 'command-that-may-fail' # Useful for benchmarking searches that may not find results hyperfine --ignore-failure 'grep PATTERN file.txt || true' ``` -------------------------------- ### Time Unit Selection for Readability Source: https://context7.com/sharkdp/hyperfine/llms.txt Display benchmark results in specific time units like milliseconds or seconds using the `--time-unit` flag for improved readability. By default, hyperfine auto-selects the most appropriate unit. ```bash # Force milliseconds hyperfine --time-unit millisecond 'quick-command' # Force seconds hyperfine --time-unit second 'long-command' ``` -------------------------------- ### Output Redirection Policies Source: https://context7.com/sharkdp/hyperfine/llms.txt Control where the stdout/stderr of benchmarked commands are sent during execution. Options include redirecting to `/dev/null` (default for quiet), inheriting to the terminal, saving to a file, or piping the output. ```bash # Redirect to /dev/null (default for quiet measurement) hyperfine --output null 'noisy-command' # Inherit to terminal (same as --show-output) hyperfine --output inherit 'command' # Save to file hyperfine --output benchmark.log 'command' # Use pipe (feeds through pipe, not to terminal) hyperfine --output pipe 'command' # Different policies per command hyperfine --output null 'fast-cmd' --output ./slow.log 'slow-cmd' ``` -------------------------------- ### Specify Benchmark Run Count Source: https://context7.com/sharkdp/hyperfine/llms.txt Allows precise control over the number of benchmark iterations. You can set an exact number of runs, or define minimum and maximum run counts. This is useful for consistent testing or when dealing with long-running commands. ```bash # Run exactly 50 times hyperfine --runs 50 'python script.py' # Set minimum and maximum runs hyperfine --min-runs 20 --max-runs 100 'cargo build' # Output shows statistics across all runs: # Benchmark 1: cargo build # Time (mean ± σ): 5.234 s ± 0.127 s [User: 42.1 s, System: 3.2 s] # Range (min … max): 5.089 s … 5.567 s 20 runs ``` -------------------------------- ### Run Hyperfine and Plot Results (Bash) Source: https://github.com/sharkdp/hyperfine/blob/master/scripts/README.md This snippet demonstrates how to run hyperfine with the --export-json option and then use a Python script to plot the results. It requires the 'plot_whisker.py' script and a JSON export file from hyperfine. ```bash hyperfine 'sleep 0.020' 'sleep 0.021' 'sleep 0.022' --export-json sleep.json ./plot_whisker.py sleep.json ``` -------------------------------- ### Conclude Commands After Each Run Source: https://context7.com/sharkdp/hyperfine/llms.txt Specifies a command to execute after each individual timing run. This is useful for cleanup operations between runs, such as killing a server or removing temporary files generated by the benchmarked command. The conclude command is not timed. ```bash # Kill server after each benchmark run hyperfine --prepare 'start-server.sh' \ --conclude 'killall server' \ 'curl http://localhost:8080/api/test' # Clean temporary files after each run hyperfine --conclude 'rm -rf /tmp/test-*' \ 'app --output /tmp/test-{#}.dat' # Conclude command is NOT timed ``` -------------------------------- ### Basic Command Benchmark Source: https://context7.com/sharkdp/hyperfine/llms.txt Benchmarks a single shell command. Hyperfine automatically determines the number of runs based on a minimum of 10 runs or 3 seconds of execution time. The output includes the mean execution time, standard deviation, and the number of runs performed. ```bash # Simple benchmark - runs at least 10 times or for 3 seconds minimum hyperfine 'sleep 0.3' # Output: # Benchmark 1: sleep 0.3 # Time (mean ± σ): 303.1 ms ± 2.4 ms [User: 2.1 ms, System: 1.8 ms] # Range (min … max): 299.7 ms … 307.2 ms 10 runs ``` -------------------------------- ### Control hyperfine Sort Order Source: https://context7.com/sharkdp/hyperfine/llms.txt This snippet demonstrates how to control the sort order of benchmark results displayed by hyperfine. Users can sort by mean execution time (default) or alphabetically by command name. This affects the summary comparison output. ```bash # Sort by mean execution time (default) hyperfine --sort command 'slow' 'medium' 'fast' # Sort by command name alphabetically hyperfine --sort mean-time 'cmd-c' 'cmd-a' 'cmd-b' ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.