Skip to content

Commit 6ff0cdd

Browse files
committed
feat: expose a collection entrypoint usable within walltime integrations
1 parent 41e5994 commit 6ff0cdd

File tree

5 files changed

+84
-9
lines changed

5 files changed

+84
-9
lines changed

Cargo.lock

Lines changed: 10 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,7 @@ members = [
88
"crates/divan_compat/macros",
99
]
1010
resolver = "2"
11+
12+
[workspace.dependencies]
13+
serde = { version = "1.0.217", features = ["derive"] }
14+
serde_json = "1.0.138"

crates/codspeed/Cargo.toml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@ keywords = ["codspeed", "benchmark"]
2020
[dependencies]
2121
colored = "2.0.0"
2222
libc = "^0.2"
23-
serde_json = "1.0.120"
23+
serde = { workspace = true }
24+
serde_json = { workspace = true }
25+
uuid = { version = "1.12.1", features = ["v4"] }
2426

2527
[[bench]]
2628
name = "native"

crates/codspeed/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@ mod macros;
33
mod measurement;
44
mod request;
55
pub mod utils;
6+
pub mod walltime;

crates/codspeed/src/walltime.rs

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
use std::{
2+
io::Write,
3+
path::{Path, PathBuf},
4+
};
5+
6+
use serde::{Deserialize, Serialize};
7+
8+
#[derive(Debug, Serialize, Deserialize)]
9+
pub struct BenchmarkMetadata {
10+
pub name: String,
11+
pub uri: String,
12+
}
13+
14+
#[derive(Debug, Serialize, Deserialize)]
15+
pub struct RawWallTimeData {
16+
#[serde(flatten)]
17+
pub metadata: BenchmarkMetadata,
18+
pub iter_per_round: u32,
19+
pub times_ps: Vec<u128>,
20+
}
21+
22+
impl RawWallTimeData {
23+
fn from_runtime_data(
24+
name: String,
25+
uri: String,
26+
iter_per_round: u32,
27+
times_ps: Vec<u128>,
28+
) -> Self {
29+
RawWallTimeData {
30+
metadata: BenchmarkMetadata { name, uri },
31+
iter_per_round,
32+
times_ps,
33+
}
34+
}
35+
36+
fn dump_to_results(&self, workspace_root: &Path, scope: &str) {
37+
let output_dir = workspace_root
38+
.join("target")
39+
.join("codspeed")
40+
.join("raw_results")
41+
.join(scope);
42+
std::fs::create_dir_all(&output_dir).unwrap();
43+
let bench_id = uuid::Uuid::new_v4().to_string();
44+
let output_path = output_dir.join(format!("{}.json", bench_id));
45+
let mut writer = std::fs::File::create(&output_path).expect("Failed to create the file");
46+
serde_json::to_writer_pretty(&mut writer, self).expect("Failed to write the data");
47+
writer.flush().expect("Failed to flush the writer");
48+
}
49+
}
50+
51+
/// Entry point called in patched integration to harvest raw walltime data
52+
pub fn collect_raw_walltime_results(
53+
scope: &str,
54+
name: String,
55+
uri: String,
56+
iter_per_round: u32,
57+
times_ps: Vec<u128>,
58+
) {
59+
let workspace_root = std::env::var("CODSPEED_CARGO_WORKSPACE_ROOT").map(PathBuf::from);
60+
let Ok(workspace_root) = workspace_root else {
61+
eprintln!("codspeed failed to get workspace root. skipping");
62+
return;
63+
};
64+
let data = RawWallTimeData::from_runtime_data(name, uri, iter_per_round, times_ps);
65+
data.dump_to_results(&workspace_root, scope);
66+
}

0 commit comments

Comments
 (0)