1+ mod benchmark;
2+
13use crate :: benchmark:: profile:: Profile ;
24use crate :: toolchain:: { get_local_toolchain, LocalToolchain } ;
3- use benchlib:: benchmark:: passes_filter;
45use benchlib:: comm:: messages:: BenchmarkMessage ;
5- use cargo_metadata:: Message ;
66use std:: io:: { BufRead , BufReader } ;
77use std:: path:: { Path , PathBuf } ;
88use std:: process:: { Command , Stdio } ;
99
10- #[ derive( Debug ) ]
11- struct BenchmarkBinary {
12- path : PathBuf ,
13- benchmark_names : Vec < String > ,
14- }
15-
16- impl BenchmarkBinary {
17- fn name ( & self ) -> & str {
18- self . path . file_name ( ) . unwrap ( ) . to_str ( ) . unwrap ( )
19- }
20- }
21-
22- #[ derive( Debug ) ]
23- struct BenchmarkDatabase {
24- binaries : Vec < BenchmarkBinary > ,
25- }
26-
27- impl BenchmarkDatabase {
28- fn benchmark_names ( & self ) -> impl Iterator < Item = & str > {
29- self . binaries
30- . iter ( )
31- . flat_map ( |binary| binary. benchmark_names . iter ( ) . map ( |n| n. as_ref ( ) ) )
32- }
33-
34- fn total_benchmark_count ( & self ) -> u64 {
35- self . benchmark_names ( ) . count ( ) as u64
36- }
37- fn filtered_benchmark_count ( & self , filter : & BenchmarkFilter ) -> u64 {
38- self . benchmark_names ( )
39- . filter ( |benchmark| {
40- passes_filter (
41- & benchmark,
42- filter. exclude . as_deref ( ) ,
43- filter. include . as_deref ( ) ,
44- )
45- } )
46- . count ( ) as u64
47- }
48- }
49-
50- pub struct BenchmarkFilter {
51- exclude : Option < String > ,
52- include : Option < String > ,
53- }
54-
55- impl BenchmarkFilter {
56- pub fn new ( exclude : Option < String > , include : Option < String > ) -> BenchmarkFilter {
57- Self { exclude, include }
58- }
59- }
10+ pub use benchmark:: BenchmarkFilter ;
6011
6112/// Perform a series of runtime benchmarks using the provided `rustc` compiler.
6213/// The runtime benchmarks are looked up in `benchmark_dir`, which is expected to be a path
@@ -70,7 +21,7 @@ pub fn bench_runtime(
7021) -> anyhow:: Result < ( ) > {
7122 let toolchain = get_local_toolchain ( & [ Profile :: Opt ] , rustc, None , None , id, "" ) ?;
7223 let output = compile_runtime_benchmarks ( & toolchain, & benchmark_dir) ?;
73- let benchmark_db = discover_benchmarks ( & output) ?;
24+ let benchmark_db = benchmark :: discover_benchmarks ( & output) ?;
7425
7526 let total_benchmark_count = benchmark_db. total_benchmark_count ( ) ;
7627 let filtered = benchmark_db. filtered_benchmark_count ( & filter) ;
@@ -162,44 +113,3 @@ fn compile_runtime_benchmarks(toolchain: &LocalToolchain, dir: &Path) -> anyhow:
162113 return Ok ( result. stdout ) ;
163114 }
164115}
165-
166- /// Parse Cargo JSON output and find all compiled binaries.
167- /// Then execute each benchmark with the `list-benchmarks` command to find out its benchmark names.
168- fn discover_benchmarks ( cargo_stdout : & [ u8 ] ) -> anyhow:: Result < BenchmarkDatabase > {
169- let mut binaries = vec ! [ ] ;
170-
171- for message in Message :: parse_stream ( cargo_stdout) {
172- let message = message?;
173- match message {
174- Message :: CompilerArtifact ( artifact) => {
175- if let Some ( ref executable) = artifact. executable {
176- if artifact. target . kind . iter ( ) . any ( |k| k == "bin" ) {
177- let path = executable. as_std_path ( ) . to_path_buf ( ) ;
178- let benchmarks = gather_benchmarks ( & path) . map_err ( |err| {
179- anyhow:: anyhow!(
180- "Cannot gather benchmarks from `{}`: {err:?}" ,
181- path. display( )
182- )
183- } ) ?;
184- binaries. push ( BenchmarkBinary {
185- path,
186- benchmark_names : benchmarks,
187- } ) ;
188- }
189- }
190- }
191- _ => { }
192- }
193- }
194-
195- log:: debug!( "Found binaries: {:?}" , binaries) ;
196-
197- Ok ( BenchmarkDatabase { binaries } )
198- }
199-
200- /// Uses the `list-benchmarks` command from `benchlib` to find the benchmark names from the given
201- /// benchmark binary.
202- fn gather_benchmarks ( binary : & Path ) -> anyhow:: Result < Vec < String > > {
203- let output = Command :: new ( binary) . arg ( "list-benchmarks" ) . output ( ) ?;
204- Ok ( serde_json:: from_slice ( & output. stdout ) ?)
205- }
0 commit comments