blob: b17bb536dc98148ef5eae0cc08c983310c688af1 [file] [log] [blame]
Jeff Vander Stoep956d1432020-09-18 13:46:24 +02001'\" t
2.\" Title: cargo-bench
3.\" Author: [see the "AUTHOR(S)" section]
4.\" Generator: Asciidoctor 2.0.10
5.\" Date: 2020-06-25
6.\" Manual: \ \&
7.\" Source: \ \&
8.\" Language: English
9.\"
10.TH "CARGO\-BENCH" "1" "2020-06-25" "\ \&" "\ \&"
11.ie \n(.g .ds Aq \(aq
12.el .ds Aq '
13.ss \n[.ss] 0
14.nh
15.ad l
16.de URL
17\fI\\$2\fP <\\$1>\\$3
18..
19.als MTO URL
20.if \n[.g] \{\
21. mso www.tmac
22. am URL
23. ad l
24. .
25. am MTO
26. ad l
27. .
28. LINKSTYLE blue R < >
29.\}
30.SH "NAME"
31cargo\-bench \- Execute benchmarks of a package
32.SH "SYNOPSIS"
33.sp
34\fBcargo bench [\fIOPTIONS\fP] [BENCHNAME] [\-\- \fIBENCH\-OPTIONS\fP]\fP
35.SH "DESCRIPTION"
36.sp
37Compile and execute benchmarks.
38.sp
39The benchmark filtering argument \fBBENCHNAME\fP and all the arguments following
40the two dashes (\fB\-\-\fP) are passed to the benchmark binaries and thus to
41\fIlibtest\fP (rustc\(cqs built in unit\-test and micro\-benchmarking framework). If
42you\(cqre passing arguments to both Cargo and the binary, the ones after \fB\-\-\fP go
43to the binary, the ones before go to Cargo. For details about libtest\(cqs
44arguments see the output of \fBcargo bench \-\- \-\-help\fP. As an example, this will
45run only the benchmark named \fBfoo\fP (and skip other similarly named benchmarks
46like \fBfoobar\fP):
47.sp
48.if n .RS 4
49.nf
50cargo bench \-\- foo \-\-exact
51.fi
52.if n .RE
53.sp
54Benchmarks are built with the \fB\-\-test\fP option to \fBrustc\fP which creates an
55executable with a \fBmain\fP function that automatically runs all functions
56annotated with the \fB#[bench]\fP attribute. Cargo passes the \fB\-\-bench\fP flag to
57the test harness to tell it to run only benchmarks.
58.sp
59The libtest harness may be disabled by setting \fBharness = false\fP in the target
60manifest settings, in which case your code will need to provide its own \fBmain\fP
61function to handle running benchmarks.
62.RS 3
63.ll -.6i
64.sp
65\fBNote\fP: The
66\c
67.URL "https://doc.rust\-lang.org/nightly/unstable\-book/library\-features/test.html" "\fB#[bench]\fP attribute"
68is currently unstable and only available on the
69.URL "https://doc.rust\-lang.org/book/appendix\-07\-nightly\-rust.html" "nightly channel" "."
70There are some packages available on
71.URL "https://crates.io/keywords/benchmark" "crates.io" " "
72that may help with
73running benchmarks on the stable channel, such as
74.URL "https://crates.io/crates/criterion" "Criterion" "."
75.br
76.RE
77.ll
78.SH "OPTIONS"
79.SS "Benchmark Options"
80.sp
81\fB\-\-no\-run\fP
82.RS 4
83Compile, but don\(cqt run benchmarks.
84.RE
85.sp
86\fB\-\-no\-fail\-fast\fP
87.RS 4
88Run all benchmarks regardless of failure. Without this flag, Cargo will exit
89after the first executable fails. The Rust test harness will run all
90benchmarks within the executable to completion, this flag only applies to
91the executable as a whole.
92.RE
93.SS "Package Selection"
94.sp
95By default, when no package selection options are given, the packages selected
96depend on the selected manifest file (based on the current working directory if
97\fB\-\-manifest\-path\fP is not given). If the manifest is the root of a workspace then
98the workspaces default members are selected, otherwise only the package defined
99by the manifest will be selected.
100.sp
101The default members of a workspace can be set explicitly with the
102\fBworkspace.default\-members\fP key in the root manifest. If this is not set, a
103virtual workspace will include all workspace members (equivalent to passing
104\fB\-\-workspace\fP), and a non\-virtual workspace will include only the root crate itself.
105.sp
106\fB\-p\fP \fISPEC\fP..., \fB\-\-package\fP \fISPEC\fP...
107.RS 4
108Benchmark only the specified packages. See \fBcargo\-pkgid\fP(1) for the
109SPEC format. This flag may be specified multiple times.
110.RE
111.sp
112\fB\-\-workspace\fP
113.RS 4
114Benchmark all members in the workspace.
115.RE
116.sp
117\fB\-\-all\fP
118.RS 4
119Deprecated alias for \fB\-\-workspace\fP.
120.RE
121.sp
122\fB\-\-exclude\fP \fISPEC\fP...
123.RS 4
124Exclude the specified packages. Must be used in conjunction with the
125\fB\-\-workspace\fP flag. This flag may be specified multiple times.
126.RE
127.SS "Target Selection"
128.sp
129When no target selection options are given, \fBcargo bench\fP will build the
130following targets of the selected packages:
131.sp
132.RS 4
133.ie n \{\
134\h'-04'\(bu\h'+03'\c
135.\}
136.el \{\
137. sp -1
138. IP \(bu 2.3
139.\}
140lib used to link with binaries and benchmarks
141.RE
142.sp
143.RS 4
144.ie n \{\
145\h'-04'\(bu\h'+03'\c
146.\}
147.el \{\
148. sp -1
149. IP \(bu 2.3
150.\}
151bins (only if benchmark targets are built and required features are
152available)
153.RE
154.sp
155.RS 4
156.ie n \{\
157\h'-04'\(bu\h'+03'\c
158.\}
159.el \{\
160. sp -1
161. IP \(bu 2.3
162.\}
163lib as a benchmark
164.RE
165.sp
166.RS 4
167.ie n \{\
168\h'-04'\(bu\h'+03'\c
169.\}
170.el \{\
171. sp -1
172. IP \(bu 2.3
173.\}
174bins as benchmarks
175.RE
176.sp
177.RS 4
178.ie n \{\
179\h'-04'\(bu\h'+03'\c
180.\}
181.el \{\
182. sp -1
183. IP \(bu 2.3
184.\}
185benchmark targets
186.RE
187.sp
188The default behavior can be changed by setting the \fBbench\fP flag for the target
189in the manifest settings. Setting examples to \fBbench = true\fP will build and
190run the example as a benchmark. Setting targets to \fBbench = false\fP will stop
191them from being benchmarked by default. Target selection options that take a
192target by name ignore the \fBbench\fP flag and will always benchmark the given
193target.
194.sp
195Passing target selection flags will benchmark only the
196specified targets.
197.sp
198\fB\-\-lib\fP
199.RS 4
200Benchmark the package\(cqs library.
201.RE
202.sp
203\fB\-\-bin\fP \fINAME\fP...
204.RS 4
205Benchmark the specified binary. This flag may be specified multiple times.
206.RE
207.sp
208\fB\-\-bins\fP
209.RS 4
210Benchmark all binary targets.
211.RE
212.sp
213\fB\-\-example\fP \fINAME\fP...
214.RS 4
215Benchmark the specified example. This flag may be specified multiple times.
216.RE
217.sp
218\fB\-\-examples\fP
219.RS 4
220Benchmark all example targets.
221.RE
222.sp
223\fB\-\-test\fP \fINAME\fP...
224.RS 4
225Benchmark the specified integration test. This flag may be specified multiple
226times.
227.RE
228.sp
229\fB\-\-tests\fP
230.RS 4
231Benchmark all targets in test mode that have the \fBtest = true\fP manifest
232flag set. By default this includes the library and binaries built as
233unittests, and integration tests. Be aware that this will also build any
234required dependencies, so the lib target may be built twice (once as a
235unittest, and once as a dependency for binaries, integration tests, etc.).
236Targets may be enabled or disabled by setting the \fBtest\fP flag in the
237manifest settings for the target.
238.RE
239.sp
240\fB\-\-bench\fP \fINAME\fP...
241.RS 4
242Benchmark the specified benchmark. This flag may be specified multiple times.
243.RE
244.sp
245\fB\-\-benches\fP
246.RS 4
247Benchmark all targets in benchmark mode that have the \fBbench = true\fP
248manifest flag set. By default this includes the library and binaries built
249as benchmarks, and bench targets. Be aware that this will also build any
250required dependencies, so the lib target may be built twice (once as a
251benchmark, and once as a dependency for binaries, benchmarks, etc.).
252Targets may be enabled or disabled by setting the \fBbench\fP flag in the
253manifest settings for the target.
254.RE
255.sp
256\fB\-\-all\-targets\fP
257.RS 4
258Benchmark all targets. This is equivalent to specifying \fB\-\-lib \-\-bins
259\-\-tests \-\-benches \-\-examples\fP.
260.RE
261.SS "Feature Selection"
262.sp
263The feature flags allow you to control the enabled features for the "current"
264package. The "current" package is the package in the current directory, or the
265one specified in \fB\-\-manifest\-path\fP. If running in the root of a virtual
266workspace, then the default features are selected for all workspace members,
267or all features if \fB\-\-all\-features\fP is specified.
268.sp
269When no feature options are given, the \fBdefault\fP feature is activated for
270every selected package.
271.sp
272\fB\-\-features\fP \fIFEATURES\fP
273.RS 4
274Space or comma separated list of features to activate. These features only
275apply to the current directory\(cqs package. Features of direct dependencies
276may be enabled with \fB<dep\-name>/<feature\-name>\fP syntax. This flag may be
277specified multiple times, which enables all specified features.
278.RE
279.sp
280\fB\-\-all\-features\fP
281.RS 4
282Activate all available features of all selected packages.
283.RE
284.sp
285\fB\-\-no\-default\-features\fP
286.RS 4
287Do not activate the \fBdefault\fP feature of the current directory\(cqs
288package.
289.RE
290.SS "Compilation Options"
291.sp
292\fB\-\-target\fP \fITRIPLE\fP
293.RS 4
294Benchmark for the given architecture. The default is the host
295architecture. The general format of the triple is
296\fB<arch><sub>\-<vendor>\-<sys>\-<abi>\fP. Run \fBrustc \-\-print target\-list\fP for a
297list of supported targets.
298.sp
299This may also be specified with the \fBbuild.target\fP
300.URL "https://doc.rust\-lang.org/cargo/reference/config.html" "config value" "."
301.sp
302Note that specifying this flag makes Cargo run in a different mode where the
303target artifacts are placed in a separate directory. See the
304.URL "https://doc.rust\-lang.org/cargo/guide/build\-cache.html" "build cache" " "
305documentation for more details.
306.RE
307.SS "Output Options"
308.sp
309\fB\-\-target\-dir\fP \fIDIRECTORY\fP
310.RS 4
311Directory for all generated artifacts and intermediate files. May also be
312specified with the \fBCARGO_TARGET_DIR\fP environment variable, or the
313\fBbuild.target\-dir\fP \c
314.URL "https://doc.rust\-lang.org/cargo/reference/config.html" "config value" "."
315Defaults
316to \fBtarget\fP in the root of the workspace.
317.RE
318.SS "Display Options"
319.sp
320By default the Rust test harness hides output from benchmark execution to keep
321results readable. Benchmark output can be recovered (e.g., for debugging) by
322passing \fB\-\-nocapture\fP to the benchmark binaries:
323.sp
324.if n .RS 4
325.nf
326cargo bench \-\- \-\-nocapture
327.fi
328.if n .RE
329.sp
330\fB\-v\fP, \fB\-\-verbose\fP
331.RS 4
332Use verbose output. May be specified twice for "very verbose" output which
333includes extra output such as dependency warnings and build script output.
334May also be specified with the \fBterm.verbose\fP
335.URL "https://doc.rust\-lang.org/cargo/reference/config.html" "config value" "."
336.RE
337.sp
338\fB\-q\fP, \fB\-\-quiet\fP
339.RS 4
340No output printed to stdout.
341.RE
342.sp
343\fB\-\-color\fP \fIWHEN\fP
344.RS 4
345Control when colored output is used. Valid values:
346.sp
347.RS 4
348.ie n \{\
349\h'-04'\(bu\h'+03'\c
350.\}
351.el \{\
352. sp -1
353. IP \(bu 2.3
354.\}
355\fBauto\fP (default): Automatically detect if color support is available on the
356terminal.
357.RE
358.sp
359.RS 4
360.ie n \{\
361\h'-04'\(bu\h'+03'\c
362.\}
363.el \{\
364. sp -1
365. IP \(bu 2.3
366.\}
367\fBalways\fP: Always display colors.
368.RE
369.sp
370.RS 4
371.ie n \{\
372\h'-04'\(bu\h'+03'\c
373.\}
374.el \{\
375. sp -1
376. IP \(bu 2.3
377.\}
378\fBnever\fP: Never display colors.
379.RE
380.sp
381May also be specified with the \fBterm.color\fP
382.URL "https://doc.rust\-lang.org/cargo/reference/config.html" "config value" "."
383.RE
384.sp
385\fB\-\-message\-format\fP \fIFMT\fP
386.RS 4
387The output format for diagnostic messages. Can be specified multiple times
388and consists of comma\-separated values. Valid values:
389.sp
390.RS 4
391.ie n \{\
392\h'-04'\(bu\h'+03'\c
393.\}
394.el \{\
395. sp -1
396. IP \(bu 2.3
397.\}
398\fBhuman\fP (default): Display in a human\-readable text format.
399.RE
400.sp
401.RS 4
402.ie n \{\
403\h'-04'\(bu\h'+03'\c
404.\}
405.el \{\
406. sp -1
407. IP \(bu 2.3
408.\}
409\fBshort\fP: Emit shorter, human\-readable text messages.
410.RE
411.sp
412.RS 4
413.ie n \{\
414\h'-04'\(bu\h'+03'\c
415.\}
416.el \{\
417. sp -1
418. IP \(bu 2.3
419.\}
420\fBjson\fP: Emit JSON messages to stdout. See
421\c
422.URL "https://doc.rust\-lang.org/cargo/reference/external\-tools.html#json\-messages" "the reference"
423for more details.
424.RE
425.sp
426.RS 4
427.ie n \{\
428\h'-04'\(bu\h'+03'\c
429.\}
430.el \{\
431. sp -1
432. IP \(bu 2.3
433.\}
434\fBjson\-diagnostic\-short\fP: Ensure the \fBrendered\fP field of JSON messages contains
435the "short" rendering from rustc.
436.RE
437.sp
438.RS 4
439.ie n \{\
440\h'-04'\(bu\h'+03'\c
441.\}
442.el \{\
443. sp -1
444. IP \(bu 2.3
445.\}
446\fBjson\-diagnostic\-rendered\-ansi\fP: Ensure the \fBrendered\fP field of JSON messages
447contains embedded ANSI color codes for respecting rustc\(cqs default color
448scheme.
449.RE
450.sp
451.RS 4
452.ie n \{\
453\h'-04'\(bu\h'+03'\c
454.\}
455.el \{\
456. sp -1
457. IP \(bu 2.3
458.\}
459\fBjson\-render\-diagnostics\fP: Instruct Cargo to not include rustc diagnostics in
460in JSON messages printed, but instead Cargo itself should render the
461JSON diagnostics coming from rustc. Cargo\(cqs own JSON diagnostics and others
462coming from rustc are still emitted.
463.RE
464.RE
465.SS "Manifest Options"
466.sp
467\fB\-\-manifest\-path\fP \fIPATH\fP
468.RS 4
469Path to the \fBCargo.toml\fP file. By default, Cargo searches for the
470\fBCargo.toml\fP file in the current directory or any parent directory.
471.RE
472.sp
473\fB\-\-frozen\fP, \fB\-\-locked\fP
474.RS 4
475Either of these flags requires that the \fBCargo.lock\fP file is
476up\-to\-date. If the lock file is missing, or it needs to be updated, Cargo will
477exit with an error. The \fB\-\-frozen\fP flag also prevents Cargo from
478attempting to access the network to determine if it is out\-of\-date.
479.sp
480These may be used in environments where you want to assert that the
481\fBCargo.lock\fP file is up\-to\-date (such as a CI build) or want to avoid network
482access.
483.RE
484.sp
485\fB\-\-offline\fP
486.RS 4
487Prevents Cargo from accessing the network for any reason. Without this
488flag, Cargo will stop with an error if it needs to access the network and
489the network is not available. With this flag, Cargo will attempt to
490proceed without the network if possible.
491.sp
492Beware that this may result in different dependency resolution than online
493mode. Cargo will restrict itself to crates that are downloaded locally, even
494if there might be a newer version as indicated in the local copy of the index.
495See the \fBcargo\-fetch\fP(1) command to download dependencies before going
496offline.
497.sp
498May also be specified with the \fBnet.offline\fP \c
499.URL "https://doc.rust\-lang.org/cargo/reference/config.html" "config value" "."
500.RE
501.SS "Common Options"
502.sp
503\fB+TOOLCHAIN\fP
504.RS 4
505If Cargo has been installed with rustup, and the first argument to \fBcargo\fP
506begins with \fB+\fP, it will be interpreted as a rustup toolchain name (such
507as \fB+stable\fP or \fB+nightly\fP).
508See the \c
509.URL "https://github.com/rust\-lang/rustup/" "rustup documentation"
510for more information about how toolchain overrides work.
511.RE
512.sp
513\fB\-h\fP, \fB\-\-help\fP
514.RS 4
515Prints help information.
516.RE
517.sp
518\fB\-Z\fP \fIFLAG\fP...
519.RS 4
520Unstable (nightly\-only) flags to Cargo. Run \fBcargo \-Z help\fP for
521details.
522.RE
523.SS "Miscellaneous Options"
524.sp
525The \fB\-\-jobs\fP argument affects the building of the benchmark executable but
526does not affect how many threads are used when running the benchmarks. The
527Rust test harness runs benchmarks serially in a single thread.
528.sp
529\fB\-j\fP \fIN\fP, \fB\-\-jobs\fP \fIN\fP
530.RS 4
531Number of parallel jobs to run. May also be specified with the
532\fBbuild.jobs\fP \c
533.URL "https://doc.rust\-lang.org/cargo/reference/config.html" "config value" "."
534Defaults to
535the number of CPUs.
536.RE
537.SH "PROFILES"
538.sp
539Profiles may be used to configure compiler options such as optimization levels
540and debug settings. See
541\c
542.URL "https://doc.rust\-lang.org/cargo/reference/profiles.html" "the reference"
543for more details.
544.sp
545Benchmarks are always built with the \fBbench\fP profile. Binary and lib targets
546are built separately as benchmarks with the \fBbench\fP profile. Library targets
547are built with the \fBrelease\fP profiles when linked to binaries and benchmarks.
548Dependencies use the \fBrelease\fP profile.
549.sp
550If you need a debug build of a benchmark, try building it with
551\fBcargo\-build\fP(1) which will use the \fBtest\fP profile which is by default
552unoptimized and includes debug information. You can then run the debug\-enabled
553benchmark manually.
554.SH "ENVIRONMENT"
555.sp
556See \c
557.URL "https://doc.rust\-lang.org/cargo/reference/environment\-variables.html" "the reference" " "
558for
559details on environment variables that Cargo reads.
560.SH "EXIT STATUS"
561.sp
5620
563.RS 4
564Cargo succeeded.
565.RE
566.sp
567101
568.RS 4
569Cargo failed to complete.
570.RE
571.SH "EXAMPLES"
572.sp
573.RS 4
574.ie n \{\
575\h'-04' 1.\h'+01'\c
576.\}
577.el \{\
578. sp -1
579. IP " 1." 4.2
580.\}
581Build and execute all the benchmarks of the current package:
582.sp
583.if n .RS 4
584.nf
585cargo bench
586.fi
587.if n .RE
588.RE
589.sp
590.RS 4
591.ie n \{\
592\h'-04' 2.\h'+01'\c
593.\}
594.el \{\
595. sp -1
596. IP " 2." 4.2
597.\}
598Run only a specific benchmark within a specific benchmark target:
599.sp
600.if n .RS 4
601.nf
602cargo bench \-\-bench bench_name \-\- modname::some_benchmark
603.fi
604.if n .RE
605.RE
606.SH "SEE ALSO"
607.sp
608\fBcargo\fP(1), \fBcargo\-test\fP(1)