forked from Taritsyn/JavaScriptEngineSwitcher
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJsExecutionHeavyBenchmark.cs
More file actions
310 lines (268 loc) · 7.38 KB
/
JsExecutionHeavyBenchmark.cs
File metadata and controls
310 lines (268 loc) · 7.38 KB
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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
using System;
using System.IO;
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Diagnosers;
using BenchmarkDotNet.Order;
using JavaScriptEngineSwitcher.ChakraCore;
using JavaScriptEngineSwitcher.Core;
using JavaScriptEngineSwitcher.Jint;
using JavaScriptEngineSwitcher.Jurassic;
using JavaScriptEngineSwitcher.Msie;
#if NIL_JS
using JavaScriptEngineSwitcher.NiL;
#endif
using JavaScriptEngineSwitcher.Node;
using JavaScriptEngineSwitcher.V8;
using JavaScriptEngineSwitcher.Vroom;
using JavaScriptEngineSwitcher.Yantra;
namespace JavaScriptEngineSwitcher.Benchmarks
{
[MemoryDiagnoser]
[Orderer(SummaryOrderPolicy.Method, MethodOrderPolicy.Declared)]
public class JsExecutionHeavyBenchmark
{
/// <summary>
/// Name of the file containing library for template rendering
/// </summary>
private const string LibraryFileName = "bundle.min.js";
/// <summary>
/// Name of template rendering function
/// </summary>
private const string FunctionName = "renderTemplate";
/// <summary>
/// Code of library for template rendering
/// </summary>
private static string _libraryCode;
/// <summary>
/// List of items
/// </summary>
private static ContentItem[] _contentItems = [
new ContentItem("hello-world"),
new ContentItem("contacts"),
new ContentItem("js-engines"),
new ContentItem("web-browser-family-tree")
];
/// <summary>
/// Static constructor
/// </summary>
static JsExecutionHeavyBenchmark()
{
PopulateTestData();
}
public static string GetAbsoluteDirectoryPath(string directoryPath)
{
string baseDirectoryPath = AppDomain.CurrentDomain.BaseDirectory;
string absoluteDirectoryPath = Path.GetFullPath(Path.Combine(baseDirectoryPath, directoryPath));
#if NETCOREAPP
if (!Directory.Exists(absoluteDirectoryPath))
{
absoluteDirectoryPath = Path.GetFullPath(
Path.Combine(baseDirectoryPath, "../../../../", directoryPath));
}
#endif
return absoluteDirectoryPath;
}
/// <summary>
/// Populates a test data
/// </summary>
public static void PopulateTestData()
{
string filesDirectoryPath = GetAbsoluteDirectoryPath("Files/template-rendering");
string librariesDirectoryPath = Path.Combine(filesDirectoryPath, "lib");
string contentDirectoryPath = Path.Combine(filesDirectoryPath, "content");
_libraryCode = File.ReadAllText(Path.Combine(librariesDirectoryPath, LibraryFileName));
foreach (ContentItem item in _contentItems)
{
string itemDirectoryPath = Path.Combine(contentDirectoryPath, item.Name);
item.TemplateCode = File.ReadAllText(Path.Combine(itemDirectoryPath, "template.handlebars"));
item.SerializedData = File.ReadAllText(Path.Combine(itemDirectoryPath, "data.json"));
item.TargetOutput = File.ReadAllText(Path.Combine(itemDirectoryPath, "target-output.html"));
}
}
/// <summary>
/// Render a templates
/// </summary>
/// <param name="createJsEngine">Delegate for create an instance of the JS engine</param>
/// <param name="withPrecompilation">Flag for whether to allow execution of JS code with pre-compilation</param>
private static void RenderTemplates(Func<IJsEngine> createJsEngine, bool withPrecompilation)
{
// Arrange
IPrecompiledScript precompiledCode = null;
// Act
using (var jsEngine = createJsEngine())
{
if (withPrecompilation)
{
if (!jsEngine.SupportsScriptPrecompilation)
{
throw new NotSupportedException($"{jsEngine.Name} does not support precompilation.");
}
precompiledCode = jsEngine.Precompile(_libraryCode, LibraryFileName);
jsEngine.Execute(precompiledCode);
}
else
{
jsEngine.Execute(_libraryCode, LibraryFileName);
}
_contentItems[0].Output = jsEngine.CallFunction<string>(FunctionName, _contentItems[0].TemplateCode,
_contentItems[0].SerializedData);
}
for (int itemIndex = 1; itemIndex < _contentItems.Length; itemIndex++)
{
using (var jsEngine = createJsEngine())
{
if (withPrecompilation)
{
jsEngine.Execute(precompiledCode);
}
else
{
jsEngine.Execute(_libraryCode, LibraryFileName);
}
_contentItems[itemIndex].Output = jsEngine.CallFunction<string>(FunctionName,
_contentItems[itemIndex].TemplateCode, _contentItems[itemIndex].SerializedData);
}
}
// Assert
foreach (ContentItem item in _contentItems)
{
Assert.Equal(item.TargetOutput, item.Output, true);
}
}
[Benchmark]
[Arguments(false)]
[Arguments(true)]
public void ChakraCore(bool withPrecompilation)
{
Func<IJsEngine> createJsEngine = () => new ChakraCoreJsEngine();
RenderTemplates(createJsEngine, withPrecompilation);
}
[Benchmark]
[Arguments(false)]
[Arguments(true)]
public void Jint(bool withPrecompilation)
{
Func<IJsEngine> createJsEngine = () => new JintJsEngine();
RenderTemplates(createJsEngine, withPrecompilation);
}
[Benchmark]
[Arguments(false)]
[Arguments(true)]
public void Jurassic(bool withPrecompilation)
{
Func<IJsEngine> createJsEngine = () => new JurassicJsEngine();
RenderTemplates(createJsEngine, withPrecompilation);
}
#if NET462
[Benchmark]
public void MsieClassic()
{
Func<IJsEngine> createJsEngine = () => new MsieJsEngine(new MsieSettings
{
EngineMode = JsEngineMode.Classic,
UseJson2Library = true
});
RenderTemplates(createJsEngine, false);
}
[Benchmark]
public void MsieChakraActiveScript()
{
Func<IJsEngine> createJsEngine = () => new MsieJsEngine(new MsieSettings
{
EngineMode = JsEngineMode.ChakraActiveScript
});
RenderTemplates(createJsEngine, false);
}
#endif
[Benchmark]
[Arguments(false)]
[Arguments(true)]
public void MsieChakraIeJsRt(bool withPrecompilation)
{
Func<IJsEngine> createJsEngine = () => new MsieJsEngine(new MsieSettings
{
EngineMode = JsEngineMode.ChakraIeJsRt
});
RenderTemplates(createJsEngine, withPrecompilation);
}
[Benchmark]
[Arguments(false)]
[Arguments(true)]
public void MsieChakraEdgeJsRt(bool withPrecompilation)
{
Func<IJsEngine> createJsEngine = () => new MsieJsEngine(new MsieSettings
{
EngineMode = JsEngineMode.ChakraEdgeJsRt
});
RenderTemplates(createJsEngine, withPrecompilation);
}
#if NIL_JS
[Benchmark]
public void NiL()
{
Func<IJsEngine> createJsEngine = () => new NiLJsEngine();
RenderTemplates(createJsEngine, false);
}
#endif
[Benchmark]
public void Node()
{
Func<IJsEngine> createJsEngine = () => new NodeJsEngine();
RenderTemplates(createJsEngine, false);
}
[Benchmark]
[Arguments(false)]
[Arguments(true)]
public void V8(bool withPrecompilation)
{
Func<IJsEngine> createJsEngine = () => new V8JsEngine();
RenderTemplates(createJsEngine, withPrecompilation);
}
[Benchmark]
public void Vroom()
{
Func<IJsEngine> createJsEngine = () => new VroomJsEngine();
RenderTemplates(createJsEngine, false);
}
[Benchmark]
public void Yantra()
{
Func<IJsEngine> createJsEngine = () => new YantraJsEngine();
RenderTemplates(createJsEngine, false);
}
#region Internal types
private sealed class ContentItem
{
public string Name
{
get;
set;
}
public string TemplateCode
{
get;
set;
}
public string SerializedData
{
get;
set;
}
public string TargetOutput
{
get;
set;
}
public string Output
{
get;
set;
}
public ContentItem(string name)
{
Name = name;
}
}
#endregion
}
}