mirror of
https://github.com/sharkdp/hyperfine.git
synced 2026-11-20 03:11:28 +00:00
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
f326201882 | ||
|
|
bda623eb75 | ||
|
|
d7dc7804b4 | ||
|
|
241b89f12b | ||
|
|
ef2bef588c | ||
|
|
35a513917d | ||
|
|
0a77b43d17 | ||
|
|
793b561d4c | ||
|
|
ae47586998 | ||
|
|
93386dc808 | ||
|
|
bd5d5fd4fa | ||
|
|
d42994bf64 | ||
|
|
f44f24c8d9 | ||
|
|
a656fbe508 | ||
|
|
d5e02ff768 | ||
|
|
b6ca2d6ac2 | ||
|
|
f29bbb58eb | ||
|
|
a4df90e44c | ||
|
|
b0bab45e51 | ||
|
|
1fe3c66430 | ||
|
|
933d167201 | ||
|
|
5d06e3979c |
No files matched your search
+95
@@ -0,0 +1,95 @@
|
||||
language: rust
|
||||
cache: cargo
|
||||
matrix:
|
||||
include:
|
||||
# Stable channel.
|
||||
- os: linux
|
||||
rust: stable
|
||||
env: TARGET=x86_64-unknown-linux-gnu
|
||||
- os: linux
|
||||
rust: stable
|
||||
env: TARGET=x86_64-unknown-linux-musl
|
||||
- os: linux
|
||||
rust: stable
|
||||
env: TARGET=i686-unknown-linux-gnu
|
||||
- os: linux
|
||||
rust: stable
|
||||
env: TARGET=i686-unknown-linux-musl
|
||||
- os: osx
|
||||
rust: stable
|
||||
env: TARGET=x86_64-apple-darwin
|
||||
|
||||
# Minimum Rust supported channel.
|
||||
- os: linux
|
||||
rust: 1.20.0
|
||||
env: TARGET=x86_64-unknown-linux-gnu
|
||||
- os: linux
|
||||
rust: 1.20.0
|
||||
env: TARGET=x86_64-unknown-linux-musl
|
||||
- os: linux
|
||||
rust: 1.20.0
|
||||
env: TARGET=i686-unknown-linux-gnu
|
||||
- os: linux
|
||||
rust: 1.20.0
|
||||
env: TARGET=i686-unknown-linux-musl
|
||||
- os: osx
|
||||
rust: 1.20.0
|
||||
env: TARGET=x86_64-apple-darwin
|
||||
|
||||
addons:
|
||||
apt:
|
||||
packages:
|
||||
# needed for i686-unknown-linux-gnu target
|
||||
- gcc-multilib
|
||||
# needed to build deb packages
|
||||
- fakeroot
|
||||
|
||||
env:
|
||||
global:
|
||||
# Default target on travis-ci.
|
||||
# Used as conditional check in the install stage
|
||||
- HOST=x86_64-unknown-linux-gnu
|
||||
# Used on the deployment script
|
||||
- PROJECT_NAME=hyperfine
|
||||
|
||||
install:
|
||||
# prevent target re-add error from rustup
|
||||
- if [[ $TRAVIS_OS_NAME = linux && $HOST != $TARGET ]]; then rustup target add $TARGET; fi
|
||||
|
||||
script:
|
||||
# Incorporate TARGET env var to the build and test process
|
||||
- cargo build --target $TARGET --verbose
|
||||
- cargo test --target $TARGET --verbose
|
||||
|
||||
before_deploy:
|
||||
- bash ci/before_deploy.bash
|
||||
|
||||
deploy:
|
||||
provider: releases
|
||||
# NOTE updating the `api_key.secure`
|
||||
# - go to: https://github.com/settings/tokens/new
|
||||
# - generate new token using `public_repo` scope
|
||||
# - encrypt it using: `travis encrypt API_KEY_HERE`
|
||||
# - paste the output below
|
||||
api_key:
|
||||
secure: "U9QfCOQkeozaDNrTV6i2vRocX2puB5p1WSN+w9t/1j33rAtXx6ZhFx67A8nl0+dQ9nvPu/RIsFWXrYzfHQskL1WulKNHm0S5otN+747dEzbSIfMNi1UfysyCiM1SNPq+FX3qOkva83+Fe2Lyd1g4gQObceE4xsgfplfzdm+ebIa6CrfKOypToEuetzP/r8V0WpW4Cp8ojQwvsL1nfHjr1gmvmr443Gr9Yvgq9SS/NAHldrpaJr8mD26W/7jbHdm3lvrNwqLbthYDUTPZsO+2B44h+T/tuhVYbur4fel1KMo4mkIwZwsSKvJbf3Q6Vdcc9dZM+niZU78UGLt0JNEMQNqQLPGYqh+TlFR2B++lcYlybT3LcUaSkt0YIX7C4DAbMLS6sl5Fq2pRNuM/eY2BWdHf0I+K19PDxUEliUifsfSQ3rbfl52aKKLcJa0WPyDJt0qldElagxOaTIk/jn1kKkdkQu9M4gVzKXImIauRQih3CLER7th8ADMxW5Plgq8fvffJBTKlubUznKrGeeR3Mjm8DsRjhkIvFO0Doqmki4sd5zE/BOFI5a/SHg73ms+bLXLuKFK+YzIlHszCHolzkAT44aq7+LEudk8jt+QVtBEocCN/4ew4Q9Qh2tcn22+hU3Aw88lO95PsbZzTzoFtX4X89Sl1fGtpkHFZvvL4ZT0="
|
||||
# for uploading multiple files
|
||||
file_glob: true
|
||||
# NOTE explanation on each env variable
|
||||
# - PROJECT_NAME: name of the project, set on the `env.global` above
|
||||
# - TRAVIS_TAG: tag name that the build is being deployed for, usually the version number
|
||||
# - TARGET: target triple of the build
|
||||
file:
|
||||
- $PROJECT_NAME-$TRAVIS_TAG-$TARGET.*
|
||||
- $PROJECT_NAME*.deb
|
||||
# don't delete artifacts from previous stage
|
||||
skip_cleanup: true
|
||||
on:
|
||||
# deploy only if we push a tag
|
||||
tags: true
|
||||
# deploy only on stable channel that has TARGET env variable sets
|
||||
condition: $TRAVIS_RUST_VERSION = stable && $TARGET != ""
|
||||
|
||||
notifications:
|
||||
email:
|
||||
on_success: never
|
||||
Generated
+21
-14
@@ -11,6 +11,11 @@ name = "ansi_term"
|
||||
version = "0.10.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
|
||||
[[package]]
|
||||
name = "approx"
|
||||
version = "0.1.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
|
||||
[[package]]
|
||||
name = "atty"
|
||||
version = "0.2.6"
|
||||
@@ -28,7 +33,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
|
||||
[[package]]
|
||||
name = "clap"
|
||||
version = "2.29.1"
|
||||
version = "2.29.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
dependencies = [
|
||||
"ansi_term 0.10.2 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
@@ -42,7 +47,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "clicolors-control"
|
||||
version = "0.1.0"
|
||||
version = "0.2.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
dependencies = [
|
||||
"kernel32-sys 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
@@ -53,11 +58,10 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "console"
|
||||
version = "0.5.0"
|
||||
version = "0.6.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
dependencies = [
|
||||
"clicolors-control 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"kernel32-sys 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"clicolors-control 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"lazy_static 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"libc 0.2.36 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"parking_lot 0.5.3 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
@@ -83,20 +87,22 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
|
||||
[[package]]
|
||||
name = "hyperfine"
|
||||
version = "0.2.0"
|
||||
version = "0.3.0"
|
||||
dependencies = [
|
||||
"ansi_term 0.10.2 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"clap 2.29.1 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"indicatif 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"approx 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"clap 2.29.2 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"indicatif 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"libc 0.2.36 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"statistical 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "indicatif"
|
||||
version = "0.8.0"
|
||||
version = "0.9.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
dependencies = [
|
||||
"console 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"console 0.6.1 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"lazy_static 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"parking_lot 0.5.3 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"regex 0.2.5 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
@@ -397,14 +403,15 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
[metadata]
|
||||
"checksum aho-corasick 0.6.4 (registry+https://github.com/rust-lang/crates.io-index)" = "d6531d44de723825aa81398a6415283229725a00fa30713812ab9323faa82fc4"
|
||||
"checksum ansi_term 0.10.2 (registry+https://github.com/rust-lang/crates.io-index)" = "6b3568b48b7cefa6b8ce125f9bb4989e52fbcc29ebea88df04cc7c5f12f70455"
|
||||
"checksum approx 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "08abcc3b4e9339e33a3d0a5ed15d84a687350c05689d825e0f6655eef9e76a94"
|
||||
"checksum atty 0.2.6 (registry+https://github.com/rust-lang/crates.io-index)" = "8352656fd42c30a0c3c89d26dea01e3b77c0ab2af18230835c15e2e13cd51859"
|
||||
"checksum bitflags 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)" = "b3c30d3802dfb7281680d6285f2ccdaa8c2d8fee41f93805dba5c4cf50dc23cf"
|
||||
"checksum clap 2.29.1 (registry+https://github.com/rust-lang/crates.io-index)" = "8f4a2b3bb7ef3c672d7c13d15613211d5a6976b6892c598b0fcb5d40765f19c2"
|
||||
"checksum clicolors-control 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "61bd6bff2f99f947c2dbdc73cd0ebd55d8263b921fd4f68a44598555be49f32b"
|
||||
"checksum console 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)" = "21d6020031c1b7b2b1aa3f9c35568735c46270c6b0e9b338aae6e4a062301711"
|
||||
"checksum clap 2.29.2 (registry+https://github.com/rust-lang/crates.io-index)" = "4151c5790817c7d21bbdc6c3530811f798172915f93258244948b93ba19604a6"
|
||||
"checksum clicolors-control 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "1f84dec9bc083ce2503908cd305af98bd363da6f54bf8d4bf0ac14ee749ad5d1"
|
||||
"checksum console 0.6.1 (registry+https://github.com/rust-lang/crates.io-index)" = "7649ca90478264b9686aac8d269fcb014f14c2bed7c79a7e51b9f6afd4d783eb"
|
||||
"checksum fuchsia-zircon 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)" = "2e9763c69ebaae630ba35f74888db465e49e259ba1bc0eda7d06f4a067615d82"
|
||||
"checksum fuchsia-zircon-sys 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)" = "3dcaa9ae7725d12cdb85b3ad99a434db70b468c09ded17e012d86b5c1010f7a7"
|
||||
"checksum indicatif 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)" = "15aea47abd6d77e1a2d9f17abf42558a647d8c4e0400bff4baa0ced335b78d37"
|
||||
"checksum indicatif 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)" = "a29b2fa6f00010c268bface64c18bb0310aaa70d46a195d5382d288c477fb016"
|
||||
"checksum kernel32-sys 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "7507624b29483431c0ba2d82aece8ca6cdba9382bff4ddd0f7490560c056098d"
|
||||
"checksum lazy_static 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)" = "76f033c7ad61445c5b347c7382dd1237847eb1bce590fe50365dcb33d546be73"
|
||||
"checksum lazy_static 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "c8f31047daa365f19be14b47c29df4f7c3b581832407daabe6ae77397619237d"
|
||||
|
||||
+6
-2
@@ -7,14 +7,18 @@ license = "MIT/Apache-2.0"
|
||||
name = "hyperfine"
|
||||
readme = "README.md"
|
||||
repository = "https://github.com/sharkdp/hyperfine"
|
||||
version = "0.2.0"
|
||||
version = "0.3.0"
|
||||
|
||||
[dependencies]
|
||||
ansi_term = "0.10"
|
||||
indicatif = "0.8"
|
||||
indicatif = "0.9"
|
||||
statistical = "0.1"
|
||||
libc = "0.2"
|
||||
|
||||
[dependencies.clap]
|
||||
version = "2"
|
||||
default-features = false
|
||||
features = ["suggestions", "color", "wrap_help"]
|
||||
|
||||
[dev-dependencies]
|
||||
approx = "0.1.1"
|
||||
@@ -1,13 +1,11 @@
|
||||
# hyperfine
|
||||
[](https://travis-ci.org/sharkdp/hyperfine)
|
||||
[](https://crates.io/crates/hyperfine)
|
||||
|
||||
A command-line benchmarking tool (inspired by [bench](https://github.com/Gabriel439/bench)).
|
||||
A command-line benchmarking tool (*inspired by [bench](https://github.com/Gabriel439/bench)*).
|
||||
|
||||
Usage
|
||||
``` bash
|
||||
> hyperfine 'sleep 0.3' 'sleep 1.7'
|
||||
```
|
||||
|
||||
## Demo
|
||||
**Demo**: Benchmarking [`fd`](https://github.com/sharkdp/fd) and
|
||||
[`find`](https://www.gnu.org/software/findutils/):
|
||||
|
||||

|
||||
|
||||
@@ -15,16 +13,63 @@ Usage
|
||||
|
||||
* Statistical analysis across multiple runs
|
||||
* Support for arbitrary shell commands
|
||||
* Visual indication of the benchmark progress
|
||||
* Warmup runs that are executed before the actual benchmark
|
||||
* Constant feedback about the benchmark progress and current estimates
|
||||
* Warmup runs can be executed before the actual benchmark
|
||||
|
||||
## Usage
|
||||
|
||||
### Basic benchmark
|
||||
|
||||
To run a benchmark, you can simply call `hyperfine <command>...`. The argument(s) can be any
|
||||
shell command. For example:
|
||||
``` bash
|
||||
> hyperfine 'sleep 0.3'
|
||||
```
|
||||
|
||||
Hyperfine will automatically determine the number of runs to perform for each command. By default,
|
||||
it will perform *at least* 10 benchmarking runs. To change this, you can use the `-m`/`--min-runs`
|
||||
option:
|
||||
``` bash
|
||||
> hyperfine --min-runs 5 'sleep 0.2' 'sleep 3.2'
|
||||
```
|
||||
|
||||
### I/O-heavy programs
|
||||
|
||||
If the program execution time is limited by disk I/O, the benchmarking results can be heavily
|
||||
influence by disk caches and whether they are cold or warm.
|
||||
|
||||
If you want to run the benchmark on a warm cache, you can use the `-w`/`--warmup` option to perform
|
||||
a certain number of program executions before the actual benchmark:
|
||||
``` bash
|
||||
> hyperfine --warmup 3 'grep -R TODO *'
|
||||
```
|
||||
|
||||
Conversely, if you want to run the benchmark for a cold cache, you can use the `-p`/`--prepare`
|
||||
option to run a special command before *each* timing run. For example, to clear harddisk caches
|
||||
on Linux, you can run
|
||||
``` bash
|
||||
sync; echo 3 | sudo tee /proc/sys/vm/drop_caches
|
||||
```
|
||||
To use this specific command with Hyperfine, call `sudo echo` to temporarily gain sudo permissions
|
||||
and then call:
|
||||
``` bash
|
||||
hyperfine --prepare 'sync; echo 3 | sudo tee /proc/sys/vm/drop_caches' 'grep -R TODO *'
|
||||
```
|
||||
|
||||
## Installation
|
||||
|
||||
Hyperfine can be install via [cargo](https://doc.rust-lang.org/cargo/):
|
||||
Hyperfine can be installed via [cargo](https://doc.rust-lang.org/cargo/):
|
||||
```
|
||||
cargo install hyperfine
|
||||
```
|
||||
|
||||
### Arch Linux
|
||||
|
||||
On Arch Linux, hyperfine can be installed [from the AUR](https://aur.archlinux.org/packages/hyperfine):
|
||||
```
|
||||
yaourt -S hyperfine
|
||||
```
|
||||
|
||||
## Origin of the name
|
||||
|
||||
The name *hyperfine* was chosen in reference to the hyperfine levels of caesium 133 which play a crucial role in the
|
||||
|
||||
Executable
+103
@@ -0,0 +1,103 @@
|
||||
#!/usr/bin/env bash
|
||||
# Building and packaging for release
|
||||
|
||||
set -ex
|
||||
|
||||
build() {
|
||||
cargo build --target "$TARGET" --release --verbose
|
||||
}
|
||||
|
||||
pack() {
|
||||
local tempdir
|
||||
local out_dir
|
||||
local package_name
|
||||
|
||||
tempdir=$(mktemp -d 2>/dev/null || mktemp -d -t tmp)
|
||||
out_dir=$(pwd)
|
||||
package_name="$PROJECT_NAME-$TRAVIS_TAG-$TARGET"
|
||||
|
||||
# create a "staging" directory
|
||||
mkdir "$tempdir/$package_name"
|
||||
|
||||
# copying the main binary
|
||||
cp "target/$TARGET/release/$PROJECT_NAME" "$tempdir/$package_name/"
|
||||
strip "$tempdir/$package_name/$PROJECT_NAME"
|
||||
|
||||
# readme and license
|
||||
cp README.md "$tempdir/$package_name"
|
||||
cp LICENSE-MIT "$tempdir/$package_name"
|
||||
cp LICENSE-APACHE "$tempdir/$package_name"
|
||||
|
||||
# archiving
|
||||
pushd "$tempdir"
|
||||
tar czf "$out_dir/$package_name.tar.gz" "$package_name"/*
|
||||
popd
|
||||
rm -r "$tempdir"
|
||||
}
|
||||
|
||||
make_deb() {
|
||||
local tempdir
|
||||
local architecture
|
||||
local version
|
||||
local dpkgname
|
||||
local conflictname
|
||||
|
||||
case $TARGET in
|
||||
x86_64*)
|
||||
architecture=amd64
|
||||
;;
|
||||
i686*)
|
||||
architecture=i386
|
||||
;;
|
||||
*)
|
||||
echo "ERROR: unknown target" >&2
|
||||
return 1
|
||||
;;
|
||||
esac
|
||||
version=${TRAVIS_TAG#v}
|
||||
if [[ $TARGET = *musl* ]]; then
|
||||
dpkgname=$PROJECT_NAME-musl
|
||||
conflictname=$PROJECT_NAME
|
||||
else
|
||||
dpkgname=$PROJECT_NAME
|
||||
conflictname=$PROJECT_NAME-musl
|
||||
fi
|
||||
|
||||
tempdir=$(mktemp -d 2>/dev/null || mktemp -d -t tmp)
|
||||
|
||||
# copy the main binary
|
||||
install -Dm755 "target/$TARGET/release/$PROJECT_NAME" "$tempdir/usr/bin/$PROJECT_NAME"
|
||||
strip "$tempdir/usr/bin/$PROJECT_NAME"
|
||||
|
||||
# readme and license
|
||||
install -Dm644 README.md "$tempdir/usr/share/doc/$PROJECT_NAME/README.md"
|
||||
install -Dm644 LICENSE-MIT "$tempdir/usr/share/doc/$PROJECT_NAME/LICENSE-MIT"
|
||||
install -Dm644 LICENSE-APACHE "$tempdir/usr/share/doc/$PROJECT_NAME/LICENSE-APACHE"
|
||||
|
||||
# Control file
|
||||
mkdir "$tempdir/DEBIAN"
|
||||
cat > "$tempdir/DEBIAN/control" <<EOF
|
||||
Package: $dpkgname
|
||||
Version: $version
|
||||
Section: utils
|
||||
Priority: optional
|
||||
Maintainer: David Peter <mail@david-peter.de>
|
||||
Architecture: $architecture
|
||||
Provides: $PROJECT_NAME
|
||||
Conflicts: $conflictname
|
||||
Description: A command-line benchmarking tool.
|
||||
EOF
|
||||
|
||||
fakeroot dpkg-deb --build "$tempdir" "${dpkgname}_${version}_${architecture}.deb"
|
||||
}
|
||||
|
||||
|
||||
main() {
|
||||
build
|
||||
pack
|
||||
if [[ $TARGET = *linux* ]]; then
|
||||
make_deb
|
||||
fi
|
||||
}
|
||||
|
||||
main
|
||||
+121
-71
@@ -2,38 +2,44 @@ use std::io;
|
||||
use std::process::{Command, Stdio};
|
||||
use std::time::Instant;
|
||||
|
||||
use ansi_term::Colour::{Cyan, Green, White, Yellow, Purple};
|
||||
use ansi_term::Colour::{Blue, Cyan, Green, Purple, White, Yellow};
|
||||
use statistical::{mean, standard_deviation};
|
||||
|
||||
use hyperfine::internal::{get_progress_bar, max, min, HyperfineOptions, Second, Warnings,
|
||||
MIN_EXECUTION_TIME};
|
||||
use hyperfine::format::{format_duration, format_duration_unit, Unit};
|
||||
use hyperfine::internal::{get_progress_bar, max, min, CmdFailureAction, HyperfineOptions, Second,
|
||||
Warnings, MIN_EXECUTION_TIME};
|
||||
use hyperfine::format::{format_duration, format_duration_unit};
|
||||
use hyperfine::cputime::{cpu_time_interval, get_cpu_times};
|
||||
|
||||
/// Results from timing a single shell command
|
||||
#[derive(Debug, Copy, Clone)]
|
||||
pub struct TimingResult {
|
||||
/// Execution time in seconds
|
||||
pub execution_time: Second,
|
||||
/// Wall clock time
|
||||
pub time_real: Second,
|
||||
|
||||
/// True if the command finished with exit code zero
|
||||
pub success: bool,
|
||||
/// Time spent in user mode
|
||||
pub time_user: Second,
|
||||
|
||||
/// Time spent in kernel mode
|
||||
pub time_system: Second,
|
||||
}
|
||||
|
||||
impl TimingResult {
|
||||
fn new(execution_time: Second, success: bool) -> TimingResult {
|
||||
TimingResult {
|
||||
execution_time,
|
||||
success,
|
||||
}
|
||||
/// Correct for shell spawning time
|
||||
fn subtract_shell_spawning_time(time: Second, shell_spawning_time: Second) -> Second {
|
||||
if time < shell_spawning_time {
|
||||
0.0
|
||||
} else {
|
||||
time - shell_spawning_time
|
||||
}
|
||||
}
|
||||
|
||||
/// Run the given shell command and measure the execution time
|
||||
pub fn time_shell_command(
|
||||
shell_cmd: &str,
|
||||
ignore_failure: bool,
|
||||
shell_spawning_time: Option<Second>,
|
||||
) -> io::Result<TimingResult> {
|
||||
failure_action: CmdFailureAction,
|
||||
shell_spawning_time: Option<TimingResult>,
|
||||
) -> io::Result<(TimingResult, bool)> {
|
||||
let start = Instant::now();
|
||||
let start_cpu = get_cpu_times();
|
||||
|
||||
let status = Command::new("sh")
|
||||
.arg("-c")
|
||||
@@ -43,7 +49,10 @@ pub fn time_shell_command(
|
||||
.stderr(Stdio::null())
|
||||
.status()?;
|
||||
|
||||
if !ignore_failure && !status.success() {
|
||||
let end_cpu = get_cpu_times();
|
||||
let duration = start.elapsed();
|
||||
|
||||
if failure_action == CmdFailureAction::RaiseError && !status.success() {
|
||||
return Err(io::Error::new(
|
||||
io::ErrorKind::Other,
|
||||
"Command terminated with non-zero exit code. \
|
||||
@@ -51,36 +60,43 @@ pub fn time_shell_command(
|
||||
));
|
||||
}
|
||||
|
||||
let duration = start.elapsed();
|
||||
// Real time
|
||||
let mut time_real = duration.as_secs() as f64 + (duration.subsec_nanos() as f64) * 1e-9;
|
||||
|
||||
let execution_time = duration.as_secs() as f64 + duration.subsec_nanos() as f64 * 1e-9;
|
||||
// User and system time
|
||||
let cpu_interval = cpu_time_interval(&start_cpu, &end_cpu);
|
||||
let mut time_user = cpu_interval.user;
|
||||
let mut time_system = cpu_interval.system;
|
||||
|
||||
// Correct for shell spawning time
|
||||
let execution_time_corrected = if let Some(spawning_time) = shell_spawning_time {
|
||||
if execution_time < spawning_time {
|
||||
0.0
|
||||
} else {
|
||||
execution_time - spawning_time
|
||||
}
|
||||
} else {
|
||||
execution_time
|
||||
};
|
||||
if let Some(spawning_time) = shell_spawning_time {
|
||||
time_real = subtract_shell_spawning_time(time_real, spawning_time.time_real);
|
||||
time_user = subtract_shell_spawning_time(cpu_interval.user, spawning_time.time_user);
|
||||
time_system = subtract_shell_spawning_time(cpu_interval.system, spawning_time.time_system);
|
||||
}
|
||||
|
||||
Ok(TimingResult::new(
|
||||
execution_time_corrected,
|
||||
Ok((
|
||||
TimingResult {
|
||||
time_real,
|
||||
time_user,
|
||||
time_system,
|
||||
},
|
||||
status.success(),
|
||||
))
|
||||
}
|
||||
|
||||
/// Measure the average shell spawning time
|
||||
pub fn mean_shell_spawning_time() -> io::Result<Second> {
|
||||
pub fn mean_shell_spawning_time() -> io::Result<TimingResult> {
|
||||
const COUNT: u64 = 200;
|
||||
let bar = get_progress_bar(COUNT, "Measuring shell spawning time");
|
||||
let progress_bar = get_progress_bar(COUNT, "Measuring shell spawning time");
|
||||
|
||||
let mut times_real: Vec<Second> = vec![];
|
||||
let mut times_user: Vec<Second> = vec![];
|
||||
let mut times_system: Vec<Second> = vec![];
|
||||
|
||||
let mut times: Vec<Second> = vec![];
|
||||
for _ in 0..COUNT {
|
||||
// Just run the shell without any command
|
||||
let res = time_shell_command("", false, None);
|
||||
let res = time_shell_command("", CmdFailureAction::RaiseError, None);
|
||||
|
||||
match res {
|
||||
Err(_) => {
|
||||
@@ -90,22 +106,28 @@ pub fn mean_shell_spawning_time() -> io::Result<Second> {
|
||||
Make sure you can run 'sh -c \"\"'.",
|
||||
))
|
||||
}
|
||||
Ok(r) => {
|
||||
times.push(r.execution_time);
|
||||
Ok((r, _)) => {
|
||||
times_real.push(r.time_real);
|
||||
times_user.push(r.time_user);
|
||||
times_system.push(r.time_system);
|
||||
}
|
||||
}
|
||||
bar.inc(1);
|
||||
progress_bar.inc(1);
|
||||
}
|
||||
bar.finish_and_clear();
|
||||
progress_bar.finish_and_clear();
|
||||
|
||||
Ok(mean(×))
|
||||
Ok(TimingResult {
|
||||
time_real: mean(×_real),
|
||||
time_user: mean(×_user),
|
||||
time_system: mean(×_system),
|
||||
})
|
||||
}
|
||||
|
||||
/// Run the benchmark for a single shell command
|
||||
pub fn run_benchmark(
|
||||
num: usize,
|
||||
cmd: &str,
|
||||
shell_spawning_time: Second,
|
||||
shell_spawning_time: TimingResult,
|
||||
options: &HyperfineOptions,
|
||||
) -> io::Result<()> {
|
||||
println!(
|
||||
@@ -116,29 +138,40 @@ pub fn run_benchmark(
|
||||
);
|
||||
println!();
|
||||
|
||||
let mut execution_times: Vec<Second> = vec![];
|
||||
let mut times_real: Vec<Second> = vec![];
|
||||
let mut times_user: Vec<Second> = vec![];
|
||||
let mut times_system: Vec<Second> = vec![];
|
||||
let mut all_succeeded = true;
|
||||
|
||||
// Warmup phase
|
||||
if options.warmup_count > 0 {
|
||||
let bar = get_progress_bar(options.warmup_count, "Performing warmup runs");
|
||||
let progress_bar = get_progress_bar(options.warmup_count, "Performing warmup runs");
|
||||
|
||||
for _ in 0..options.warmup_count {
|
||||
let _ = time_shell_command(cmd, options.ignore_failure, None)?;
|
||||
bar.inc(1);
|
||||
let _ = time_shell_command(cmd, options.failure_action, None)?;
|
||||
progress_bar.inc(1);
|
||||
}
|
||||
bar.finish_and_clear();
|
||||
progress_bar.finish_and_clear();
|
||||
}
|
||||
|
||||
// Set up progress bar (and spinner for initial measurement)
|
||||
let bar = get_progress_bar(options.min_runs, "Initial time measurement");
|
||||
let progress_bar = get_progress_bar(options.min_runs, "Initial time measurement");
|
||||
|
||||
// Run init / cleanup command
|
||||
let run_preparation_command = || {
|
||||
if let Some(ref preparation_command) = options.preparation_command {
|
||||
let _ = time_shell_command(preparation_command, options.failure_action, None);
|
||||
}
|
||||
};
|
||||
run_preparation_command();
|
||||
|
||||
// Initial timing run
|
||||
let res = time_shell_command(cmd, options.ignore_failure, Some(shell_spawning_time))?;
|
||||
let (res, success) =
|
||||
time_shell_command(cmd, options.failure_action, Some(shell_spawning_time))?;
|
||||
|
||||
// Determine number of benchmark runs
|
||||
let runs_in_min_time =
|
||||
(options.min_time_sec / (res.execution_time + shell_spawning_time)) as u64;
|
||||
(options.min_time_sec / (res.time_real + shell_spawning_time.time_real)) as u64;
|
||||
|
||||
let count = if runs_in_min_time >= options.min_runs {
|
||||
runs_in_min_time
|
||||
@@ -149,47 +182,64 @@ pub fn run_benchmark(
|
||||
let count_remaining = count - 1;
|
||||
|
||||
// Save the first result
|
||||
execution_times.push(res.execution_time);
|
||||
all_succeeded = all_succeeded && res.success;
|
||||
times_real.push(res.time_real);
|
||||
times_user.push(res.time_user);
|
||||
times_system.push(res.time_system);
|
||||
|
||||
all_succeeded = all_succeeded && success;
|
||||
|
||||
// Re-configure the progress bar
|
||||
bar.set_length(count_remaining);
|
||||
progress_bar.set_length(count_remaining);
|
||||
|
||||
// Gather statistics
|
||||
for _ in 0..count_remaining {
|
||||
run_preparation_command();
|
||||
|
||||
let msg = {
|
||||
let mean = format_duration(mean(&execution_times), Unit::Auto);
|
||||
let mean = format_duration(mean(×_real), None);
|
||||
format!("Current estimate: {}", Green.paint(mean))
|
||||
};
|
||||
bar.set_message(&msg);
|
||||
progress_bar.set_message(&msg);
|
||||
|
||||
let res = time_shell_command(cmd, options.ignore_failure, Some(shell_spawning_time))?;
|
||||
let (res, success) =
|
||||
time_shell_command(cmd, options.failure_action, Some(shell_spawning_time))?;
|
||||
|
||||
execution_times.push(res.execution_time);
|
||||
all_succeeded = all_succeeded && res.success;
|
||||
times_real.push(res.time_real);
|
||||
times_user.push(res.time_user);
|
||||
times_system.push(res.time_system);
|
||||
|
||||
bar.inc(1);
|
||||
all_succeeded = all_succeeded && success;
|
||||
|
||||
progress_bar.inc(1);
|
||||
}
|
||||
bar.finish_and_clear();
|
||||
progress_bar.finish_and_clear();
|
||||
|
||||
// Compute statistical quantities
|
||||
let t_mean = mean(&execution_times);
|
||||
let t_stddev = standard_deviation(&execution_times, Some(t_mean));
|
||||
let t_min = min(&execution_times);
|
||||
let t_max = max(&execution_times);
|
||||
let t_mean = mean(×_real);
|
||||
let t_stddev = standard_deviation(×_real, Some(t_mean));
|
||||
let t_min = min(×_real);
|
||||
let t_max = max(×_real);
|
||||
|
||||
let user_mean = mean(×_user);
|
||||
let system_mean = mean(×_system);
|
||||
|
||||
// Formatting and console output
|
||||
let (mean_str, unit_mean) = format_duration_unit(t_mean, Unit::Auto);
|
||||
let stddev_str = format_duration(t_stddev, unit_mean);
|
||||
let min_str = format_duration(t_min, unit_mean);
|
||||
let max_str = format_duration(t_max, unit_mean);
|
||||
let (mean_str, unit_mean) = format_duration_unit(t_mean, None);
|
||||
let stddev_str = format_duration(t_stddev, Some(unit_mean));
|
||||
let min_str = format_duration(t_min, Some(unit_mean));
|
||||
let max_str = format_duration(t_max, Some(unit_mean));
|
||||
|
||||
let (user_str, user_unit) = format_duration_unit(user_mean, None);
|
||||
let system_str = format_duration(system_mean, Some(user_unit));
|
||||
|
||||
println!(
|
||||
" Time ({} ± {}): {} ± {}",
|
||||
" Time ({} ± {}): {} ± {} [User: {}, System: {}]",
|
||||
Green.bold().paint("mean"),
|
||||
Green.paint("σ"),
|
||||
Green.bold().paint(mean_str),
|
||||
Green.paint(stddev_str)
|
||||
Green.paint(stddev_str),
|
||||
Blue.paint(user_str),
|
||||
Blue.paint(system_str)
|
||||
);
|
||||
println!(" ");
|
||||
|
||||
@@ -205,7 +255,7 @@ pub fn run_benchmark(
|
||||
let mut warnings = vec![];
|
||||
|
||||
// Check execution time
|
||||
if execution_times.iter().any(|&t| t < MIN_EXECUTION_TIME) {
|
||||
if times_real.iter().any(|&t| t < MIN_EXECUTION_TIME) {
|
||||
warnings.push(Warnings::FastExecutionTime);
|
||||
}
|
||||
|
||||
@@ -214,7 +264,7 @@ pub fn run_benchmark(
|
||||
warnings.push(Warnings::NonZeroExitCode);
|
||||
}
|
||||
|
||||
if warnings.len() > 0 {
|
||||
if !warnings.is_empty() {
|
||||
eprintln!(" ");
|
||||
for warning in &warnings {
|
||||
eprintln!(" {}: {}", Yellow.paint("Warning"), warning);
|
||||
|
||||
@@ -0,0 +1,74 @@
|
||||
use libc::{getrusage, rusage, RUSAGE_CHILDREN};
|
||||
use std::mem;
|
||||
|
||||
use hyperfine::internal::Second;
|
||||
|
||||
#[derive(Debug, Copy, Clone)]
|
||||
pub struct CPUTimes {
|
||||
/// Total amount of time spent executing in user mode
|
||||
user_usec: i64,
|
||||
|
||||
/// Total amount of time spent executing in kernel mode
|
||||
system_usec: i64,
|
||||
}
|
||||
|
||||
#[derive(Debug, Copy, Clone)]
|
||||
pub struct CPUInterval {
|
||||
/// Total amount of time spent executing in user mode
|
||||
pub user: Second,
|
||||
|
||||
/// Total amount of time spent executing in kernel mode
|
||||
pub system: Second,
|
||||
}
|
||||
|
||||
/// Read CPU execution times ('user' and 'system')
|
||||
pub fn get_cpu_times() -> CPUTimes {
|
||||
let result: rusage = unsafe {
|
||||
let mut buf = mem::zeroed();
|
||||
let success = getrusage(RUSAGE_CHILDREN, &mut buf);
|
||||
assert_eq!(0, success);
|
||||
buf
|
||||
};
|
||||
|
||||
const MICROSEC_PER_SEC: i64 = 1000 * 1000;
|
||||
|
||||
CPUTimes {
|
||||
user_usec: i64::from(result.ru_utime.tv_sec) * MICROSEC_PER_SEC
|
||||
+ i64::from(result.ru_utime.tv_usec),
|
||||
system_usec: i64::from(result.ru_stime.tv_sec) * MICROSEC_PER_SEC
|
||||
+ i64::from(result.ru_stime.tv_usec),
|
||||
}
|
||||
}
|
||||
|
||||
/// Compute the time intervals in between two `CPUTimes` snapshots
|
||||
pub fn cpu_time_interval(start: &CPUTimes, end: &CPUTimes) -> CPUInterval {
|
||||
CPUInterval {
|
||||
user: ((end.user_usec - start.user_usec) as f64) * 1e-6,
|
||||
system: ((end.system_usec - start.system_usec) as f64) * 1e-6,
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_cpu_time_interval() {
|
||||
let t_a = CPUTimes {
|
||||
user_usec: 12345,
|
||||
system_usec: 54321,
|
||||
};
|
||||
|
||||
let t_b = CPUTimes {
|
||||
user_usec: 20000,
|
||||
system_usec: 70000,
|
||||
};
|
||||
|
||||
let t_zero = cpu_time_interval(&t_a, &t_a);
|
||||
assert_eq!(0.0, t_zero.user);
|
||||
assert_eq!(0.0, t_zero.system);
|
||||
|
||||
let t_ab = cpu_time_interval(&t_a, &t_b);
|
||||
assert_relative_eq!(0.007655, t_ab.user);
|
||||
assert_relative_eq!(0.015679, t_ab.system);
|
||||
|
||||
let t_ba = cpu_time_interval(&t_b, &t_a);
|
||||
assert_relative_eq!(-0.007655, t_ba.user);
|
||||
assert_relative_eq!(-0.015679, t_ba.system);
|
||||
}
|
||||
+49
-6
@@ -1,22 +1,65 @@
|
||||
use hyperfine::internal::Second;
|
||||
|
||||
#[derive(Clone, Copy, PartialEq)]
|
||||
#[derive(Debug, Clone, Copy, PartialEq)]
|
||||
pub enum Unit {
|
||||
Auto,
|
||||
Second,
|
||||
MilliSecond,
|
||||
}
|
||||
|
||||
pub fn format_duration(duration: Second, unit: Unit) -> String {
|
||||
/// Format the given duration as a string. The output-unit can be enforced by setting `unit` to
|
||||
/// `Some(target_unit)`. If `unit` is `None`, it will be determined automatically.
|
||||
pub fn format_duration(duration: Second, unit: Option<Unit>) -> String {
|
||||
match format_duration_unit(duration, unit) {
|
||||
(duration_fmt, _) => duration_fmt,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn format_duration_unit(duration: Second, unit: Unit) -> (String, Unit) {
|
||||
if (duration < 1.0 && unit == Unit::Auto) || unit == Unit::MilliSecond {
|
||||
/// Like `format_duration`, but returns the target unit as well.
|
||||
pub fn format_duration_unit(duration: Second, unit: Option<Unit>) -> (String, Unit) {
|
||||
if (duration < 1.0 && unit.is_none()) || unit == Some(Unit::MilliSecond) {
|
||||
(format!("{:5.1} ms", duration * 1e3), Unit::MilliSecond)
|
||||
} else {
|
||||
(format!("{:5.3} s", duration), Unit::Second)
|
||||
(format!("{:6.3} s", duration), Unit::Second)
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_format_duration_unit_basic() {
|
||||
let (out_str, out_unit) = format_duration_unit(1.3, None);
|
||||
|
||||
assert_eq!(" 1.300 s", out_str);
|
||||
assert_eq!(Unit::Second, out_unit);
|
||||
|
||||
let (out_str, out_unit) = format_duration_unit(1.0, None);
|
||||
|
||||
assert_eq!(" 1.000 s", out_str);
|
||||
assert_eq!(Unit::Second, out_unit);
|
||||
|
||||
let (out_str, out_unit) = format_duration_unit(0.999, None);
|
||||
|
||||
assert_eq!("999.0 ms", out_str);
|
||||
assert_eq!(Unit::MilliSecond, out_unit);
|
||||
|
||||
let (out_str, out_unit) = format_duration_unit(0.0, None);
|
||||
|
||||
assert_eq!(" 0.0 ms", out_str);
|
||||
assert_eq!(Unit::MilliSecond, out_unit);
|
||||
|
||||
let (out_str, out_unit) = format_duration_unit(1000.0, None);
|
||||
|
||||
assert_eq!("1000.000 s", out_str);
|
||||
assert_eq!(Unit::Second, out_unit);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_format_duration_unit_with_unit() {
|
||||
let (out_str, out_unit) = format_duration_unit(1.3, Some(Unit::Second));
|
||||
|
||||
assert_eq!(" 1.300 s", out_str);
|
||||
assert_eq!(Unit::Second, out_unit);
|
||||
|
||||
let (out_str, out_unit) = format_duration_unit(1.3, Some(Unit::MilliSecond));
|
||||
|
||||
assert_eq!("1300.0 ms", out_str);
|
||||
assert_eq!(Unit::MilliSecond, out_unit);
|
||||
}
|
||||
+35
-14
@@ -8,6 +8,16 @@ pub type Second = f64;
|
||||
/// Threshold for warning about fast execution time
|
||||
pub const MIN_EXECUTION_TIME: Second = 5e-3;
|
||||
|
||||
/// Action to take when an executed command fails.
|
||||
#[derive(Debug, Clone, Copy, PartialEq)]
|
||||
pub enum CmdFailureAction {
|
||||
/// Exit with an error message
|
||||
RaiseError,
|
||||
|
||||
/// Simply ignore the non-zero exit code
|
||||
Ignore,
|
||||
}
|
||||
|
||||
/// A set of options for hyperfine
|
||||
pub struct HyperfineOptions {
|
||||
/// Number of warmup runs
|
||||
@@ -20,7 +30,10 @@ pub struct HyperfineOptions {
|
||||
pub min_time_sec: Second,
|
||||
|
||||
/// Whether or not to ignore non-zero exit codes
|
||||
pub ignore_failure: bool,
|
||||
pub failure_action: CmdFailureAction,
|
||||
|
||||
/// Command to run before each timing run
|
||||
pub preparation_command: Option<String>,
|
||||
}
|
||||
|
||||
impl Default for HyperfineOptions {
|
||||
@@ -29,7 +42,8 @@ impl Default for HyperfineOptions {
|
||||
warmup_count: 0,
|
||||
min_runs: 10,
|
||||
min_time_sec: 3.0,
|
||||
ignore_failure: false,
|
||||
failure_action: CmdFailureAction::RaiseError,
|
||||
preparation_command: None,
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -40,12 +54,12 @@ pub fn get_progress_bar(length: u64, msg: &str) -> ProgressBar {
|
||||
.tick_chars("⠋⠙⠹⠸⠼⠴⠦⠧⠇⠏")
|
||||
.template(" {spinner} {msg:<30} {wide_bar} ETA {eta_precise}");
|
||||
|
||||
let bar = ProgressBar::new(length);
|
||||
bar.set_style(progressbar_style.clone());
|
||||
bar.enable_steady_tick(80);
|
||||
bar.set_message(msg);
|
||||
let progress_bar = ProgressBar::new(length);
|
||||
progress_bar.set_style(progressbar_style.clone());
|
||||
progress_bar.enable_steady_tick(80);
|
||||
progress_bar.set_message(msg);
|
||||
|
||||
bar
|
||||
progress_bar
|
||||
}
|
||||
|
||||
/// Possible benchmark warnings
|
||||
@@ -56,29 +70,36 @@ pub enum Warnings {
|
||||
|
||||
impl fmt::Display for Warnings {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
match self {
|
||||
&Warnings::FastExecutionTime => write!(
|
||||
match *self {
|
||||
Warnings::FastExecutionTime => write!(
|
||||
f,
|
||||
"Command took less than {:.0} ms to complete. Results might be inaccurate.",
|
||||
MIN_EXECUTION_TIME * 1e3
|
||||
),
|
||||
&Warnings::NonZeroExitCode => write!(f, "Ignoring non-zero exit code."),
|
||||
Warnings::NonZeroExitCode => write!(f, "Ignoring non-zero exit code."),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// A max function for f64's without NaNs
|
||||
pub fn max(vals: &[f64]) -> f64 {
|
||||
vals.iter()
|
||||
*vals.iter()
|
||||
.max_by(|a, b| a.partial_cmp(b).unwrap())
|
||||
.unwrap()
|
||||
.clone()
|
||||
}
|
||||
|
||||
/// A min function for f64's without NaNs
|
||||
pub fn min(vals: &[f64]) -> f64 {
|
||||
vals.iter()
|
||||
*vals.iter()
|
||||
.min_by(|a, b| a.partial_cmp(b).unwrap())
|
||||
.unwrap()
|
||||
.clone()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_max() {
|
||||
assert_eq!(1.0, max(&[1.0]));
|
||||
assert_eq!(-1.0, max(&[-1.0]));
|
||||
assert_eq!(-1.0, max(&[-2.0, -1.0]));
|
||||
assert_eq!(1.0, max(&[-1.0, 1.0]));
|
||||
assert_eq!(1.0, max(&[-1.0, 1.0, 0.0]));
|
||||
}
|
||||
@@ -1,3 +1,4 @@
|
||||
pub mod benchmark;
|
||||
pub mod format;
|
||||
pub mod internal;
|
||||
pub mod cputime;
|
||||
+42
-13
@@ -2,8 +2,13 @@ extern crate ansi_term;
|
||||
#[macro_use]
|
||||
extern crate clap;
|
||||
extern crate indicatif;
|
||||
extern crate libc;
|
||||
extern crate statistical;
|
||||
|
||||
#[cfg(test)]
|
||||
#[macro_use]
|
||||
extern crate approx;
|
||||
|
||||
use std::cmp;
|
||||
use std::error::Error;
|
||||
use std::io;
|
||||
@@ -13,7 +18,7 @@ use clap::{App, AppSettings, Arg};
|
||||
|
||||
mod hyperfine;
|
||||
|
||||
use hyperfine::internal::HyperfineOptions;
|
||||
use hyperfine::internal::{CmdFailureAction, HyperfineOptions};
|
||||
use hyperfine::benchmark::{mean_shell_spawning_time, run_benchmark};
|
||||
|
||||
/// Print error message to stderr and terminate
|
||||
@@ -28,18 +33,23 @@ fn run(commands: &Vec<&str>, options: &HyperfineOptions) -> io::Result<()> {
|
||||
|
||||
// Run the benchmarks
|
||||
for (num, cmd) in commands.iter().enumerate() {
|
||||
run_benchmark(num, &cmd, shell_spawning_time, &options)?;
|
||||
run_benchmark(num, cmd, shell_spawning_time, options)?;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn main() {
|
||||
// Process command line options
|
||||
let mut options = HyperfineOptions::default();
|
||||
|
||||
let matches = App::new("hyperfine")
|
||||
.version(crate_version!())
|
||||
.setting(AppSettings::ColoredHelp)
|
||||
.setting(AppSettings::DeriveDisplayOrder)
|
||||
.about("A command-line benchmarking tool")
|
||||
.setting(AppSettings::UnifiedHelpMessage)
|
||||
.max_term_width(90)
|
||||
.about("A command-line benchmarking tool.")
|
||||
.arg(
|
||||
Arg::with_name("command")
|
||||
.help("Command to benchmark")
|
||||
@@ -53,7 +63,10 @@ fn main() {
|
||||
.short("w")
|
||||
.takes_value(true)
|
||||
.value_name("NUM")
|
||||
.help("Perform NUM warmup runs before the actual benchmark"),
|
||||
.help(
|
||||
"Perform NUM warmup runs before the actual benchmark. This can be used \
|
||||
to fill (disk) caches for I/O-heavy programs.",
|
||||
),
|
||||
)
|
||||
.arg(
|
||||
Arg::with_name("min-runs")
|
||||
@@ -61,21 +74,34 @@ fn main() {
|
||||
.short("m")
|
||||
.takes_value(true)
|
||||
.value_name("NUM")
|
||||
.help("Perform at least NUM runs for each command"),
|
||||
.help(&format!(
|
||||
"Perform at least NUM runs for each command (default: {}).",
|
||||
options.min_runs
|
||||
)),
|
||||
)
|
||||
.arg(
|
||||
Arg::with_name("prepare")
|
||||
.long("prepare")
|
||||
.short("p")
|
||||
.takes_value(true)
|
||||
.value_name("CMD")
|
||||
.help(
|
||||
"Execute CMD before each timing run. This is useful for \
|
||||
clearing disk caches, for example.",
|
||||
),
|
||||
)
|
||||
.arg(
|
||||
Arg::with_name("ignore-failure")
|
||||
.long("ignore-failure")
|
||||
.short("i")
|
||||
.help("Ignore non-zero exit codes"),
|
||||
.help("Ignore non-zero exit codes."),
|
||||
)
|
||||
.help_message("Print this help message.")
|
||||
.version_message("Show version information.")
|
||||
.get_matches();
|
||||
|
||||
let str_to_u64 = |n| u64::from_str_radix(n, 10).ok();
|
||||
|
||||
// Process command line options
|
||||
let mut options = HyperfineOptions::default();
|
||||
|
||||
options.warmup_count = matches
|
||||
.value_of("warmup")
|
||||
.and_then(&str_to_u64)
|
||||
@@ -86,13 +112,16 @@ fn main() {
|
||||
options.min_runs = cmp::max(2, min_runs);
|
||||
}
|
||||
|
||||
options.ignore_failure = matches.is_present("ignore-failure");
|
||||
options.preparation_command = matches.value_of("prepare").map(String::from);
|
||||
|
||||
if matches.is_present("ignore-failure") {
|
||||
options.failure_action = CmdFailureAction::Ignore;
|
||||
}
|
||||
|
||||
let commands = matches.values_of("command").unwrap().collect();
|
||||
let res = run(&commands, &options);
|
||||
|
||||
match res {
|
||||
Err(e) => error(e.description()),
|
||||
Ok(_) => {}
|
||||
if let Err(e) = res {
|
||||
error(e.description());
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user