fork/cargo

git clone https://git.julia.blue/fork/cargo

borsAuto merge of #14675 - RalfJung:help, r=weihanglode128bea7

0.87.0
20.7 KiB466 linesraw
1CARGO-BENCH(1)
2
3NAME
4       cargo-bench — Execute benchmarks of a package
5
6SYNOPSIS
7       cargo bench [options] [benchname] [-- bench-options]
8
9DESCRIPTION
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 and check out the rustc book’s chapter on how tests work at
19       <https://doc.rust-lang.org/rustc/tests/index.html>.
20
21       As an example, this will run only the benchmark named foo (and skip
22       other similarly named benchmarks like foobar):
23
24           cargo bench -- foo --exact
25
26       Benchmarks are built with the --test option to rustc which creates a
27       special executable by linking your code with libtest. The executable
28       automatically runs all functions annotated with the #[bench] attribute.
29       Cargo passes the --bench flag to the test harness to tell it to run only
30       benchmarks, regardless of whether the harness is libtest or a custom
31       harness.
32
33       The libtest harness may be disabled by setting harness = false in the
34       target manifest settings, in which case your code will need to provide
35       its own main function to handle running benchmarks.
36
37          Note: The #[bench] attribute
38          <https://doc.rust-lang.org/nightly/unstable-book/library-features/test.html>
39          is currently unstable and only available on the nightly channel
40          <https://doc.rust-lang.org/book/appendix-07-nightly-rust.html>. There
41          are some packages available on crates.io
42          <https://crates.io/keywords/benchmark> that may help with running
43          benchmarks on the stable channel, such as Criterion
44          <https://crates.io/crates/criterion>.
45
46       By default, cargo bench uses the bench profile
47       <https://doc.rust-lang.org/cargo/reference/profiles.html#bench>, which
48       enables optimizations and disables debugging information. If you need to
49       debug a benchmark, you can use the --profile=dev command-line option to
50       switch to the dev profile. You can then run the debug-enabled benchmark
51       within a debugger.
52
53   Working directory of benchmarks
54       The working directory of every benchmark is set to the root directory of
55       the package the benchmark belongs to. Setting the working directory of
56       benchmarks to the package’s root directory makes it possible for
57       benchmarks to reliably access the package’s files using relative
58       paths, regardless from where cargo bench was executed from.
59
60OPTIONS
61   Benchmark Options
62       --no-run
63           Compile, but don’t run benchmarks.
64
65       --no-fail-fast
66           Run all benchmarks regardless of failure. Without this flag, Cargo
67           will exit after the first executable fails. The Rust test harness
68           will run all benchmarks within the executable to completion, this
69           flag only applies to the executable as a whole.
70
71   Package Selection
72       By default, when no package selection options are given, the packages
73       selected depend on the selected manifest file (based on the current
74       working directory if --manifest-path is not given). If the manifest is
75       the root of a workspace then the workspaces default members are
76       selected, otherwise only the package defined by the manifest will be
77       selected.
78
79       The default members of a workspace can be set explicitly with the
80       workspace.default-members key in the root manifest. If this is not set,
81       a virtual workspace will include all workspace members (equivalent to
82       passing --workspace), and a non-virtual workspace will include only the
83       root crate itself.
84
85       -p spec…, --package spec…
86           Benchmark only the specified packages. See cargo-pkgid(1) for the
87           SPEC format. This flag may be specified multiple times and supports
88           common Unix glob patterns like *, ? and []. However, to avoid your
89           shell accidentally expanding glob patterns before Cargo handles
90           them, you must use single quotes or double quotes around each
91           pattern.
92
93       --workspace
94           Benchmark all members in the workspace.
95
96       --all
97           Deprecated alias for --workspace.
98
99       --exclude SPEC…
100           Exclude the specified packages. Must be used in conjunction with the
101           --workspace flag. This flag may be specified multiple times and
102           supports common Unix glob patterns like *, ? and []. However, to
103           avoid your shell accidentally expanding glob patterns before Cargo
104           handles them, you must use single quotes or double quotes around
105           each pattern.
106
107   Target Selection
108       When no target selection options are given, cargo bench will build the
109       following targets of the selected packages:
110
111       o  lib — used to link with binaries and benchmarks
112
113       o  bins (only if benchmark targets are built and required features are
114          available)
115
116       o  lib as a benchmark
117
118       o  bins as benchmarks
119
120       o  benchmark targets
121
122       The default behavior can be changed by setting the bench flag for the
123       target in the manifest settings. Setting examples to bench = true will
124       build and run the example as a benchmark, replacing the example’s main
125       function with the libtest harness.
126
127       Setting targets to bench = false will stop them from being benchmarked
128       by default. Target selection options that take a target by name (such as
129       --example foo) ignore the bench flag and will always benchmark the given
130       target.
131
132       See Configuring a target
133       <https://doc.rust-lang.org/cargo/reference/cargo-targets.html#configuring-a-target>
134       for more information on per-target settings.
135
136       Binary targets are automatically built if there is an integration test
137       or benchmark being selected to benchmark. This allows an integration
138       test to execute the binary to exercise and test its behavior. The
139       CARGO_BIN_EXE_<name> environment variable
140       <https://doc.rust-lang.org/cargo/reference/environment-variables.html#environment-variables-cargo-sets-for-crates>
141       is set when the integration test is built so that it can use the env
142       macro <https://doc.rust-lang.org/std/macro.env.html> to locate the
143       executable.
144
145       Passing target selection flags will benchmark only the specified
146       targets.
147
148       Note that --bin, --example, --test and --bench flags also support common
149       Unix glob patterns like *, ? and []. However, to avoid your shell
150       accidentally expanding glob patterns before Cargo handles them, you must
151       use single quotes or double quotes around each glob pattern.
152
153       --lib
154           Benchmark the package’s library.
155
156       --bin name…
157           Benchmark the specified binary. This flag may be specified multiple
158           times and supports common Unix glob patterns.
159
160       --bins
161           Benchmark all binary targets.
162
163       --example name…
164           Benchmark the specified example. This flag may be specified multiple
165           times and supports common Unix glob patterns.
166
167       --examples
168           Benchmark all example targets.
169
170       --test name…
171           Benchmark the specified integration test. This flag may be specified
172           multiple times and supports common Unix glob patterns.
173
174       --tests
175           Benchmark all targets that have the test = true manifest flag set.
176           By default this includes the library and binaries built as
177           unittests, and integration tests. Be aware that this will also build
178           any required dependencies, so the lib target may be built twice
179           (once as a unittest, and once as a dependency for binaries,
180           integration tests, etc.). Targets may be enabled or disabled by
181           setting the test flag in the manifest settings for the target.
182
183       --bench name…
184           Benchmark the specified benchmark. This flag may be specified
185           multiple times and supports common Unix glob patterns.
186
187       --benches
188           Benchmark all targets that have the bench = true manifest flag set.
189           By default this includes the library and binaries built as
190           benchmarks, and bench targets. Be aware that this will also build
191           any required dependencies, so the lib target may be built twice
192           (once as a benchmark, and once as a dependency for binaries,
193           benchmarks, etc.). Targets may be enabled or disabled by setting the
194           bench flag in the manifest settings for the target.
195
196       --all-targets
197           Benchmark all targets. This is equivalent to specifying --lib --bins
198           --tests --benches --examples.
199
200   Feature Selection
201       The feature flags allow you to control which features are enabled. When
202       no feature options are given, the default feature is activated for every
203       selected package.
204
205       See the features documentation
206       <https://doc.rust-lang.org/cargo/reference/features.html#command-line-feature-options>
207       for more details.
208
209       -F features, --features features
210           Space or comma separated list of features to activate. Features of
211           workspace members may be enabled with package-name/feature-name
212           syntax. This flag may be specified multiple times, which enables all
213           specified features.
214
215       --all-features
216           Activate all available features of all selected packages.
217
218       --no-default-features
219           Do not activate the default feature of the selected packages.
220
221   Compilation Options
222       --target triple
223           Benchmark for the given architecture. The default is the host
224           architecture. The general format of the triple is
225           <arch><sub>-<vendor>-<sys>-<abi>. Run rustc --print target-list for
226           a list of supported targets. This flag may be specified multiple
227           times.
228
229           This may also be specified with the build.target config value
230           <https://doc.rust-lang.org/cargo/reference/config.html>.
231
232           Note that specifying this flag makes Cargo run in a different mode
233           where the target artifacts are placed in a separate directory. See
234           the build cache
235           <https://doc.rust-lang.org/cargo/reference/build-cache.html>
236           documentation for more details.
237
238       --profile name
239           Benchmark with the given profile. See the reference
240           <https://doc.rust-lang.org/cargo/reference/profiles.html> for more
241           details on profiles.
242
243       --timings=fmts
244           Output information how long each compilation takes, and track
245           concurrency information over time. Accepts an optional
246           comma-separated list of output formats; --timings without an
247           argument will default to --timings=html. Specifying an output format
248           (rather than the default) is unstable and requires
249           -Zunstable-options. Valid output formats:
250
251           o  html (unstable, requires -Zunstable-options): Write a
252              human-readable file cargo-timing.html to the target/cargo-timings
253              directory with a report of the compilation. Also write a report
254              to the same directory with a timestamp in the filename if you
255              want to look at older runs. HTML output is suitable for human
256              consumption only, and does not provide machine-readable timing
257              data.
258
259           o  json (unstable, requires -Zunstable-options): Emit
260              machine-readable JSON information about timing information.
261
262   Output Options
263       --target-dir directory
264           Directory for all generated artifacts and intermediate files. May
265           also be specified with the CARGO_TARGET_DIR environment variable, or
266           the build.target-dir config value
267           <https://doc.rust-lang.org/cargo/reference/config.html>. Defaults to
268           target in the root of the workspace.
269
270   Display Options
271       By default the Rust test harness hides output from benchmark execution
272       to keep results readable. Benchmark output can be recovered (e.g., for
273       debugging) by passing --nocapture to the benchmark binaries:
274
275           cargo bench -- --nocapture
276
277       -v, --verbose
278           Use verbose output. May be specified twice for “very verbose”
279           output which includes extra output such as dependency warnings and
280           build script output. May also be specified with the term.verbose
281           config value
282           <https://doc.rust-lang.org/cargo/reference/config.html>.
283
284       -q, --quiet
285           Do not print cargo log messages. May also be specified with the
286           term.quiet config value
287           <https://doc.rust-lang.org/cargo/reference/config.html>.
288
289       --color when
290           Control when colored output is used. Valid values:
291
292           o  auto (default): Automatically detect if color support is
293              available on the terminal.
294
295           o  always: Always display colors.
296
297           o  never: Never display colors.
298
299           May also be specified with the term.color config value
300           <https://doc.rust-lang.org/cargo/reference/config.html>.
301
302       --message-format fmt
303           The output format for diagnostic messages. Can be specified multiple
304           times and consists of comma-separated values. Valid values:
305
306           o  human (default): Display in a human-readable text format.
307              Conflicts with short and json.
308
309           o  short: Emit shorter, human-readable text messages. Conflicts with
310              human and json.
311
312           o  json: Emit JSON messages to stdout. See the reference
313              <https://doc.rust-lang.org/cargo/reference/external-tools.html#json-messages>
314              for more details. Conflicts with human and short.
315
316           o  json-diagnostic-short: Ensure the rendered field of JSON messages
317              contains the “short” rendering from rustc. Cannot be used
318              with human or short.
319
320           o  json-diagnostic-rendered-ansi: Ensure the rendered field of JSON
321              messages contains embedded ANSI color codes for respecting
322              rustc’s default color scheme. Cannot be used with human or
323              short.
324
325           o  json-render-diagnostics: Instruct Cargo to not include rustc
326              diagnostics in JSON messages printed, but instead Cargo itself
327              should render the JSON diagnostics coming from rustc. Cargo’s
328              own JSON diagnostics and others coming from rustc are still
329              emitted. Cannot be used with human or short.
330
331   Manifest Options
332       --manifest-path path
333           Path to the Cargo.toml file. By default, Cargo searches for the
334           Cargo.toml file in the current directory or any parent directory.
335
336       --ignore-rust-version
337           Ignore rust-version specification in packages.
338
339       --locked
340           Asserts that the exact same dependencies and versions are used as
341           when the existing Cargo.lock file was originally generated. Cargo
342           will exit with an error when either of the following scenarios
343           arises:
344
345           o  The lock file is missing.
346
347           o  Cargo attempted to change the lock file due to a different
348              dependency resolution.
349
350           It may be used in environments where deterministic builds are
351           desired, such as in CI pipelines.
352
353       --offline
354           Prevents Cargo from accessing the network for any reason. Without
355           this flag, Cargo will stop with an error if it needs to access the
356           network and the network is not available. With this flag, Cargo will
357           attempt to proceed without the network if possible.
358
359           Beware that this may result in different dependency resolution than
360           online mode. Cargo will restrict itself to crates that are
361           downloaded locally, even if there might be a newer version as
362           indicated in the local copy of the index. See the cargo-fetch(1)
363           command to download dependencies before going offline.
364
365           May also be specified with the net.offline config value
366           <https://doc.rust-lang.org/cargo/reference/config.html>.
367
368       --frozen
369           Equivalent to specifying both --locked and --offline.
370
371       --lockfile-path PATH
372           Changes the path of the lockfile from the default
373           (<workspace_root>/Cargo.lock) to PATH. PATH must end with Cargo.lock
374           (e.g. --lockfile-path /tmp/temporary-lockfile/Cargo.lock). Note that
375           providing --lockfile-path will ignore existing lockfile at the
376           default path, and instead will either use the lockfile from PATH, or
377           write a new lockfile into the provided PATH if it doesn’t exist.
378           This flag can be used to run most commands in read-only directories,
379           writing lockfile into the provided PATH.
380
381           This option is only available on the nightly channel
382           <https://doc.rust-lang.org/book/appendix-07-nightly-rust.html> and
383           requires the -Z unstable-options flag to enable (see #14421
384           <https://github.com/rust-lang/cargo/issues/14421>).
385
386   Common Options
387       +toolchain
388           If Cargo has been installed with rustup, and the first argument to
389           cargo begins with +, it will be interpreted as a rustup toolchain
390           name (such as +stable or +nightly). See the rustup documentation
391           <https://rust-lang.github.io/rustup/overrides.html> for more
392           information about how toolchain overrides work.
393
394       --config KEY=VALUE or PATH
395           Overrides a Cargo configuration value. The argument should be in
396           TOML syntax of KEY=VALUE, or provided as a path to an extra
397           configuration file. This flag may be specified multiple times. See
398           the command-line overrides section
399           <https://doc.rust-lang.org/cargo/reference/config.html#command-line-overrides>
400           for more information.
401
402       -C PATH
403           Changes the current working directory before executing any specified
404           operations. This affects things like where cargo looks by default
405           for the project manifest (Cargo.toml), as well as the directories
406           searched for discovering .cargo/config.toml, for example. This
407           option must appear before the command name, for example cargo -C
408           path/to/my-project build.
409
410           This option is only available on the nightly channel
411           <https://doc.rust-lang.org/book/appendix-07-nightly-rust.html> and
412           requires the -Z unstable-options flag to enable (see #10098
413           <https://github.com/rust-lang/cargo/issues/10098>).
414
415       -h, --help
416           Prints help information.
417
418       -Z flag
419           Unstable (nightly-only) flags to Cargo. Run cargo -Z help for
420           details.
421
422   Miscellaneous Options
423       The --jobs argument affects the building of the benchmark executable but
424       does not affect how many threads are used when running the benchmarks.
425       The Rust test harness runs benchmarks serially in a single thread.
426
427       -j N, --jobs N
428           Number of parallel jobs to run. May also be specified with the
429           build.jobs config value
430           <https://doc.rust-lang.org/cargo/reference/config.html>. Defaults to
431           the number of logical CPUs. If negative, it sets the maximum number
432           of parallel jobs to the number of logical CPUs plus provided value.
433           If a string default is provided, it sets the value back to defaults.
434           Should not be 0.
435
436       While cargo bench involves compilation, it does not provide a
437       --keep-going flag. Use --no-fail-fast to run as many benchmarks as
438       possible without stopping at the first failure. To “compile” as many
439       benchmarks as possible, use --benches to build benchmark binaries
440       separately. For example:
441
442           cargo build --benches --release --keep-going
443           cargo bench --no-fail-fast
444
445ENVIRONMENT
446       See the reference
447       <https://doc.rust-lang.org/cargo/reference/environment-variables.html>
448       for details on environment variables that Cargo reads.
449
450EXIT STATUS
451       o  0: Cargo succeeded.
452
453       o  101: Cargo failed to complete.
454
455EXAMPLES
456       1. Build and execute all the benchmarks of the current package:
457
458              cargo bench
459
460       2. Run only a specific benchmark within a specific benchmark target:
461
462              cargo bench --bench bench_name -- modname::some_benchmark
463
464SEE ALSO
465       cargo(1), cargo-test(1)
466