-
-
Notifications
You must be signed in to change notification settings - Fork 560
/
InteropBenchmark.cs
299 lines (263 loc) · 8.46 KB
/
InteropBenchmark.cs
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
using System.Reflection;
using BenchmarkDotNet.Attributes;
using Jint.Native;
using Jint.Native.Array;
namespace Jint.Benchmark;
[MemoryDiagnoser]
public class InteropBenchmark
{
private const int OperationsPerInvoke = 1_000;
public class Person
{
public string Name { get; set; }
public int Age { get; set; }
}
private Engine _engine;
[GlobalSetup]
public void Setup()
{
_engine = new Engine(cfg => cfg.AllowClr(
typeof(Person).GetTypeInfo().Assembly,
typeof(Console).GetTypeInfo().Assembly,
typeof(System.IO.File).GetTypeInfo().Assembly))
.SetValue("log", new Action<object>(Console.WriteLine))
.SetValue("assert", new Action<bool>(x => { }))
.SetValue("equal", new Action<object, object>((x, y) => { }));
}
[Benchmark(OperationsPerInvoke = OperationsPerInvoke)]
public void DelegatesCanBeSet()
{
for (int i = 0; i < OperationsPerInvoke; ++i)
{
_engine.SetValue("square", new Func<double, double>(x => x * x));
_engine.Execute("assert(square(10) === 100);");
}
}
[Benchmark(OperationsPerInvoke = OperationsPerInvoke)]
public void ExtraParametersAreIgnored()
{
for (int i = 0; i < OperationsPerInvoke; ++i)
{
_engine.SetValue("passNumber", new Func<int, int>(x => x));
_engine.Execute("assert(passNumber(123,'test',{},[],null) === 123);");
}
}
[Benchmark(OperationsPerInvoke = OperationsPerInvoke)]
public void GetObjectProperties()
{
var p = new Person
{
Name = "Mickey Mouse"
};
for (int i = 0; i < OperationsPerInvoke; ++i)
{
_engine.SetValue("p", p);
_engine.Execute("assert(p.Name === 'Mickey Mouse');");
}
}
[Benchmark(OperationsPerInvoke = OperationsPerInvoke)]
public void InvokeObjectMethods()
{
var p = new Person
{
Name = "Mickey Mouse"
};
for (int i = 0; i < OperationsPerInvoke; ++i)
{
_engine.SetValue("p", p);
_engine.Execute(@"assert(p.ToString() === 'Mickey Mouse');");
}
}
[Benchmark(OperationsPerInvoke = OperationsPerInvoke)]
public void SetObjectProperties()
{
var p = new Person
{
Name = "Mickey Mouse"
};
for (int i = 0; i < OperationsPerInvoke; ++i)
{
_engine.SetValue("p", p);
_engine.Execute("p.Name = 'Donald Duck'; assert(p.Name === 'Donald Duck');");
}
}
[Benchmark(OperationsPerInvoke = OperationsPerInvoke)]
public void GetIndexUsingStringKey()
{
var dictionary = new Dictionary<string, Person>();
dictionary.Add("person1", new Person {Name = "Mickey Mouse"});
dictionary.Add("person2", new Person {Name = "Goofy"});
for (int i = 0; i < OperationsPerInvoke; ++i)
{
_engine.SetValue("dictionary", dictionary);
_engine.Execute("assert(dictionary['person1'].Name === 'Mickey Mouse'); assert(dictionary['person2'].Name === 'Goofy');");
}
}
[Benchmark(OperationsPerInvoke = OperationsPerInvoke)]
public void GenericMethods()
{
var dictionary = new Dictionary<int, string>
{
{1, "Mickey Mouse"}
};
for (int i = 0; i < OperationsPerInvoke; ++i)
{
_engine.SetValue("dictionary", dictionary);
_engine.Execute("dictionary.Clear(); dictionary.Add(2, 'Goofy'); assert(dictionary[2] === 'Goofy');");
}
}
[Benchmark(OperationsPerInvoke = OperationsPerInvoke)]
public void MultiGenericTypes()
{
for (int i = 0; i < OperationsPerInvoke; ++i)
{
_engine.Execute(@"
var type = System.Collections.Generic.Dictionary(System.Int32, System.String);
var dictionary = new type();
dictionary.Add(1, 'Mickey Mouse');
dictionary.Add(2, 'Goofy');
assert(dictionary[2] === 'Goofy');
");
}
}
[Benchmark(OperationsPerInvoke = OperationsPerInvoke)]
public void IndexOnList()
{
var list = new List<object>(2);
list.Add("Mickey Mouse");
list.Add("Goofy");
for (int i = 0; i < OperationsPerInvoke; ++i)
{
_engine.SetValue("list", list);
_engine.Execute("list[1] = 'Donald Duck'; assert(list[1] === 'Donald Duck');");
}
}
[Benchmark(OperationsPerInvoke = OperationsPerInvoke)]
public void EcmaValuesAreAutomaticallyConvertedWhenSetInPoco()
{
var p = new Person
{
Name = "foo",
};
for (int i = 0; i < OperationsPerInvoke; ++i)
{
_engine.SetValue("p", p);
_engine.Execute(@"
assert(p.Name === 'foo');
assert(p.Age === 0);
p.Name = 'bar';
p.Age = 10;
");
}
}
[Benchmark(OperationsPerInvoke = OperationsPerInvoke)]
public void Trim()
{
var p = new Person
{
Name = "Mickey Mouse "
};
for (int i = 0; i < OperationsPerInvoke; ++i)
{
_engine.SetValue("p", p);
_engine.Execute(@"
assert(p.Name === 'Mickey Mouse ');
p.Name = p.Name.trim();
assert(p.Name === 'Mickey Mouse');
");
}
}
[Benchmark(OperationsPerInvoke = OperationsPerInvoke)]
public void MathFloor()
{
var p = new Person();
for (int i = 0; i < OperationsPerInvoke; ++i)
{
_engine.SetValue("p", p);
_engine.Execute("p.Age = Math.floor(1.6); assert(p.Age === 1);");
}
}
[Benchmark(OperationsPerInvoke = OperationsPerInvoke)]
public void DelegateAsFunction()
{
var even = new Func<int, bool>(x => x % 2 == 0);
for (int i = 0; i < OperationsPerInvoke; ++i)
{
_engine.SetValue("even", even);
_engine.Execute("assert(even(2) === true);");
}
}
[Benchmark(OperationsPerInvoke = OperationsPerInvoke)]
public void ConvertArrayToArrayInstance()
{
var ints = new[] {1, 2, 3, 4, 5, 6};
for (int i = 0; i < OperationsPerInvoke; ++i)
{
_engine
.SetValue("values", ints)
.Execute("values.filter(function(x){ return x % 2 == 0; })");
}
}
[Benchmark(OperationsPerInvoke = OperationsPerInvoke)]
public void ConvertListsToArrayInstance()
{
var ints = new List<object> {1, 2, 3, 4, 5, 6};
for (int i = 0; i < OperationsPerInvoke; ++i)
{
_engine
.SetValue("values", ints)
.Execute("new Array(values).filter(function(x){ return x % 2 == 0; })");
}
}
[Benchmark(OperationsPerInvoke = OperationsPerInvoke)]
public void ConvertArrayInstanceToArray()
{
for (int i = 0; i < OperationsPerInvoke; ++i)
{
_engine.Execute("'[email protected]'.split('@');");
}
}
[Benchmark(OperationsPerInvoke = OperationsPerInvoke)]
public void ResolvingConsoleWriteLine()
{
var originalOut = Console.Out;
Console.SetOut(TextWriter.Null);
for (int i = 0; i < OperationsPerInvoke; ++i)
{
_engine.Execute("System.Console.WriteLine('value to write');");
}
Console.SetOut(originalOut);
}
[Benchmark(OperationsPerInvoke = OperationsPerInvoke)]
public void Setter()
{
var p = new Person();
_engine.SetValue("p", p);
for (int i = 0; i < OperationsPerInvoke; ++i)
{
_engine.Execute("p.Age = 42;");
}
}
[Benchmark(OperationsPerInvoke = OperationsPerInvoke)]
public void LoopWithNativeEnumerator()
{
JsValue Adder(JsValue argValue)
{
ArrayInstance args = argValue.AsArray();
double sum = 0;
foreach (var item in args)
{
if (item.IsNumber())
{
sum += item.AsNumber();
}
}
return sum;
}
for (int i = 0; i < OperationsPerInvoke; ++i)
{
_engine.SetValue("getSum", new Func<JsValue, JsValue>(Adder));
_engine.Execute("getSum([1,2,3]);");
}
}
}