Skip to content

Commit c290bab

Browse files
committed
context
1 parent f60a475 commit c290bab

File tree

7 files changed

+89
-54
lines changed

7 files changed

+89
-54
lines changed

β€Žlib/bencher_context/src/client/mod.rsβ€Ž

Lines changed: 53 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@ use std::collections::HashMap;
22

33
use gix::Repository;
44

5-
mod fingerprint;
5+
mod platform;
66

7-
pub use fingerprint::Fingerprint;
7+
pub use platform::Fingerprint;
8+
use platform::OperatingSystem;
89

910
use crate::ReportContext;
1011

@@ -15,7 +16,8 @@ impl ReportContext {
1516
get_context()
1617
}
1718

18-
fn insert(&mut self, key: String, value: String) -> Option<String> {
19+
fn insert(&mut self, path: &str, value: String) -> Option<String> {
20+
let key = format!("bencher.dev{path}");
1921
self.0.insert(key, value)
2022
}
2123
}
@@ -27,62 +29,73 @@ impl From<ReportContext> for HashMap<String, String> {
2729
}
2830
}
2931

30-
pub fn find_repo() -> Option<Repository> {
31-
let current_dir = std::env::current_dir().ok()?;
32-
for directory in current_dir.ancestors() {
33-
if let Ok(repo) = gix::open(directory) {
34-
return Some(repo);
35-
}
36-
}
37-
None
38-
}
39-
4032
pub fn get_context() -> ReportContext {
4133
let mut context = ReportContext::default();
34+
git_context(&mut context);
35+
platform_context(&mut context);
36+
context
37+
}
38+
39+
fn git_context(context: &mut ReportContext) {
40+
let Some(repo) = find_repo() else {
41+
return;
42+
};
4243

43-
let repo = find_repo();
44-
let repo_name = repo_name(repo.as_ref());
45-
if let Some(repo_name) = repo_name {
46-
context.insert("bencher.dev/repo/name".to_owned(), repo_name);
44+
if let Some(repo_name) = repo_name(&repo) {
45+
context.insert("/repo/name", repo_name);
4746
}
48-
if let Some(repo) = &repo {
49-
if let Some(root_commit) = find_default_branch_and_root_commit(repo) {
50-
context.insert("bencher.dev/repo/hash".to_owned(), root_commit);
51-
}
47+
48+
if let Some(root_commit) = repo_hash(&repo) {
49+
context.insert("/repo/hash", root_commit);
5250
}
5351

54-
if let Some(repo) = &repo {
55-
if let Some((branch_ref, branch_ref_name)) = current_branch_name(repo) {
56-
context.insert("bencher.dev/branch/ref".to_owned(), branch_ref);
57-
context.insert("bencher.dev/branch/ref/name".to_owned(), branch_ref_name);
58-
}
52+
if let Some((branch_ref, branch_ref_name)) = branch_ref(&repo) {
53+
context.insert("/branch/ref", branch_ref);
54+
context.insert("/branch/ref/name", branch_ref_name);
55+
}
5956

60-
if let Some(hash) = current_branch_hash(repo) {
61-
context.insert("bencher.dev/branch/hash".to_owned(), hash);
62-
}
57+
if let Some(hash) = branch_hash(&repo) {
58+
context.insert("/branch/hash", hash);
6359
}
60+
}
61+
62+
fn platform_context(context: &mut ReportContext) {
63+
context.insert("/testbed/os", OperatingSystem::current().to_string());
6464

65-
let fingerprint = Fingerprint::new();
66-
if let Some(fingerprint) = fingerprint {
67-
context.insert(
68-
"bencher.dev/testbed/fingerprint".to_owned(),
69-
fingerprint.to_string(),
70-
);
65+
if let Some(fingerprint) = Fingerprint::current() {
66+
context.insert("/testbed/fingerprint", fingerprint.to_string());
7167
}
68+
}
7269

73-
context
70+
fn find_repo() -> Option<Repository> {
71+
let current_dir = std::env::current_dir().ok()?;
72+
for directory in current_dir.ancestors() {
73+
if let Ok(repo) = gix::open(directory) {
74+
return Some(repo);
75+
}
76+
}
77+
None
7478
}
7579

76-
fn repo_name(repo: Option<&Repository>) -> Option<String> {
77-
let repo = repo?;
80+
fn repo_name(repo: &Repository) -> Option<String> {
7881
let Some(parent) = repo.path().parent() else {
7982
return Some(ROOT.to_owned());
8083
};
8184
let file_name = parent.file_name()?;
8285
file_name.to_str().map(ToOwned::to_owned)
8386
}
8487

85-
fn current_branch_name(repo: &Repository) -> Option<(String, String)> {
88+
fn repo_hash(repo: &Repository) -> Option<String> {
89+
let head_id = repo.head_id().ok()?;
90+
let rev_walk = repo.rev_walk([head_id]).all().ok()?;
91+
if let Some(Ok(commit)) = rev_walk.last() {
92+
Some(commit.id().object().ok()?.id.to_string())
93+
} else {
94+
None
95+
}
96+
}
97+
98+
fn branch_ref(repo: &Repository) -> Option<(String, String)> {
8699
repo.head().ok()?.referent_name().map(|name| {
87100
(
88101
String::from_utf8_lossy(name.as_bstr()).to_string(),
@@ -91,18 +104,8 @@ fn current_branch_name(repo: &Repository) -> Option<(String, String)> {
91104
})
92105
}
93106

94-
fn current_branch_hash(repo: &Repository) -> Option<String> {
107+
fn branch_hash(repo: &Repository) -> Option<String> {
95108
let head_id = repo.head_id().ok()?;
96109
let head_object = head_id.object().ok()?;
97110
Some(head_object.id.to_string())
98111
}
99-
100-
fn find_default_branch_and_root_commit(repo: &Repository) -> Option<String> {
101-
let head_id = repo.head_id().ok()?;
102-
let rev_walk = repo.rev_walk([head_id]).all().ok()?;
103-
if let Some(Ok(commit)) = rev_walk.last() {
104-
Some(commit.id().object().ok()?.id.to_string())
105-
} else {
106-
None
107-
}
108-
}

β€Žlib/bencher_context/src/client/fingerprint/mod.rsβ€Ž renamed to β€Žlib/bencher_context/src/client/platform/mod.rsβ€Ž

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,11 @@ mod target_os;
55

66
#[derive(Debug, Display, Clone, Copy)]
77
pub struct Fingerprint(Uuid);
8+
9+
#[allow(dead_code)]
10+
#[derive(Debug, Display, Clone, Copy)]
11+
pub enum OperatingSystem {
12+
Linux,
13+
Macos,
14+
Windows,
15+
}
Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@ use std::fs;
22

33
use uuid::Uuid;
44

5+
use crate::client::platform::OperatingSystem;
6+
57
const HEX_BASE: u32 = 16;
68

79
impl crate::Fingerprint {
8-
pub fn new() -> Option<Self> {
10+
pub fn current() -> Option<Self> {
911
parse_machine_id("/var/lib/dbus/machine-id")
1012
.or_else(|| parse_machine_id("/etc/machine-id"))
1113
.map(Self)
@@ -18,3 +20,9 @@ fn parse_machine_id(path: &str) -> Option<Uuid> {
1820
.and_then(|id| u128::from_str_radix(id.trim(), HEX_BASE).ok())
1921
.map(Uuid::from_u128)
2022
}
23+
24+
impl OperatingSystem {
25+
pub fn current() -> Self {
26+
Self::Linux
27+
}
28+
}
Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@ use std::process::Command;
22

33
use uuid::Uuid;
44

5+
use crate::client::platform::OperatingSystem;
6+
57
impl crate::Fingerprint {
6-
pub fn new() -> Option<Self> {
8+
pub fn current() -> Option<Self> {
79
Command::new("ioreg")
810
.arg("-d2")
911
.arg("-c")
@@ -24,3 +26,9 @@ impl crate::Fingerprint {
2426
.map(Self)
2527
}
2628
}
29+
30+
impl OperatingSystem {
31+
pub fn current() -> Self {
32+
Self::Macos
33+
}
34+
}
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,19 @@
11
use uuid::Uuid;
22

3+
use crate::client::platform::OperatingSystem;
4+
35
impl crate::Fingerprint {
4-
pub fn new() -> Option<Self> {
6+
pub fn current() -> Option<Self> {
57
windows::System::Profile::SystemManufacturers::SmbiosInformation::SerialNumber()
68
.ok()
79
.as_ref()
810
.and_then(|uuid| Uuid::parse_str(&uuid.to_string().trim()).ok())
911
.map(Self)
1012
}
1113
}
14+
15+
impl OperatingSystem {
16+
pub fn current() -> Self {
17+
Self::Windows
18+
}
19+
}

β€Žservices/cli/build.rsβ€Ž

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
fn main() {
22
// This is here to test that fingerprinting works on all supported operating systems
3-
let _ = bencher_context::Fingerprint::new();
3+
let _ = bencher_context::Fingerprint::current();
44
}

0 commit comments

Comments
Β (0)