1
+ use crate :: toolchain:: LocalToolchain ;
1
2
use benchlib:: benchmark:: passes_filter;
2
3
use cargo_metadata:: Message ;
3
4
use core:: option:: Option ;
4
5
use core:: option:: Option :: Some ;
5
6
use core:: result:: Result :: Ok ;
7
+ use std:: io:: BufReader ;
6
8
use std:: path:: { Path , PathBuf } ;
7
- use std:: process:: Command ;
9
+ use std:: process:: { Child , Command , Stdio } ;
8
10
9
11
/// A binary that defines several benchmarks using the `run_benchmark_group` function from
10
12
/// `benchlib`.
@@ -65,10 +67,17 @@ impl BenchmarkFilter {
65
67
/// We assume that each binary defines a benchmark suite using `benchlib`.
66
68
/// We then execute each benchmark suite with the `list-benchmarks` command to find out its
67
69
/// benchmark names.
68
- pub fn discover_benchmarks ( cargo_stdout : & [ u8 ] ) -> anyhow:: Result < BenchmarkSuite > {
70
+ pub fn discover_benchmarks (
71
+ toolchain : & LocalToolchain ,
72
+ dir : & Path ,
73
+ ) -> anyhow:: Result < BenchmarkSuite > {
74
+ log:: info!( "Compiling runtime benchmarks" ) ;
75
+ let mut child = start_runtime_benchmarks_compilation ( toolchain, dir) ?;
76
+
69
77
let mut groups = vec ! [ ] ;
70
78
71
- for message in Message :: parse_stream ( cargo_stdout) {
79
+ let stream = BufReader :: new ( child. stdout . take ( ) . unwrap ( ) ) ;
80
+ for message in Message :: parse_stream ( stream) {
72
81
let message = message?;
73
82
match message {
74
83
Message :: CompilerArtifact ( artifact) => {
@@ -81,23 +90,57 @@ pub fn discover_benchmarks(cargo_stdout: &[u8]) -> anyhow::Result<BenchmarkSuite
81
90
path. display( )
82
91
)
83
92
} ) ?;
93
+ log:: info!( "Compiled {}" , path. display( ) ) ;
84
94
groups. push ( BenchmarkGroup {
85
95
binary : path,
86
96
benchmark_names : benchmarks,
87
97
} ) ;
88
98
}
89
99
}
90
100
}
91
- _ => { }
101
+ Message :: TextLine ( line) => println ! ( "{}" , line) ,
102
+ Message :: CompilerMessage ( msg) => {
103
+ print ! ( "{}" , msg. message. rendered. unwrap_or( msg. message. message) )
104
+ }
105
+ _ => {
106
+ log:: debug!( "Cargo metadata output: {:?}" , message) ;
107
+ }
92
108
}
93
109
}
94
110
111
+ let output = child. wait ( ) ?;
112
+ if output. success ( ) {
113
+ log:: info!( "Successfully compiled runtime benchmarks" ) ;
114
+ } else {
115
+ return Err ( anyhow:: anyhow!( "Failed to compile runtime benchmarks" ) ) ;
116
+ }
117
+
95
118
groups. sort_unstable_by ( |a, b| a. binary . cmp ( & b. binary ) ) ;
96
119
log:: debug!( "Found binaries: {:?}" , groups) ;
97
120
98
121
Ok ( BenchmarkSuite { groups } )
99
122
}
100
123
124
+ /// Compiles all runtime benchmark binaries (groups) and returns the stdout output stream of Cargo.
125
+ fn start_runtime_benchmarks_compilation (
126
+ toolchain : & LocalToolchain ,
127
+ dir : & Path ,
128
+ ) -> anyhow:: Result < Child > {
129
+ let child = Command :: new ( & toolchain. cargo )
130
+ . env ( "RUSTC" , & toolchain. rustc )
131
+ . arg ( "build" )
132
+ . arg ( "--release" )
133
+ . arg ( "--message-format" )
134
+ . arg ( "json-diagnostic-rendered-ansi" )
135
+ . current_dir ( dir)
136
+ . stdin ( Stdio :: null ( ) )
137
+ . stdout ( Stdio :: piped ( ) )
138
+ . stderr ( Stdio :: null ( ) )
139
+ . spawn ( )
140
+ . map_err ( |error| anyhow:: anyhow!( "Failed to start cargo: {:?}" , error) ) ?;
141
+ Ok ( child)
142
+ }
143
+
101
144
/// Uses a command from `benchlib` to find the benchmark names from the given
102
145
/// benchmark binary.
103
146
fn gather_benchmarks ( binary : & Path ) -> anyhow:: Result < Vec < String > > {
0 commit comments