Skip to content

Commit 8d7b9fd

Browse files
committed
Split runtime.rs
1 parent 4042bc1 commit 8d7b9fd

File tree

2 files changed

+103
-94
lines changed

2 files changed

+103
-94
lines changed

collector/src/runtime/benchmark.rs

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
use benchlib::benchmark::passes_filter;
2+
use cargo_metadata::Message;
3+
use core::option::Option;
4+
use core::option::Option::Some;
5+
use core::result::Result::Ok;
6+
use std::path::{Path, PathBuf};
7+
use std::process::Command;
8+
9+
#[derive(Debug)]
10+
pub struct BenchmarkBinary {
11+
pub path: PathBuf,
12+
pub benchmark_names: Vec<String>,
13+
}
14+
15+
impl BenchmarkBinary {
16+
pub fn name(&self) -> &str {
17+
self.path.file_name().unwrap().to_str().unwrap()
18+
}
19+
}
20+
21+
#[derive(Debug)]
22+
pub struct BenchmarkDatabase {
23+
pub binaries: Vec<BenchmarkBinary>,
24+
}
25+
26+
impl BenchmarkDatabase {
27+
pub fn total_benchmark_count(&self) -> u64 {
28+
self.benchmark_names().count() as u64
29+
}
30+
pub fn filtered_benchmark_count(&self, filter: &BenchmarkFilter) -> u64 {
31+
self.benchmark_names()
32+
.filter(|benchmark| {
33+
passes_filter(
34+
&benchmark,
35+
filter.exclude.as_deref(),
36+
filter.include.as_deref(),
37+
)
38+
})
39+
.count() as u64
40+
}
41+
42+
fn benchmark_names(&self) -> impl Iterator<Item = &str> {
43+
self.binaries
44+
.iter()
45+
.flat_map(|binary| binary.benchmark_names.iter().map(|n| n.as_ref()))
46+
}
47+
}
48+
49+
pub struct BenchmarkFilter {
50+
pub exclude: Option<String>,
51+
pub include: Option<String>,
52+
}
53+
54+
impl BenchmarkFilter {
55+
pub fn new(exclude: Option<String>, include: Option<String>) -> BenchmarkFilter {
56+
Self { exclude, include }
57+
}
58+
}
59+
60+
/// Parse Cargo JSON output and find all compiled binaries.
61+
/// Then execute each benchmark with the `list-benchmarks` command to find out its benchmark names.
62+
pub fn discover_benchmarks(cargo_stdout: &[u8]) -> anyhow::Result<BenchmarkDatabase> {
63+
let mut binaries = vec![];
64+
65+
for message in Message::parse_stream(cargo_stdout) {
66+
let message = message?;
67+
match message {
68+
Message::CompilerArtifact(artifact) => {
69+
if let Some(ref executable) = artifact.executable {
70+
if artifact.target.kind.iter().any(|k| k == "bin") {
71+
let path = executable.as_std_path().to_path_buf();
72+
let benchmarks = gather_benchmarks(&path).map_err(|err| {
73+
anyhow::anyhow!(
74+
"Cannot gather benchmarks from `{}`: {err:?}",
75+
path.display()
76+
)
77+
})?;
78+
binaries.push(BenchmarkBinary {
79+
path,
80+
benchmark_names: benchmarks,
81+
});
82+
}
83+
}
84+
}
85+
_ => {}
86+
}
87+
}
88+
89+
log::debug!("Found binaries: {:?}", binaries);
90+
91+
Ok(BenchmarkDatabase { binaries })
92+
}
93+
94+
/// Uses the `list-benchmarks` command from `benchlib` to find the benchmark names from the given
95+
/// benchmark binary.
96+
fn gather_benchmarks(binary: &Path) -> anyhow::Result<Vec<String>> {
97+
let output = Command::new(binary).arg("list-benchmarks").output()?;
98+
Ok(serde_json::from_slice(&output.stdout)?)
99+
}
Lines changed: 4 additions & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -1,62 +1,13 @@
1+
mod benchmark;
2+
13
use crate::benchmark::profile::Profile;
24
use crate::toolchain::{get_local_toolchain, LocalToolchain};
3-
use benchlib::benchmark::passes_filter;
45
use benchlib::comm::messages::BenchmarkMessage;
5-
use cargo_metadata::Message;
66
use std::io::{BufRead, BufReader};
77
use std::path::{Path, PathBuf};
88
use 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

Comments
 (0)