Skip to content

Commit 34d0ea6

Browse files
committed
chore: add parse pr bench
1 parent 1531eea commit 34d0ea6

File tree

2 files changed

+98
-0
lines changed

2 files changed

+98
-0
lines changed

crates/divan_compat/examples/Cargo.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,3 +44,7 @@ harness = false
4444
[[bench]]
4545
name = "counters"
4646
harness = false
47+
48+
[[bench]]
49+
name = "parse_pr"
50+
harness = false
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
//! Parse PR benchmark - equivalent of the TypeScript version
2+
3+
use divan::black_box;
4+
5+
fn main() {
6+
divan::main();
7+
}
8+
9+
#[derive(Clone)]
10+
struct PullRequest {
11+
#[allow(dead_code)]
12+
number: u32,
13+
title: String,
14+
body: String,
15+
}
16+
17+
fn send_event(number_of_operations: usize) {
18+
for i in 0..number_of_operations {
19+
let mut a = i;
20+
a = a + 1;
21+
let _ = black_box(a);
22+
}
23+
}
24+
25+
fn log_metrics(number_of_operations: usize, number_of_deep_operations: usize) {
26+
for _ in 0..number_of_operations {
27+
for j in 0..number_of_operations {
28+
let mut a = j;
29+
a = a + 1;
30+
a = a + 1;
31+
let _ = black_box(a);
32+
}
33+
send_event(number_of_deep_operations);
34+
}
35+
}
36+
37+
fn parse_title(title: &str) {
38+
log_metrics(10, 10);
39+
modify_title(title);
40+
}
41+
42+
fn modify_title(title: &str) {
43+
for i in 0..100 {
44+
let mut a = i;
45+
a = a + 1 + title.len();
46+
let _ = black_box(a);
47+
}
48+
}
49+
50+
fn prepare_parsing_body(body: &str) {
51+
for i in 0..100 {
52+
let mut a = i;
53+
a = a + 1;
54+
let _ = black_box(a);
55+
}
56+
parse_body(body);
57+
}
58+
59+
fn parse_body(body: &str) {
60+
log_metrics(10, 10);
61+
for i in 0..200 {
62+
let mut a = i;
63+
a = a + 1;
64+
let _ = black_box(a);
65+
}
66+
parse_issue_fixed(body);
67+
}
68+
69+
fn parse_issue_fixed(body: &str) -> Option<u32> {
70+
const PREFIX: &str = "fixes #";
71+
let index = body.find(PREFIX)?;
72+
73+
let start = index + PREFIX.len();
74+
let mut end = start;
75+
while end < body.len() && body[end..=end].chars().next().unwrap().is_ascii_digit() {
76+
end += 1;
77+
}
78+
body[start..end].parse::<u32>().ok()
79+
}
80+
81+
fn parse_pr(pull_request: &PullRequest) {
82+
parse_title(&pull_request.title);
83+
prepare_parsing_body(&pull_request.body);
84+
}
85+
86+
#[divan::bench]
87+
fn bench_parse_pr() {
88+
let pr = black_box(PullRequest {
89+
number: 123,
90+
title: "Add new feature".to_string(),
91+
body: "This PR adds a new feature.\n\nfixes #42\n\nDetails here.".to_string(),
92+
});
93+
parse_pr(&pr);
94+
}

0 commit comments

Comments
 (0)