Skip to content

Commit 531053e

Browse files
authored
Fix rust benchmark (#923)
1 parent aef1128 commit 531053e

File tree

85 files changed

+6737
-71
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

85 files changed

+6737
-71
lines changed

docs/defs.md

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -53,15 +53,16 @@ Add additional rustc_flags from the command line with `--@rules_rust//:extra_rus
5353
## rust_benchmark
5454

5555
<pre>
56-
rust_benchmark(<a href="#rust_benchmark-name">name</a>, <a href="#rust_benchmark-aliases">aliases</a>, <a href="#rust_benchmark-compile_data">compile_data</a>, <a href="#rust_benchmark-crate_features">crate_features</a>, <a href="#rust_benchmark-crate_name">crate_name</a>, <a href="#rust_benchmark-crate_root">crate_root</a>, <a href="#rust_benchmark-data">data</a>, <a href="#rust_benchmark-deps">deps</a>,
57-
<a href="#rust_benchmark-edition">edition</a>, <a href="#rust_benchmark-proc_macro_deps">proc_macro_deps</a>, <a href="#rust_benchmark-rustc_env">rustc_env</a>, <a href="#rust_benchmark-rustc_env_files">rustc_env_files</a>, <a href="#rust_benchmark-rustc_flags">rustc_flags</a>, <a href="#rust_benchmark-srcs">srcs</a>, <a href="#rust_benchmark-version">version</a>)
56+
rust_benchmark(<a href="#rust_benchmark-name">name</a>, <a href="#rust_benchmark-aliases">aliases</a>, <a href="#rust_benchmark-compile_data">compile_data</a>, <a href="#rust_benchmark-crate_features">crate_features</a>, <a href="#rust_benchmark-crate_name">crate_name</a>, <a href="#rust_benchmark-crate_root">crate_root</a>, <a href="#rust_benchmark-data">data</a>,
57+
<a href="#rust_benchmark-default_args">default_args</a>, <a href="#rust_benchmark-deps">deps</a>, <a href="#rust_benchmark-edition">edition</a>, <a href="#rust_benchmark-proc_macro_deps">proc_macro_deps</a>, <a href="#rust_benchmark-rustc_env">rustc_env</a>, <a href="#rust_benchmark-rustc_env_files">rustc_env_files</a>, <a href="#rust_benchmark-rustc_flags">rustc_flags</a>,
58+
<a href="#rust_benchmark-srcs">srcs</a>, <a href="#rust_benchmark-use_libtest_harness">use_libtest_harness</a>, <a href="#rust_benchmark-version">version</a>)
5859
</pre>
5960

6061
Builds a Rust benchmark test.
6162

6263
**Warning**: This rule is currently experimental. [Rust Benchmark tests][rust-bench] require the `Bencher` interface in the unstable `libtest` crate, which is behind the `test` unstable feature gate. As a result, using this rule would require using a nightly binary release of Rust.
6364

64-
[rust-bench]: https://doc.rust-lang.org/book/benchmark-tests.html
65+
[rust-bench]: https://doc.rust-lang.org/nightly/unstable-book/library-features/test.html
6566

6667
Example:
6768

@@ -114,19 +115,19 @@ To build the benchmark test, add a `rust_benchmark` target:
114115

115116
`fibonacci/BUILD`:
116117
```python
117-
package(default_visibility = ["//visibility:public"])
118-
119118
load("@rules_rust//rust:defs.bzl", "rust_library", "rust_benchmark")
120119

120+
package(default_visibility = ["//visibility:public"])
121+
121122
rust_library(
122-
name = "fibonacci",
123-
srcs = ["src/lib.rs"],
123+
name = "fibonacci",
124+
srcs = ["src/lib.rs"],
124125
)
125126

126127
rust_benchmark(
127-
name = "fibonacci_bench",
128-
srcs = ["benches/fibonacci_bench.rs"],
129-
deps = [":fibonacci"],
128+
name = "fibonacci_bench",
129+
srcs = ["benches/fibonacci_bench.rs"],
130+
deps = [":fibonacci"],
130131
)
131132
```
132133

@@ -145,13 +146,15 @@ Run the benchmark test using: `bazel run //fibonacci:fibonacci_bench`.
145146
| <a id="rust_benchmark-crate_name"></a>crate_name | Crate name to use for this target.<br><br>This must be a valid Rust identifier, i.e. it may contain only alphanumeric characters and underscores. Defaults to the target name, with any hyphens replaced by underscores. | String | optional | "" |
146147
| <a id="rust_benchmark-crate_root"></a>crate_root | The file that will be passed to <code>rustc</code> to be used for building this crate.<br><br>If <code>crate_root</code> is not set, then this rule will look for a <code>lib.rs</code> file (or <code>main.rs</code> for rust_binary) or the single file in <code>srcs</code> if <code>srcs</code> contains only one file. | <a href="https://bazel.build/docs/build-ref.html#labels">Label</a> | optional | None |
147148
| <a id="rust_benchmark-data"></a>data | List of files used by this rule at compile time and runtime.<br><br>If including data at compile time with include_str!() and similar, prefer <code>compile_data</code> over <code>data</code>, to prevent the data also being included in the runfiles. | <a href="https://bazel.build/docs/build-ref.html#labels">List of labels</a> | optional | [] |
149+
| <a id="rust_benchmark-default_args"></a>default_args | The default arguments to pass to the benchmark binary. This attribute is used if the built-in <code>args</code> attribute is not set. | List of strings | optional | ["--bench"] |
148150
| <a id="rust_benchmark-deps"></a>deps | List of other libraries to be linked to this library target.<br><br>These can be either other <code>rust_library</code> targets or <code>cc_library</code> targets if linking a native library. | <a href="https://bazel.build/docs/build-ref.html#labels">List of labels</a> | optional | [] |
149151
| <a id="rust_benchmark-edition"></a>edition | The rust edition to use for this crate. Defaults to the edition specified in the rust_toolchain. | String | optional | "" |
150152
| <a id="rust_benchmark-proc_macro_deps"></a>proc_macro_deps | List of <code>rust_library</code> targets with kind <code>proc-macro</code> used to help build this library target. | <a href="https://bazel.build/docs/build-ref.html#labels">List of labels</a> | optional | [] |
151153
| <a id="rust_benchmark-rustc_env"></a>rustc_env | Dictionary of additional <code>"key": "value"</code> environment variables to set for rustc.<br><br>rust_test()/rust_binary() rules can use $(rootpath //package:target) to pass in the location of a generated file or external tool. Cargo build scripts that wish to expand locations should use cargo_build_script()'s build_script_env argument instead, as build scripts are run in a different environment - see cargo_build_script()'s documentation for more. | <a href="https://bazel.build/docs/skylark/lib/dict.html">Dictionary: String -> String</a> | optional | {} |
152154
| <a id="rust_benchmark-rustc_env_files"></a>rustc_env_files | Files containing additional environment variables to set for rustc.<br><br>These files should contain a single variable per line, of format <code>NAME=value</code>, and newlines may be included in a value by ending a line with a trailing back-slash (<code>\</code>).<br><br>The order that these files will be processed is unspecified, so multiple definitions of a particular variable are discouraged. | <a href="https://bazel.build/docs/build-ref.html#labels">List of labels</a> | optional | [] |
153155
| <a id="rust_benchmark-rustc_flags"></a>rustc_flags | List of compiler flags passed to <code>rustc</code>.<br><br>These strings are subject to Make variable expansion for predefined source/output path variables like <code>$location</code>, <code>$execpath</code>, and <code>$rootpath</code>. This expansion is useful if you wish to pass a generated file of arguments to rustc: <code>@$(location //package:target)</code>. | List of strings | optional | [] |
154156
| <a id="rust_benchmark-srcs"></a>srcs | List of Rust <code>.rs</code> source files used to build the library.<br><br>If <code>srcs</code> contains more than one file, then there must be a file either named <code>lib.rs</code>. Otherwise, <code>crate_root</code> must be set to the source file that is the root of the crate to be passed to rustc to build this crate. | <a href="https://bazel.build/docs/build-ref.html#labels">List of labels</a> | optional | [] |
157+
| <a id="rust_benchmark-use_libtest_harness"></a>use_libtest_harness | Generate the test harness for this benchmark. Note that [Criterion](https://bheisler.github.io/criterion.rs/book/index.html) benchmarks do not use a test harness, but the nightly <code>#[bench]</code> api does. Pass True if you have a <code>#[bench]</code> api test. | Boolean | optional | False |
155158
| <a id="rust_benchmark-version"></a>version | A version to inject in the cargo environment variable. | String | optional | "0.0.0" |
156159

157160

docs/flatten.md

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -218,15 +218,16 @@ Produces a rust-project.json for the given targets. Configure rust-analyzer to l
218218
## rust_benchmark
219219

220220
<pre>
221-
rust_benchmark(<a href="#rust_benchmark-name">name</a>, <a href="#rust_benchmark-aliases">aliases</a>, <a href="#rust_benchmark-compile_data">compile_data</a>, <a href="#rust_benchmark-crate_features">crate_features</a>, <a href="#rust_benchmark-crate_name">crate_name</a>, <a href="#rust_benchmark-crate_root">crate_root</a>, <a href="#rust_benchmark-data">data</a>, <a href="#rust_benchmark-deps">deps</a>,
222-
<a href="#rust_benchmark-edition">edition</a>, <a href="#rust_benchmark-proc_macro_deps">proc_macro_deps</a>, <a href="#rust_benchmark-rustc_env">rustc_env</a>, <a href="#rust_benchmark-rustc_env_files">rustc_env_files</a>, <a href="#rust_benchmark-rustc_flags">rustc_flags</a>, <a href="#rust_benchmark-srcs">srcs</a>, <a href="#rust_benchmark-version">version</a>)
221+
rust_benchmark(<a href="#rust_benchmark-name">name</a>, <a href="#rust_benchmark-aliases">aliases</a>, <a href="#rust_benchmark-compile_data">compile_data</a>, <a href="#rust_benchmark-crate_features">crate_features</a>, <a href="#rust_benchmark-crate_name">crate_name</a>, <a href="#rust_benchmark-crate_root">crate_root</a>, <a href="#rust_benchmark-data">data</a>,
222+
<a href="#rust_benchmark-default_args">default_args</a>, <a href="#rust_benchmark-deps">deps</a>, <a href="#rust_benchmark-edition">edition</a>, <a href="#rust_benchmark-proc_macro_deps">proc_macro_deps</a>, <a href="#rust_benchmark-rustc_env">rustc_env</a>, <a href="#rust_benchmark-rustc_env_files">rustc_env_files</a>, <a href="#rust_benchmark-rustc_flags">rustc_flags</a>,
223+
<a href="#rust_benchmark-srcs">srcs</a>, <a href="#rust_benchmark-use_libtest_harness">use_libtest_harness</a>, <a href="#rust_benchmark-version">version</a>)
223224
</pre>
224225

225226
Builds a Rust benchmark test.
226227

227228
**Warning**: This rule is currently experimental. [Rust Benchmark tests][rust-bench] require the `Bencher` interface in the unstable `libtest` crate, which is behind the `test` unstable feature gate. As a result, using this rule would require using a nightly binary release of Rust.
228229

229-
[rust-bench]: https://doc.rust-lang.org/book/benchmark-tests.html
230+
[rust-bench]: https://doc.rust-lang.org/nightly/unstable-book/library-features/test.html
230231

231232
Example:
232233

@@ -279,19 +280,19 @@ To build the benchmark test, add a `rust_benchmark` target:
279280

280281
`fibonacci/BUILD`:
281282
```python
282-
package(default_visibility = ["//visibility:public"])
283-
284283
load("@rules_rust//rust:defs.bzl", "rust_library", "rust_benchmark")
285284

285+
package(default_visibility = ["//visibility:public"])
286+
286287
rust_library(
287-
name = "fibonacci",
288-
srcs = ["src/lib.rs"],
288+
name = "fibonacci",
289+
srcs = ["src/lib.rs"],
289290
)
290291

291292
rust_benchmark(
292-
name = "fibonacci_bench",
293-
srcs = ["benches/fibonacci_bench.rs"],
294-
deps = [":fibonacci"],
293+
name = "fibonacci_bench",
294+
srcs = ["benches/fibonacci_bench.rs"],
295+
deps = [":fibonacci"],
295296
)
296297
```
297298

@@ -310,13 +311,15 @@ Run the benchmark test using: `bazel run //fibonacci:fibonacci_bench`.
310311
| <a id="rust_benchmark-crate_name"></a>crate_name | Crate name to use for this target.<br><br>This must be a valid Rust identifier, i.e. it may contain only alphanumeric characters and underscores. Defaults to the target name, with any hyphens replaced by underscores. | String | optional | "" |
311312
| <a id="rust_benchmark-crate_root"></a>crate_root | The file that will be passed to <code>rustc</code> to be used for building this crate.<br><br>If <code>crate_root</code> is not set, then this rule will look for a <code>lib.rs</code> file (or <code>main.rs</code> for rust_binary) or the single file in <code>srcs</code> if <code>srcs</code> contains only one file. | <a href="https://bazel.build/docs/build-ref.html#labels">Label</a> | optional | None |
312313
| <a id="rust_benchmark-data"></a>data | List of files used by this rule at compile time and runtime.<br><br>If including data at compile time with include_str!() and similar, prefer <code>compile_data</code> over <code>data</code>, to prevent the data also being included in the runfiles. | <a href="https://bazel.build/docs/build-ref.html#labels">List of labels</a> | optional | [] |
314+
| <a id="rust_benchmark-default_args"></a>default_args | The default arguments to pass to the benchmark binary. This attribute is used if the built-in <code>args</code> attribute is not set. | List of strings | optional | ["--bench"] |
313315
| <a id="rust_benchmark-deps"></a>deps | List of other libraries to be linked to this library target.<br><br>These can be either other <code>rust_library</code> targets or <code>cc_library</code> targets if linking a native library. | <a href="https://bazel.build/docs/build-ref.html#labels">List of labels</a> | optional | [] |
314316
| <a id="rust_benchmark-edition"></a>edition | The rust edition to use for this crate. Defaults to the edition specified in the rust_toolchain. | String | optional | "" |
315317
| <a id="rust_benchmark-proc_macro_deps"></a>proc_macro_deps | List of <code>rust_library</code> targets with kind <code>proc-macro</code> used to help build this library target. | <a href="https://bazel.build/docs/build-ref.html#labels">List of labels</a> | optional | [] |
316318
| <a id="rust_benchmark-rustc_env"></a>rustc_env | Dictionary of additional <code>"key": "value"</code> environment variables to set for rustc.<br><br>rust_test()/rust_binary() rules can use $(rootpath //package:target) to pass in the location of a generated file or external tool. Cargo build scripts that wish to expand locations should use cargo_build_script()'s build_script_env argument instead, as build scripts are run in a different environment - see cargo_build_script()'s documentation for more. | <a href="https://bazel.build/docs/skylark/lib/dict.html">Dictionary: String -> String</a> | optional | {} |
317319
| <a id="rust_benchmark-rustc_env_files"></a>rustc_env_files | Files containing additional environment variables to set for rustc.<br><br>These files should contain a single variable per line, of format <code>NAME=value</code>, and newlines may be included in a value by ending a line with a trailing back-slash (<code>\</code>).<br><br>The order that these files will be processed is unspecified, so multiple definitions of a particular variable are discouraged. | <a href="https://bazel.build/docs/build-ref.html#labels">List of labels</a> | optional | [] |
318320
| <a id="rust_benchmark-rustc_flags"></a>rustc_flags | List of compiler flags passed to <code>rustc</code>.<br><br>These strings are subject to Make variable expansion for predefined source/output path variables like <code>$location</code>, <code>$execpath</code>, and <code>$rootpath</code>. This expansion is useful if you wish to pass a generated file of arguments to rustc: <code>@$(location //package:target)</code>. | List of strings | optional | [] |
319321
| <a id="rust_benchmark-srcs"></a>srcs | List of Rust <code>.rs</code> source files used to build the library.<br><br>If <code>srcs</code> contains more than one file, then there must be a file either named <code>lib.rs</code>. Otherwise, <code>crate_root</code> must be set to the source file that is the root of the crate to be passed to rustc to build this crate. | <a href="https://bazel.build/docs/build-ref.html#labels">List of labels</a> | optional | [] |
322+
| <a id="rust_benchmark-use_libtest_harness"></a>use_libtest_harness | Generate the test harness for this benchmark. Note that [Criterion](https://bheisler.github.io/criterion.rs/book/index.html) benchmarks do not use a test harness, but the nightly <code>#[bench]</code> api does. Pass True if you have a <code>#[bench]</code> api test. | Boolean | optional | False |
320323
| <a id="rust_benchmark-version"></a>version | A version to inject in the cargo environment variable. | String | optional | "0.0.0" |
321324

322325

examples/fibonacci/BUILD.bazel

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
load(
22
"@rules_rust//rust:rust.bzl",
3-
"rust_benchmark",
43
"rust_doc",
54
"rust_doc_test",
65
"rust_library",
@@ -17,16 +16,6 @@ rust_test(
1716
crate = ":fibonacci",
1817
)
1918

20-
rust_benchmark(
21-
name = "fibonacci_bench",
22-
srcs = ["benches/fibonacci_bench.rs"],
23-
tags = [
24-
"manual",
25-
"noci",
26-
],
27-
deps = [":fibonacci"],
28-
)
29-
3019
rust_doc(
3120
name = "fibonacci_doc",
3221
crate = ":fibonacci",

0 commit comments

Comments
 (0)