-
Notifications
You must be signed in to change notification settings - Fork 6
/
bench.rs
117 lines (104 loc) · 3.54 KB
/
bench.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
use criterion::{black_box, criterion_group, criterion_main, Criterion};
use fluent_uri::{component::Scheme, encoding::EStr, Iri, Uri, UriRef};
use iri_string::{
build::Builder,
format::ToDedicatedString,
types::{IriStr, UriAbsoluteStr, UriReferenceStr, UriStr},
};
use url::Url;
criterion_group!(
benches,
bench_parse,
bench_parse_iref,
bench_parse_iri_string,
bench_parse_oxiri,
bench_parse_url,
bench_build,
bench_build_iri_string,
bench_normalize,
bench_normalize_iri_string,
bench_resolve,
bench_resolve_iri_string,
);
criterion_main!(benches);
const PARSE_CASE: &str = "https://[email protected]/search?q=%E6%B5%8B%E8%AF%95#fragment";
const NORMALIZE_CASE: &str = "eXAMPLE://a/./b/../b/%63/%7bfoo%7d";
const RESOLVE_CASE_BASE: &str = "http://example.com/foo/bar";
const RESOLVE_CASE_REF: &str = "../baz";
fn bench_parse(c: &mut Criterion) {
c.bench_function("parse", |b| b.iter(|| Iri::parse(black_box(PARSE_CASE))));
}
fn bench_parse_iref(c: &mut Criterion) {
c.bench_function("parse_iref", |b| {
b.iter(|| iref::Iri::new(black_box(PARSE_CASE)))
});
}
fn bench_parse_iri_string(c: &mut Criterion) {
c.bench_function("parse_iri_string", |b| {
b.iter(|| IriStr::new(black_box(PARSE_CASE)))
});
}
fn bench_parse_oxiri(c: &mut Criterion) {
c.bench_function("parse_oxiri", |b| {
b.iter(|| oxiri::Iri::parse(black_box(PARSE_CASE)))
});
}
fn bench_parse_url(c: &mut Criterion) {
c.bench_function("parse_url", |b| {
b.iter(|| Url::parse(black_box(PARSE_CASE)))
});
}
fn bench_build(c: &mut Criterion) {
c.bench_function("build", |b| {
b.iter(|| {
Uri::builder()
.scheme(Scheme::new_or_panic("foo"))
.authority_with(|b| {
b.userinfo(EStr::new_or_panic("user"))
.host(EStr::new_or_panic("example.com"))
.port(8042)
})
.path(EStr::new_or_panic("/over/there"))
.query(EStr::new_or_panic("name=ferret"))
.fragment(EStr::new_or_panic("nose"))
.build()
})
});
}
fn bench_build_iri_string(c: &mut Criterion) {
c.bench_function("build_iri_string", |b| {
b.iter(|| {
let mut builder = Builder::new();
builder.scheme("foo");
builder.userinfo("user");
builder.host("example.com");
builder.port(8042u16);
builder.path("/over/there");
builder.query("name=ferret");
builder.fragment("nose");
builder.build::<UriStr>().unwrap().to_dedicated_string()
})
});
}
fn bench_normalize(c: &mut Criterion) {
let r = UriRef::parse(NORMALIZE_CASE).unwrap();
c.bench_function("normalize", |b| b.iter(|| r.normalize()));
}
fn bench_normalize_iri_string(c: &mut Criterion) {
let r = UriStr::new(NORMALIZE_CASE).unwrap();
c.bench_function("normalize_iri_string", |b| {
b.iter(|| r.normalize().to_dedicated_string())
});
}
fn bench_resolve(c: &mut Criterion) {
let base = Uri::parse(RESOLVE_CASE_BASE).unwrap();
let r = UriRef::parse(RESOLVE_CASE_REF).unwrap();
c.bench_function("resolve", |b| b.iter(|| r.resolve_against(&base)));
}
fn bench_resolve_iri_string(c: &mut Criterion) {
let base = UriAbsoluteStr::new(RESOLVE_CASE_BASE).unwrap();
let r = UriReferenceStr::new(RESOLVE_CASE_REF).unwrap();
c.bench_function("resolve_iri_string", |b| {
b.iter(|| r.resolve_against(base).to_dedicated_string())
});
}