Skip to content

Commit dc18aae

Browse files
library: Add a very simple microbenchmark for splitn
1 parent 3bc767e commit dc18aae

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

library/coretests/benches/pattern.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,3 +39,27 @@ fn ends_with_str(b: &mut Bencher) {
3939
}
4040
})
4141
}
42+
43+
#[bench]
44+
fn splitn_on_http_response(b: &mut Bencher) {
45+
fn parse_http(s: &str) -> Result<(&str, &str, &str), &str> {
46+
let mut parts = s.splitn(3, ' ');
47+
let version = parts.next().ok_or("No version")?;
48+
let code = parts.next().ok_or("No status code")?;
49+
let description = parts.next().ok_or("No description")?;
50+
Ok((version, code, description))
51+
}
52+
53+
let response = String::from("HTTP/1.1 418 I'm a teapot\r\n");
54+
let mut res: (&str, &str, &str) = ("", "", "");
55+
b.iter(|| {
56+
for _ in 0..1024 {
57+
res = black_box(match parse_http(black_box(&response)) {
58+
Ok(data) => data,
59+
Err(_) => {
60+
continue;
61+
}
62+
})
63+
}
64+
})
65+
}

0 commit comments

Comments
 (0)