-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathJsExecutionBenchmark.cs
More file actions
231 lines (195 loc) · 7.77 KB
/
JsExecutionBenchmark.cs
File metadata and controls
231 lines (195 loc) · 7.77 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
using System;
using System.IO;
using System.Reflection;
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Diagnosers;
using BenchmarkDotNet.Order;
namespace MsieJavaScriptEngine.Benchmarks
{
[MemoryDiagnoser]
[Orderer(SummaryOrderPolicy.Method, MethodOrderPolicy.Declared)]
public class JsExecutionBenchmark
{
/// <summary>
/// Name of the file containing library for transliteration of Russian
/// </summary>
private const string LibraryFileName = "russian-translit.js";
/// <summary>
/// Name of transliterate function
/// </summary>
private const string FunctionName = "transliterate";
/// <summary>
/// Number of transliterated items
/// </summary>
private const int ItemCount = 7;
/// <summary>
/// Code of library for transliteration of Russian
/// </summary>
private static string _libraryCode;
/// <summary>
/// List of transliteration types
/// </summary>
private static string[] _inputTypes;
/// <summary>
/// List of input strings
/// </summary>
private static string[] _inputStrings;
/// <summary>
/// List of target output strings
/// </summary>
private static string[] _targetOutputStrings;
/// <summary>
/// Static constructor
/// </summary>
static JsExecutionBenchmark()
{
PopulateTestData();
}
/// <summary>
/// Populates a test data
/// </summary>
public static void PopulateTestData()
{
Type type = typeof(JsExecutionBenchmark);
Assembly assembly = type.Assembly;
string resourceName = type.Namespace + ".Resources." + LibraryFileName;
using (Stream stream = assembly.GetManifestResourceStream(resourceName))
using (StreamReader reader = new StreamReader(stream))
{
_libraryCode = reader.ReadToEnd();
}
_inputTypes = new string[ItemCount]
{
"basic", "letters-numbers", "gost-16876-71", "gost-7-79-2000", "police", "foreign-passport",
"yandex-friendly-url"
};
_inputStrings = new string[ItemCount]
{
"SOLID — мнемонический акроним, введённый Майклом Фэзерсом для первых пяти принципов, названных " +
"Робертом Мартином в начале 2000-х, которые означали пять основных принципов объектно-ориентированного " +
"программирования и проектирования.",
"Принцип единственной ответственности (The Single Responsibility Principle). " +
"Каждый класс выполняет лишь одну задачу.",
"Принцип открытости/закрытости (The Open Closed Principle). " +
"«программные сущности … должны быть открыты для расширения, но закрыты для модификации.»",
"Принцип подстановки Барбары Лисков (The Liskov Substitution Principle). " +
"«объекты в программе должны быть заменяемыми на экземпляры их подтипов без изменения правильности выполнения программы.»",
"Принцип разделения интерфейса (The Interface Segregation Principle). " +
"«много интерфейсов, специально предназначенных для клиентов, лучше, чем один интерфейс общего назначения.»",
"Принцип инверсии зависимостей (The Dependency Inversion Principle). " +
"«Зависимость на Абстракциях. Нет зависимости на что-то конкретное.»",
"SOLID (объектно-ориентированное программирование)"
};
_targetOutputStrings = new string[ItemCount]
{
"SOLID — mnemonicheskij akronim, vvedjonnyj Majklom Fjezersom dlja pervyh pjati principov, nazvannyh " +
"Robertom Martinom v nachale 2000-h, kotorye oznachali pjat' osnovnyh principov ob#ektno-orientirovannogo " +
"programmirovanija i proektirovanija.",
"Princip edinstvennoj otvetstvennosti (The Single Responsibility Principle). " +
"Ka#dyj klass vypolnjaet li6' odnu zada4u.",
"Princip otkrytosti/zakrytosti (The Open Closed Principle). " +
"«programmnye sushhnosti … dolzhny byt' otkryty dlja rasshirenija, no zakryty dlja modifikacii.»",
"Princip podstanovki Barbary Liskov (The Liskov Substitution Principle). " +
"«ob\"ekty v programme dolzhny byt' zamenyaemymi na e'kzemplyary ix podtipov bez izmeneniya pravil'nosti " +
"vypolneniya programmy.»",
"Printsip razdeleniia interfeisa (The Interface Segregation Principle). " +
"«mnogo interfeisov, spetsialno prednaznachennykh dlia klientov, luchshe, chem odin interfeis obshchego " +
"naznacheniia.»",
"Printcip inversii zavisimostei (The Dependency Inversion Principle). " +
"«Zavisimost na Abstraktciiakh. Net zavisimosti na chto-to konkretnoe.»",
"solid-obektno-orientirovannoe-programmirovanie"
};
}
/// <summary>
/// Transliterates a strings
/// </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 TransliterateStrings(Func<MsieJsEngine> createJsEngine, bool withPrecompilation)
{
// Arrange
string[] outputStrings = new string[ItemCount];
PrecompiledScript precompiledCode = null;
// Act
using (var jsEngine = createJsEngine())
{
if (withPrecompilation)
{
if (!jsEngine.SupportsScriptPrecompilation)
{
throw new NotSupportedException($"{jsEngine.Mode} mode does not support precompilation.");
}
precompiledCode = jsEngine.Precompile(_libraryCode, LibraryFileName);
jsEngine.Execute(precompiledCode);
}
else
{
jsEngine.Execute(_libraryCode, LibraryFileName);
}
outputStrings[0] = jsEngine.CallFunction<string>(FunctionName, _inputStrings[0], _inputTypes[0]);
}
for (int itemIndex = 1; itemIndex < ItemCount; itemIndex++)
{
using (var jsEngine = createJsEngine())
{
if (withPrecompilation)
{
jsEngine.Execute(precompiledCode);
}
else
{
jsEngine.Execute(_libraryCode, LibraryFileName);
}
outputStrings[itemIndex] = jsEngine.CallFunction<string>(FunctionName, _inputStrings[itemIndex],
_inputTypes[itemIndex]);
}
}
// Assert
for (int itemIndex = 0; itemIndex < ItemCount; itemIndex++)
{
Assert.Equal(_targetOutputStrings[itemIndex], outputStrings[itemIndex]);
}
}
#if NET462
[Benchmark]
public void Classic()
{
Func<MsieJsEngine> createJsEngine = () => new MsieJsEngine(new JsEngineSettings{
EngineMode = JsEngineMode.Classic
});
TransliterateStrings(createJsEngine, false);
}
[Benchmark]
public void ChakraActiveScript()
{
Func<MsieJsEngine> createJsEngine = () => new MsieJsEngine(new JsEngineSettings
{
EngineMode = JsEngineMode.ChakraActiveScript
});
TransliterateStrings(createJsEngine, false);
}
#endif
[Benchmark]
[Arguments(false)]
[Arguments(true)]
public void ChakraIeJsRt(bool withPrecompilation)
{
Func<MsieJsEngine> createJsEngine = () => new MsieJsEngine(new JsEngineSettings
{
EngineMode = JsEngineMode.ChakraIeJsRt
});
TransliterateStrings(createJsEngine, withPrecompilation);
}
[Benchmark]
[Arguments(false)]
[Arguments(true)]
public void ChakraEdgeJsRt(bool withPrecompilation)
{
Func<MsieJsEngine> createJsEngine = () => new MsieJsEngine(new JsEngineSettings
{
EngineMode = JsEngineMode.ChakraEdgeJsRt
});
TransliterateStrings(createJsEngine, withPrecompilation);
}
}
}