|  | 
|  | 1 | +CARGO-BENCH(1) | 
|  | 2 | + | 
|  | 3 | +NAME | 
|  | 4 | +       cargo-bench - Execute benchmarks of a package | 
|  | 5 | + | 
|  | 6 | +SYNOPSIS | 
|  | 7 | +       cargo bench [options] [benchname] [-- bench-options] | 
|  | 8 | + | 
|  | 9 | +DESCRIPTION | 
|  | 10 | +       Compile and execute benchmarks. | 
|  | 11 | + | 
|  | 12 | +       The benchmark filtering argument benchname and all the arguments | 
|  | 13 | +       following the two dashes (--) are passed to the benchmark binaries and | 
|  | 14 | +       thus to libtest (rustc's built in unit-test and micro-benchmarking | 
|  | 15 | +       framework). If you are passing arguments to both Cargo and the binary, | 
|  | 16 | +       the ones after -- go to the binary, the ones before go to Cargo. For | 
|  | 17 | +       details about libtest's arguments see the output of cargo bench -- | 
|  | 18 | +       --help. As an example, this will run only the benchmark named foo (and | 
|  | 19 | +       skip other similarly named benchmarks like foobar): | 
|  | 20 | + | 
|  | 21 | +           cargo bench -- foo --exact | 
|  | 22 | + | 
|  | 23 | +       Benchmarks are built with the --test option to rustc which creates an | 
|  | 24 | +       executable with a main function that automatically runs all functions | 
|  | 25 | +       annotated with the #[bench] attribute. Cargo passes the --bench flag to | 
|  | 26 | +       the test harness to tell it to run only benchmarks. | 
|  | 27 | + | 
|  | 28 | +       The libtest harness may be disabled by setting harness = false in the | 
|  | 29 | +       target manifest settings, in which case your code will need to provide | 
|  | 30 | +       its own main function to handle running benchmarks. | 
|  | 31 | + | 
|  | 32 | +          Note: The #[bench] attribute | 
|  | 33 | +          <https://doc.rust-lang.org/nightly/unstable-book/library-features/test.html> | 
|  | 34 | +          is currently unstable and only available on the nightly channel | 
|  | 35 | +          <https://doc.rust-lang.org/book/appendix-07-nightly-rust.html>. There | 
|  | 36 | +          are some packages available on crates.io | 
|  | 37 | +          <https://crates.io/keywords/benchmark> that may help with running | 
|  | 38 | +          benchmarks on the stable channel, such as Criterion | 
|  | 39 | +          <https://crates.io/crates/criterion>. | 
|  | 40 | + | 
|  | 41 | +OPTIONS | 
|  | 42 | +   Benchmark Options | 
|  | 43 | +       --no-run | 
|  | 44 | +           Compile, but don't run benchmarks. | 
|  | 45 | + | 
|  | 46 | +       --no-fail-fast | 
|  | 47 | +           Run all benchmarks regardless of failure. Without this flag, Cargo | 
|  | 48 | +           will exit after the first executable fails. The Rust test harness | 
|  | 49 | +           will run all benchmarks within the executable to completion, this | 
|  | 50 | +           flag only applies to the executable as a whole. | 
|  | 51 | + | 
|  | 52 | +   Package Selection | 
|  | 53 | +       By default, when no package selection options are given, the packages | 
|  | 54 | +       selected depend on the selected manifest file (based on the current | 
|  | 55 | +       working directory if --manifest-path is not given). If the manifest is | 
|  | 56 | +       the root of a workspace then the workspaces default members are | 
|  | 57 | +       selected, otherwise only the package defined by the manifest will be | 
|  | 58 | +       selected. | 
|  | 59 | + | 
|  | 60 | +       The default members of a workspace can be set explicitly with the | 
|  | 61 | +       workspace.default-members key in the root manifest. If this is not set, | 
|  | 62 | +       a virtual workspace will include all workspace members (equivalent to | 
|  | 63 | +       passing --workspace), and a non-virtual workspace will include only the | 
|  | 64 | +       root crate itself. | 
|  | 65 | + | 
|  | 66 | +       -p spec..., --package spec... | 
|  | 67 | +           Benchmark only the specified packages. See cargo-pkgid(1) for the | 
|  | 68 | +           SPEC format. This flag may be specified multiple times. | 
|  | 69 | + | 
|  | 70 | +       --workspace | 
|  | 71 | +           Benchmark all members in the workspace. | 
|  | 72 | + | 
|  | 73 | +       --all | 
|  | 74 | +           Deprecated alias for --workspace. | 
|  | 75 | + | 
|  | 76 | +       --exclude SPEC... | 
|  | 77 | +           Exclude the specified packages. Must be used in conjunction with the | 
|  | 78 | +           --workspace flag. This flag may be specified multiple times. | 
|  | 79 | + | 
|  | 80 | +   Target Selection | 
|  | 81 | +       When no target selection options are given, cargo bench will build the | 
|  | 82 | +       following targets of the selected packages: | 
|  | 83 | + | 
|  | 84 | +       o  lib — used to link with binaries and benchmarks | 
|  | 85 | + | 
|  | 86 | +       o  bins (only if benchmark targets are built and required features are | 
|  | 87 | +          available) | 
|  | 88 | + | 
|  | 89 | +       o  lib as a benchmark | 
|  | 90 | + | 
|  | 91 | +       o  bins as benchmarks | 
|  | 92 | + | 
|  | 93 | +       o  benchmark targets | 
|  | 94 | + | 
|  | 95 | +       The default behavior can be changed by setting the bench flag for the | 
|  | 96 | +       target in the manifest settings. Setting examples to bench = true will | 
|  | 97 | +       build and run the example as a benchmark. Setting targets to bench = | 
|  | 98 | +       false will stop them from being benchmarked by default. Target selection | 
|  | 99 | +       options that take a target by name ignore the bench flag and will always | 
|  | 100 | +       benchmark the given target. | 
|  | 101 | + | 
|  | 102 | +       Passing target selection flags will benchmark only the specified | 
|  | 103 | +       targets. | 
|  | 104 | + | 
|  | 105 | +       --lib | 
|  | 106 | +           Benchmark the package's library. | 
|  | 107 | + | 
|  | 108 | +       --bin name... | 
|  | 109 | +           Benchmark the specified binary. This flag may be specified multiple | 
|  | 110 | +           times. | 
|  | 111 | + | 
|  | 112 | +       --bins | 
|  | 113 | +           Benchmark all binary targets. | 
|  | 114 | + | 
|  | 115 | +       --example name... | 
|  | 116 | +           Benchmark the specified example. This flag may be specified multiple | 
|  | 117 | +           times. | 
|  | 118 | + | 
|  | 119 | +       --examples | 
|  | 120 | +           Benchmark all example targets. | 
|  | 121 | + | 
|  | 122 | +       --test name... | 
|  | 123 | +           Benchmark the specified integration test. This flag may be specified | 
|  | 124 | +           multiple times. | 
|  | 125 | + | 
|  | 126 | +       --tests | 
|  | 127 | +           Benchmark all targets in test mode that have the test = true | 
|  | 128 | +           manifest flag set. By default this includes the library and binaries | 
|  | 129 | +           built as unittests, and integration tests. Be aware that this will | 
|  | 130 | +           also build any required dependencies, so the lib target may be built | 
|  | 131 | +           twice (once as a unittest, and once as a dependency for binaries, | 
|  | 132 | +           integration tests, etc.). Targets may be enabled or disabled by | 
|  | 133 | +           setting the test flag in the manifest settings for the target. | 
|  | 134 | + | 
|  | 135 | +       --bench name... | 
|  | 136 | +           Benchmark the specified benchmark. This flag may be specified | 
|  | 137 | +           multiple times. | 
|  | 138 | + | 
|  | 139 | +       --benches | 
|  | 140 | +           Benchmark all targets in benchmark mode that have the bench = true | 
|  | 141 | +           manifest flag set. By default this includes the library and binaries | 
|  | 142 | +           built as benchmarks, and bench targets. Be aware that this will also | 
|  | 143 | +           build any required dependencies, so the lib target may be built | 
|  | 144 | +           twice (once as a benchmark, and once as a dependency for binaries, | 
|  | 145 | +           benchmarks, etc.). Targets may be enabled or disabled by setting the | 
|  | 146 | +           bench flag in the manifest settings for the target. | 
|  | 147 | + | 
|  | 148 | +       --all-targets | 
|  | 149 | +           Benchmark all targets. This is equivalent to specifying --lib --bins | 
|  | 150 | +           --tests --benches --examples. | 
|  | 151 | + | 
|  | 152 | +   Feature Selection | 
|  | 153 | +       The feature flags allow you to control the enabled features for the | 
|  | 154 | +       "current" package. The "current" package is the package in the current | 
|  | 155 | +       directory, or the one specified in --manifest-path. If running in the | 
|  | 156 | +       root of a virtual workspace, then the default features are selected for | 
|  | 157 | +       all workspace members, or all features if --all-features is specified. | 
|  | 158 | + | 
|  | 159 | +       When no feature options are given, the default feature is activated for | 
|  | 160 | +       every selected package. | 
|  | 161 | + | 
|  | 162 | +       --features features | 
|  | 163 | +           Space or comma separated list of features to activate. These | 
|  | 164 | +           features only apply to the current directory's package. Features of | 
|  | 165 | +           direct dependencies may be enabled with <dep-name>/<feature-name> | 
|  | 166 | +           syntax. This flag may be specified multiple times, which enables all | 
|  | 167 | +           specified features. | 
|  | 168 | + | 
|  | 169 | +       --all-features | 
|  | 170 | +           Activate all available features of all selected packages. | 
|  | 171 | + | 
|  | 172 | +       --no-default-features | 
|  | 173 | +           Do not activate the default feature of the current directory's | 
|  | 174 | +           package. | 
|  | 175 | + | 
|  | 176 | +   Compilation Options | 
|  | 177 | +       --target triple | 
|  | 178 | +           Benchmark for the given architecture. The default is the host | 
|  | 179 | +           architecture. The general format of the triple is | 
|  | 180 | +           <arch><sub>-<vendor>-<sys>-<abi>. Run rustc --print target-list for | 
|  | 181 | +           a list of supported targets. | 
|  | 182 | + | 
|  | 183 | +           This may also be specified with the build.target config value | 
|  | 184 | +           <https://doc.rust-lang.org/cargo/reference/config.html>. | 
|  | 185 | + | 
|  | 186 | +           Note that specifying this flag makes Cargo run in a different mode | 
|  | 187 | +           where the target artifacts are placed in a separate directory. See | 
|  | 188 | +           the build cache | 
|  | 189 | +           <https://doc.rust-lang.org/cargo/guide/build-cache.html> | 
|  | 190 | +           documentation for more details. | 
|  | 191 | + | 
|  | 192 | +   Output Options | 
|  | 193 | +       --target-dir directory | 
|  | 194 | +           Directory for all generated artifacts and intermediate files. May | 
|  | 195 | +           also be specified with the CARGO_TARGET_DIR environment variable, or | 
|  | 196 | +           the build.target-dir config value | 
|  | 197 | +           <https://doc.rust-lang.org/cargo/reference/config.html>. Defaults to | 
|  | 198 | +           target in the root of the workspace. | 
|  | 199 | + | 
|  | 200 | +   Display Options | 
|  | 201 | +       By default the Rust test harness hides output from benchmark execution | 
|  | 202 | +       to keep results readable. Benchmark output can be recovered (e.g., for | 
|  | 203 | +       debugging) by passing --nocapture to the benchmark binaries: | 
|  | 204 | + | 
|  | 205 | +           cargo bench -- --nocapture | 
|  | 206 | + | 
|  | 207 | +       -v, --verbose | 
|  | 208 | +           Use verbose output. May be specified twice for "very verbose" output | 
|  | 209 | +           which includes extra output such as dependency warnings and build | 
|  | 210 | +           script output. May also be specified with the term.verbose config | 
|  | 211 | +           value <https://doc.rust-lang.org/cargo/reference/config.html>. | 
|  | 212 | + | 
|  | 213 | +       -q, --quiet | 
|  | 214 | +           No output printed to stdout. | 
|  | 215 | + | 
|  | 216 | +       --color when | 
|  | 217 | +           Control when colored output is used. Valid values: | 
|  | 218 | + | 
|  | 219 | +           o  auto (default): Automatically detect if color support is | 
|  | 220 | +              available on the terminal. | 
|  | 221 | + | 
|  | 222 | +           o  always: Always display colors. | 
|  | 223 | + | 
|  | 224 | +           o  never: Never display colors. | 
|  | 225 | + | 
|  | 226 | +           May also be specified with the term.color config value | 
|  | 227 | +           <https://doc.rust-lang.org/cargo/reference/config.html>. | 
|  | 228 | + | 
|  | 229 | +       --message-format fmt | 
|  | 230 | +           The output format for diagnostic messages. Can be specified multiple | 
|  | 231 | +           times and consists of comma-separated values. Valid values: | 
|  | 232 | + | 
|  | 233 | +           o  human (default): Display in a human-readable text format. | 
|  | 234 | + | 
|  | 235 | +           o  short: Emit shorter, human-readable text messages. | 
|  | 236 | + | 
|  | 237 | +           o  json: Emit JSON messages to stdout. See the reference | 
|  | 238 | +              <https://doc.rust-lang.org/cargo/reference/external-tools.html#json-messages> | 
|  | 239 | +              for more details. | 
|  | 240 | + | 
|  | 241 | +           o  json-diagnostic-short: Ensure the rendered field of JSON messages | 
|  | 242 | +              contains the "short" rendering from rustc. | 
|  | 243 | + | 
|  | 244 | +           o  json-diagnostic-rendered-ansi: Ensure the rendered field of JSON | 
|  | 245 | +              messages contains embedded ANSI color codes for respecting | 
|  | 246 | +              rustc's default color scheme. | 
|  | 247 | + | 
|  | 248 | +           o  json-render-diagnostics: Instruct Cargo to not include rustc | 
|  | 249 | +              diagnostics in in JSON messages printed, but instead Cargo itself | 
|  | 250 | +              should render the JSON diagnostics coming from rustc. Cargo's own | 
|  | 251 | +              JSON diagnostics and others coming from rustc are still emitted. | 
|  | 252 | + | 
|  | 253 | +   Manifest Options | 
|  | 254 | +       --manifest-path path | 
|  | 255 | +           Path to the Cargo.toml file. By default, Cargo searches for the | 
|  | 256 | +           Cargo.toml file in the current directory or any parent directory. | 
|  | 257 | + | 
|  | 258 | +       --frozen, --locked | 
|  | 259 | +           Either of these flags requires that the Cargo.lock file is | 
|  | 260 | +           up-to-date. If the lock file is missing, or it needs to be updated, | 
|  | 261 | +           Cargo will exit with an error. The --frozen flag also prevents Cargo | 
|  | 262 | +           from attempting to access the network to determine if it is | 
|  | 263 | +           out-of-date. | 
|  | 264 | + | 
|  | 265 | +           These may be used in environments where you want to assert that the | 
|  | 266 | +           Cargo.lock file is up-to-date (such as a CI build) or want to avoid | 
|  | 267 | +           network access. | 
|  | 268 | + | 
|  | 269 | +       --offline | 
|  | 270 | +           Prevents Cargo from accessing the network for any reason. Without | 
|  | 271 | +           this flag, Cargo will stop with an error if it needs to access the | 
|  | 272 | +           network and the network is not available. With this flag, Cargo will | 
|  | 273 | +           attempt to proceed without the network if possible. | 
|  | 274 | + | 
|  | 275 | +           Beware that this may result in different dependency resolution than | 
|  | 276 | +           online mode. Cargo will restrict itself to crates that are | 
|  | 277 | +           downloaded locally, even if there might be a newer version as | 
|  | 278 | +           indicated in the local copy of the index. See the cargo-fetch(1) | 
|  | 279 | +           command to download dependencies before going offline. | 
|  | 280 | + | 
|  | 281 | +           May also be specified with the net.offline config value | 
|  | 282 | +           <https://doc.rust-lang.org/cargo/reference/config.html>. | 
|  | 283 | + | 
|  | 284 | +   Common Options | 
|  | 285 | +       +toolchain | 
|  | 286 | +           If Cargo has been installed with rustup, and the first argument to | 
|  | 287 | +           cargo begins with +, it will be interpreted as a rustup toolchain | 
|  | 288 | +           name (such as +stable or +nightly). See the rustup documentation | 
|  | 289 | +           <https://github.com/rust-lang/rustup/> for more information about | 
|  | 290 | +           how toolchain overrides work. | 
|  | 291 | + | 
|  | 292 | +       -h, --help | 
|  | 293 | +           Prints help information. | 
|  | 294 | + | 
|  | 295 | +       -Z flag | 
|  | 296 | +           Unstable (nightly-only) flags to Cargo. Run cargo -Z help for | 
|  | 297 | +           details. | 
|  | 298 | + | 
|  | 299 | +   Miscellaneous Options | 
|  | 300 | +       The --jobs argument affects the building of the benchmark executable but | 
|  | 301 | +       does not affect how many threads are used when running the benchmarks. | 
|  | 302 | +       The Rust test harness runs benchmarks serially in a single thread. | 
|  | 303 | + | 
|  | 304 | +       -j N, --jobs N | 
|  | 305 | +           Number of parallel jobs to run. May also be specified with the | 
|  | 306 | +           build.jobs config value | 
|  | 307 | +           <https://doc.rust-lang.org/cargo/reference/config.html>. Defaults to | 
|  | 308 | +           the number of CPUs. | 
|  | 309 | + | 
|  | 310 | +PROFILES | 
|  | 311 | +       Profiles may be used to configure compiler options such as optimization | 
|  | 312 | +       levels and debug settings. See the reference | 
|  | 313 | +       <https://doc.rust-lang.org/cargo/reference/profiles.html> for more | 
|  | 314 | +       details. | 
|  | 315 | + | 
|  | 316 | +       Benchmarks are always built with the bench profile. Binary and lib | 
|  | 317 | +       targets are built separately as benchmarks with the bench profile. | 
|  | 318 | +       Library targets are built with the release profiles when linked to | 
|  | 319 | +       binaries and benchmarks. Dependencies use the release profile. | 
|  | 320 | + | 
|  | 321 | +       If you need a debug build of a benchmark, try building it with | 
|  | 322 | +       cargo-build(1) which will use the test profile which is by default | 
|  | 323 | +       unoptimized and includes debug information. You can then run the | 
|  | 324 | +       debug-enabled benchmark manually. | 
|  | 325 | + | 
|  | 326 | +ENVIRONMENT | 
|  | 327 | +       See the reference | 
|  | 328 | +       <https://doc.rust-lang.org/cargo/reference/environment-variables.html> | 
|  | 329 | +       for details on environment variables that Cargo reads. | 
|  | 330 | + | 
|  | 331 | +EXIT STATUS | 
|  | 332 | +       o  0: Cargo succeeded. | 
|  | 333 | + | 
|  | 334 | +       o  101: Cargo failed to complete. | 
|  | 335 | + | 
|  | 336 | +EXAMPLES | 
|  | 337 | +       1. Build and execute all the benchmarks of the current package: | 
|  | 338 | + | 
|  | 339 | +              cargo bench | 
|  | 340 | + | 
|  | 341 | +       2. Run only a specific benchmark within a specific benchmark target: | 
|  | 342 | + | 
|  | 343 | +              cargo bench --bench bench_name -- modname::some_benchmark | 
|  | 344 | + | 
|  | 345 | +SEE ALSO | 
|  | 346 | +       cargo(1), cargo-test(1) | 
|  | 347 | + | 
0 commit comments