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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
|
#include "llvm/Support/Mustache.h"
#include "benchmark/benchmark.h"
#include "llvm/Support/JSON.h"
#include "llvm/Support/raw_ostream.h"
#include <string>
// A large, raw string with many characters that require HTML escaping.
static const std::string LongHtmlString = [] {
std::string S;
S.reserve(500000);
for (int i = 0; i < 50000; ++i) {
S += "<script>alert('xss');</script>";
}
return S;
}();
// A deep AND wide JSON object for testing traversal.
static const llvm::json::Value DeepJsonData = [] {
llvm::json::Value Root = llvm::json::Object();
llvm::json::Object *Current = Root.getAsObject();
for (int i = 0; i < 50; ++i) { // 50 levels deep
for (int j = 0; j < 100; ++j) {
(*Current)["sibling_" + std::to_string(j)] = llvm::json::Value("noise");
}
std::string Key = "level_" + std::to_string(i);
(*Current)[Key] = llvm::json::Object();
Current = (*Current)[Key].getAsObject();
}
(*Current)["final_value"] = llvm::json::Value("Success!");
llvm::json::Array Arr;
for (int i = 0; i < 5000; ++i) { // 5,000 iterations
Arr.push_back(llvm::json::Value(i));
}
llvm::json::Object NewRoot;
NewRoot["deep_data"] = std::move(Root);
NewRoot["loop_array"] = std::move(Arr);
return llvm::json::Value(std::move(NewRoot));
}();
// A huge array for testing iteration performance.
static const llvm::json::Value HugeArrayData = [] {
llvm::json::Array Arr;
for (int i = 0; i < 100000; ++i) { // 100,000 array items
Arr.push_back(llvm::json::Object(
{{"id", llvm::json::Value(static_cast<long long>(i))},
{"is_even", llvm::json::Value(i % 2 == 0)},
{"data", llvm::json::Value("Item data for " + std::to_string(i))}}));
}
return llvm::json::Object({{"items", std::move(Arr)}});
}();
// The main template that includes a partial within a loop.
static const std::string ComplexPartialTemplate =
"Header\n"
"{{#items}}{{> item_partial}}{{/items}}\n"
"Footer";
// The partial template is now more complex, rendering multiple fields and a
// conditional section.
static const std::string ItemPartialTemplate =
"<div class=\"item\" id=\"{{id}}\">\n"
" <p>{{data}}</p>\n"
" {{#is_even}}<span>(Even)</span>{{/is_even}}\n"
"</div>\n";
// A single large string to stress the output buffer.
static const llvm::json::Value LargeOutputData = llvm::json::Object({
{"long_string",
llvm::json::Value(std::string(1024 * 1024, 'A'))} // 1MB string
});
// --- Static Data (Templates) ---
static const std::string BulkEscapingTemplate = "{{content}}";
static const std::string BulkUnescapedTemplate = "{{{content}}}";
static const std::string BulkUnescapedAmpersandTemplate = "{{& content}}";
static const std::string DeepTraversalTemplate = [] {
std::string LongKey =
"deep_data.level_0.level_1.level_2.level_3.level_4.level_5."
"level_6.level_7.level_8.level_9."
"level_10.level_11.level_12.level_13.level_14.level_"
"15.level_16.level_17.level_18.level_19."
"level_20.level_21.level_22.level_23.level_24.level_"
"25.level_26.level_27.level_28.level_29."
"level_30.level_31.level_32.level_33.level_34.level_"
"35.level_36.level_37.level_38.level_39."
"level_40.level_41.level_42.level_43.level_44.level_"
"45.level_46.level_47.level_48.level_49.final_value";
return "{{#loop_array}}{{" + LongKey + "}}{{/loop_array}}";
}();
static const std::string DeeplyNestedRenderingTemplate = [] {
std::string NestedTemplate = "{{#deep_data}}";
for (int i = 0; i < 50; ++i) {
NestedTemplate += "{{#level_" + std::to_string(i) + "}}";
}
NestedTemplate += "{{final_value}}";
for (int i = 49; i >= 0; --i) {
NestedTemplate += "{{/level_" + std::to_string(i) + "}}";
}
NestedTemplate += "{{/deep_data}}";
return NestedTemplate;
}();
static const std::string HugeArrayIterationTemplate =
"{{#items}}ID: {{id}}.{{/items}}";
static const std::string ComplexTemplateParsingTemplate = [] {
std::string LargeTemplate;
LargeTemplate.reserve(100000);
for (int i = 0; i < 1000; ++i) {
LargeTemplate += "{{var_" + std::to_string(i) +
"}}"
"{{#section_" +
std::to_string(i) + "}}Content{{/section_" +
std::to_string(i) +
"}}"
"{{!comment_" +
std::to_string(i) +
"}}"
"{{=<% %>=}}"
"<%var_tag_changed_to_percent_sign_" +
std::to_string(i) +
"%>"
"<%={{ }}=%>"
"{{^inverted_" +
std::to_string(i) + "}}Not Present{{/inverted_" +
std::to_string(i) + "}}";
}
return LargeTemplate;
}();
static const std::string SmallTemplateParsingTemplate =
"{{level_0.sibling_99}}\n"
"{{level_0.level_1.level_2.level_3.level_4.level_5.sibling_50}}\n"
"{{level_0.level_1.level_2.level_3.level_4.level_5."
"level_6.level_7.level_8.level_9."
"level_10.level_11.level_12.level_13.level_14.level_"
"15.level_16.level_17.level_18.level_19."
"level_20.level_21.level_22.level_23.level_24.level_"
"25.level_26.level_27.level_28.level_29."
"level_30.level_31.level_32.level_33.level_34.level_"
"35.level_36.level_37.level_38.level_39."
"level_40.level_41.level_42.level_43.level_44.level_"
"45.level_46.level_47.level_48.level_49.final_value}}\n";
static const std::string LargeOutputStringTemplate = "{{long_string}}";
// Tests the performance of rendering a large string with various escaping
// syntaxes.
static void BM_Mustache_StringRendering(benchmark::State &state,
const std::string &TplStr) {
llvm::mustache::Template Tpl(TplStr);
llvm::json::Value Data =
llvm::json::Object({{"content", llvm::json::Value(LongHtmlString)}});
for (auto _ : state) {
std::string Result;
llvm::raw_string_ostream OS(Result);
Tpl.render(Data, OS);
benchmark::DoNotOptimize(Result);
}
}
BENCHMARK_CAPTURE(BM_Mustache_StringRendering, Escaped, BulkEscapingTemplate);
BENCHMARK_CAPTURE(BM_Mustache_StringRendering, Unescaped_Triple,
BulkUnescapedTemplate);
BENCHMARK_CAPTURE(BM_Mustache_StringRendering, Unescaped_Ampersand,
BulkUnescapedAmpersandTemplate);
// Tests the "hot render" cost of repeatedly traversing a deep and wide
// JSON object.
static void BM_Mustache_DeepTraversal(benchmark::State &state) {
llvm::mustache::Template Tpl(DeepTraversalTemplate);
for (auto _ : state) {
std::string Result;
llvm::raw_string_ostream OS(Result);
Tpl.render(DeepJsonData, OS);
benchmark::DoNotOptimize(Result);
}
}
BENCHMARK(BM_Mustache_DeepTraversal);
// Tests the "hot render" cost of pushing and popping a deep context stack.
static void BM_Mustache_DeeplyNestedRendering(benchmark::State &state) {
llvm::mustache::Template Tpl(DeeplyNestedRenderingTemplate);
for (auto _ : state) {
std::string Result;
llvm::raw_string_ostream OS(Result);
Tpl.render(DeepJsonData, OS);
benchmark::DoNotOptimize(Result);
}
}
BENCHMARK(BM_Mustache_DeeplyNestedRendering);
// Tests the performance of the loop logic when iterating over a huge number of
// items.
static void BM_Mustache_HugeArrayIteration(benchmark::State &state) {
llvm::mustache::Template Tpl(HugeArrayIterationTemplate);
for (auto _ : state) {
std::string Result;
llvm::raw_string_ostream OS(Result);
Tpl.render(HugeArrayData, OS);
benchmark::DoNotOptimize(Result);
}
}
BENCHMARK(BM_Mustache_HugeArrayIteration);
// Tests the performance of the parser on a large, "wide" template.
static void BM_Mustache_ComplexTemplateParsing(benchmark::State &state) {
for (auto _ : state) {
llvm::mustache::Template Tpl(ComplexTemplateParsingTemplate);
benchmark::DoNotOptimize(Tpl);
}
}
BENCHMARK(BM_Mustache_ComplexTemplateParsing);
// Tests the performance of the parser on a small, "deep" template.
static void BM_Mustache_SmallTemplateParsing(benchmark::State &state) {
for (auto _ : state) {
llvm::mustache::Template Tpl(SmallTemplateParsingTemplate);
benchmark::DoNotOptimize(Tpl);
}
}
BENCHMARK(BM_Mustache_SmallTemplateParsing);
// Tests the performance of rendering a template that includes a partial.
static void BM_Mustache_PartialsRendering(benchmark::State &state) {
llvm::mustache::Template Tpl(ComplexPartialTemplate);
Tpl.registerPartial("item_partial", ItemPartialTemplate);
llvm::json::Value Data = HugeArrayData;
for (auto _ : state) {
std::string Result;
llvm::raw_string_ostream OS(Result);
Tpl.render(Data, OS);
benchmark::DoNotOptimize(Result);
}
}
BENCHMARK(BM_Mustache_PartialsRendering);
// Tests the performance of the underlying buffer management when generating a
// very large output.
static void BM_Mustache_LargeOutputString(benchmark::State &state) {
llvm::mustache::Template Tpl(LargeOutputStringTemplate);
for (auto _ : state) {
std::string Result;
llvm::raw_string_ostream OS(Result);
Tpl.render(LargeOutputData, OS);
benchmark::DoNotOptimize(Result);
}
}
BENCHMARK(BM_Mustache_LargeOutputString);
BENCHMARK_MAIN();
|