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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
|
#[macro_use]
extern crate criterion;
#[macro_use]
extern crate serde_derive;
use criterion::Criterion;
use handlebars::{to_json, Context, Handlebars, Template};
use serde_json::value::Value as Json;
use std::collections::BTreeMap;
#[cfg(unix)]
use criterion::profiler::Profiler;
#[cfg(unix)]
use pprof::protos::Message;
#[cfg(unix)]
use pprof::ProfilerGuard;
#[cfg(unix)]
use std::fs::{create_dir_all, File};
#[cfg(unix)]
use std::io::Write;
#[cfg(unix)]
use std::path::Path;
#[cfg(unix)]
#[derive(Default)]
struct CpuProfiler<'a> {
guard: Option<ProfilerGuard<'a>>,
}
#[cfg(unix)]
impl<'a> Profiler for CpuProfiler<'a> {
fn start_profiling(&mut self, _benchmark_id: &str, benchmark_dir: &Path) {
create_dir_all(&benchmark_dir).unwrap();
let guard = ProfilerGuard::new(100).unwrap();
self.guard = Some(guard);
}
fn stop_profiling(&mut self, benchmark_id: &str, benchmark_dir: &Path) {
if let Ok(ref report) = self.guard.as_ref().unwrap().report().build() {
let fg_file_name = benchmark_dir.join(format!("{}.svg", benchmark_id));
let fg_file = File::create(fg_file_name).unwrap();
report.flamegraph(fg_file).unwrap();
let pb_file_name = benchmark_dir.join(format!("{}.pb", benchmark_id));
let mut pb_file = File::create(pb_file_name).unwrap();
let profile = report.pprof().unwrap();
let mut content = Vec::new();
profile.encode(&mut content).unwrap();
pb_file.write_all(&content).unwrap();
};
self.guard = None;
}
}
#[cfg(unix)]
fn profiled() -> Criterion {
Criterion::default().with_profiler(CpuProfiler::default())
}
#[derive(Serialize)]
struct DataWrapper {
v: String,
}
#[derive(Serialize)]
struct RowWrapper {
real: Vec<DataWrapper>,
dummy: Vec<DataWrapper>,
}
#[derive(Serialize)]
struct NestedRowWrapper {
parent: Vec<Vec<DataWrapper>>,
}
static SOURCE: &'static str = "<html>
<head>
<title>{{year}}</title>
</head>
<body>
<h1>CSL {{year}}</h1>
<ul>
{{#each teams}}
<li class=\"{{#if @first}}champion{{/if}}\">
<b>{{name}}</b>: {{score}}
</li>
{{/each}}
</ul>
</body>
</html>";
fn make_data() -> BTreeMap<String, Json> {
let mut data = BTreeMap::new();
data.insert("year".to_string(), to_json("2015"));
let mut teams = Vec::new();
for v in vec![
("Jiangsu", 43u16),
("Beijing", 27u16),
("Guangzhou", 22u16),
("Shandong", 12u16),
]
.iter()
{
let (name, score) = *v;
let mut t = BTreeMap::new();
t.insert("name".to_string(), to_json(name));
t.insert("score".to_string(), to_json(score));
teams.push(t)
}
data.insert("teams".to_string(), to_json(&teams));
data
}
fn parse_template(c: &mut Criterion) {
c.bench_function("parse_template", move |b| {
b.iter(|| Template::compile(SOURCE).ok().unwrap())
});
}
fn render_template(c: &mut Criterion) {
let mut handlebars = Handlebars::new();
handlebars
.register_template_string("table", SOURCE)
.ok()
.expect("Invalid template format");
let ctx = Context::wraps(make_data()).unwrap();
c.bench_function("render_template", move |b| {
b.iter(|| handlebars.render_with_context("table", &ctx).ok().unwrap())
});
}
fn large_loop_helper(c: &mut Criterion) {
let mut handlebars = Handlebars::new();
handlebars
.register_template_string("test", "BEFORE\n{{#each real}}{{this.v}}{{/each}}AFTER")
.ok()
.expect("Invalid template format");
let real: Vec<DataWrapper> = (1..1000)
.map(|i| DataWrapper {
v: format!("n={}", i),
})
.collect();
let dummy: Vec<DataWrapper> = (1..1000)
.map(|i| DataWrapper {
v: format!("n={}", i),
})
.collect();
let rows = RowWrapper { real, dummy };
let ctx = Context::wraps(&rows).unwrap();
c.bench_function("large_loop_helper", move |b| {
b.iter(|| handlebars.render_with_context("test", &ctx).ok().unwrap())
});
}
fn large_loop_helper_with_context_creation(c: &mut Criterion) {
let mut handlebars = Handlebars::new();
handlebars
.register_template_string("test", "BEFORE\n{{#each real}}{{this.v}}{{/each}}AFTER")
.ok()
.expect("Invalid template format");
let real: Vec<DataWrapper> = (1..1000)
.map(|i| DataWrapper {
v: format!("n={}", i),
})
.collect();
let dummy: Vec<DataWrapper> = (1..1000)
.map(|i| DataWrapper {
v: format!("n={}", i),
})
.collect();
let rows = RowWrapper { real, dummy };
c.bench_function("large_loop_helper_with_context_creation", move |b| {
b.iter(|| handlebars.render("test", &rows).ok().unwrap())
});
}
fn large_nested_loop(c: &mut Criterion) {
let mut handlebars = Handlebars::new();
handlebars
.register_template_string(
"test",
"BEFORE\n{{#each parent as |child|}}{{#each child}}{{this.v}}{{/each}}{{/each}}AFTER",
)
.ok()
.expect("Invalid template format");
let parent: Vec<Vec<DataWrapper>> = (1..100)
.map(|_| {
(1..10)
.map(|v| DataWrapper {
v: format!("v={}", v),
})
.collect()
})
.collect();
let rows = NestedRowWrapper { parent };
let ctx = Context::wraps(&rows).unwrap();
c.bench_function("large_nested_loop", move |b| {
b.iter(|| handlebars.render_with_context("test", &ctx).ok().unwrap())
});
}
#[cfg(unix)]
criterion_group!(
name = benches;
config = profiled();
targets = parse_template, render_template, large_loop_helper, large_loop_helper_with_context_creation,
large_nested_loop
);
#[cfg(not(unix))]
criterion_group!(
benches,
parse_template,
render_template,
large_loop_helper,
large_loop_helper_with_context_creation,
large_nested_loop
);
criterion_main!(benches);
|