-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathHostTypesEmbeddingBenchmark.cs
More file actions
118 lines (99 loc) · 3.23 KB
/
HostTypesEmbeddingBenchmark.cs
File metadata and controls
118 lines (99 loc) · 3.23 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
using System;
using System.Drawing;
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Diagnosers;
using BenchmarkDotNet.Order;
using MsieJavaScriptEngine.Benchmarks.Interop.TypesEmbedding;
namespace MsieJavaScriptEngine.Benchmarks
{
[MemoryDiagnoser]
[Orderer(SummaryOrderPolicy.Method, MethodOrderPolicy.Declared)]
public class HostTypesEmbeddingBenchmark
{
private static void EmbedAndUseHostTypes(Func<MsieJsEngine> createJsEngine)
{
// Arrange
var someType = typeof(SomeClass);
var pointType = typeof(Point);
var someOtherType = typeof(SomeOtherClass);
const string input = @"(function(SomeClass, Point, SomeOtherClass, undefined) {
var arg1, arg2, arg3, arg4, interimResult, result;
SomeClass.Field1 = false;
SomeClass.Field2 = 678;
SomeClass.Field3 = 2.20;
SomeClass.Field4 = 'QWERTY';
SomeClass.Field5 = new Point(2, 4);
SomeClass.Property1 = true;
SomeClass.Property2 = 711;
SomeClass.Property3 = 5.5;
SomeClass.Property4 = 'ЙЦУКЕН';
SomeClass.Property5 = new SomeOtherClass(true, 611, 69.82, 'ASDF',
false, 555, 79.99, 'ФЫВА');
arg1 = SomeClass.Field1 || SomeClass.Property1;
arg2 = SomeClass.Field2 + SomeClass.Property2 + SomeClass.Field5.X;
arg3 = SomeClass.Field3 + SomeClass.Property3 + SomeClass.Field5.Y;
arg4 = SomeClass.Field4 + SomeClass.Property4;
interimResult = SomeClass.DoSomething(arg1, arg2, arg3, arg4);
arg1 = SomeClass.Property5.Field1 && SomeClass.Property5.Property1;
arg2 = interimResult - SomeClass.Property5.Field2 - SomeClass.Property5.Property2;
arg3 = SomeClass.Property5.Field3 / SomeClass.Property5.Property3;
arg4 = SomeClass.Property5.Field4 + SomeClass.Property5.Property4;
result = SomeOtherClass.DoSomething(arg1, arg2, arg3, arg4);
return result;
}(SomeClass, Point, SomeOtherClass));";
#if NET462
const string targetOutput = "RmFsc2V8MjkyMHwwLjg3Mjg1OTEwNzM4ODQyNHxBU0RG0KTQq9CS0JA=";
#else
const string targetOutput = "RmFsc2V8MjkyMHwwLjg3Mjg1OTEwNzM4ODQyMzV8QVNERtCk0KvQktCQ";
#endif
// Act
string output;
using (var jsEngine = createJsEngine())
{
jsEngine.EmbedHostType("SomeClass", someType);
jsEngine.EmbedHostType("Point", pointType);
jsEngine.EmbedHostType("SomeOtherClass", someOtherType);
output = jsEngine.Evaluate<string>(input);
}
// Assert
Assert.Equal(targetOutput, output);
}
#if NET462
[Benchmark]
public void Classic()
{
Func<MsieJsEngine> createJsEngine = () => new MsieJsEngine(new JsEngineSettings{
EngineMode = JsEngineMode.Classic
});
EmbedAndUseHostTypes(createJsEngine);
}
[Benchmark]
public void ChakraActiveScript()
{
Func<MsieJsEngine> createJsEngine = () => new MsieJsEngine(new JsEngineSettings
{
EngineMode = JsEngineMode.ChakraActiveScript
});
EmbedAndUseHostTypes(createJsEngine);
}
#endif
[Benchmark]
public void ChakraIeJsRt()
{
Func<MsieJsEngine> createJsEngine = () => new MsieJsEngine(new JsEngineSettings
{
EngineMode = JsEngineMode.ChakraIeJsRt
});
EmbedAndUseHostTypes(createJsEngine);
}
[Benchmark]
public void ChakraEdgeJsRt()
{
Func<MsieJsEngine> createJsEngine = () => new MsieJsEngine(new JsEngineSettings
{
EngineMode = JsEngineMode.ChakraEdgeJsRt
});
EmbedAndUseHostTypes(createJsEngine);
}
}
}