Skip to content

Commit d395655

Browse files
committed
Added a host types embedding benchmark
1 parent 8a4abb5 commit d395655

File tree

3 files changed

+195
-0
lines changed

3 files changed

+195
-0
lines changed
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
using System;
2+
using System.Drawing;
3+
4+
using BenchmarkDotNet.Attributes;
5+
using BenchmarkDotNet.Diagnosers;
6+
using BenchmarkDotNet.Order;
7+
8+
using MsieJavaScriptEngine.Benchmarks.Interop.TypesEmbedding;
9+
10+
namespace MsieJavaScriptEngine.Benchmarks
11+
{
12+
[MemoryDiagnoser]
13+
[Orderer(SummaryOrderPolicy.Method, MethodOrderPolicy.Declared)]
14+
public class HostTypesEmbeddingBenchmark
15+
{
16+
private static void EmbedAndUseHostTypes(Func<MsieJsEngine> createJsEngine)
17+
{
18+
// Arrange
19+
var someType = typeof(SomeClass);
20+
var pointType = typeof(Point);
21+
var someOtherType = typeof(SomeOtherClass);
22+
23+
const string input = @"(function(SomeClass, Point, SomeOtherClass, undefined) {
24+
var arg1, arg2, arg3, arg4, interimResult, result;
25+
26+
SomeClass.Field1 = false;
27+
SomeClass.Field2 = 678;
28+
SomeClass.Field3 = 2.20;
29+
SomeClass.Field4 = 'QWERTY';
30+
SomeClass.Field5 = new Point(2, 4);
31+
32+
SomeClass.Property1 = true;
33+
SomeClass.Property2 = 711;
34+
SomeClass.Property3 = 5.5;
35+
SomeClass.Property4 = 'ЙЦУКЕН';
36+
SomeClass.Property5 = new SomeOtherClass(true, 611, 69.82, 'ASDF',
37+
false, 555, 79.99, 'ФЫВА');
38+
39+
arg1 = SomeClass.Field1 || SomeClass.Property1;
40+
arg2 = SomeClass.Field2 + SomeClass.Property2 + SomeClass.Field5.X;
41+
arg3 = SomeClass.Field3 + SomeClass.Property3 + SomeClass.Field5.Y;
42+
arg4 = SomeClass.Field4 + SomeClass.Property4;
43+
44+
interimResult = SomeClass.DoSomething(arg1, arg2, arg3, arg4);
45+
46+
arg1 = SomeClass.Property5.Field1 && SomeClass.Property5.Property1;
47+
arg2 = interimResult - SomeClass.Property5.Field2 - SomeClass.Property5.Property2;
48+
arg3 = SomeClass.Property5.Field3 / SomeClass.Property5.Property3;
49+
arg4 = SomeClass.Property5.Field4 + SomeClass.Property5.Property4;
50+
51+
result = SomeOtherClass.DoSomething(arg1, arg2, arg3, arg4);
52+
53+
return result;
54+
}(SomeClass, Point, SomeOtherClass));";
55+
const string targetOutput = "RmFsc2V8MjkyMHwwLjg3Mjg1OTEwNzM4ODQyNHxBU0RG0KTQq9CS0JA=";
56+
57+
// Act
58+
string output;
59+
60+
using (var jsEngine = createJsEngine())
61+
{
62+
jsEngine.EmbedHostType("SomeClass", someType);
63+
jsEngine.EmbedHostType("Point", pointType);
64+
jsEngine.EmbedHostType("SomeOtherClass", someOtherType);
65+
66+
output = jsEngine.Evaluate<string>(input);
67+
}
68+
69+
// Assert
70+
Assert.Equal(targetOutput, output);
71+
}
72+
#if NET46
73+
74+
[Benchmark]
75+
public void Classic()
76+
{
77+
Func<MsieJsEngine> createJsEngine = () => new MsieJsEngine(new JsEngineSettings{
78+
EngineMode = JsEngineMode.Classic
79+
});
80+
EmbedAndUseHostTypes(createJsEngine);
81+
}
82+
83+
[Benchmark]
84+
public void ChakraActiveScript()
85+
{
86+
Func<MsieJsEngine> createJsEngine = () => new MsieJsEngine(new JsEngineSettings
87+
{
88+
EngineMode = JsEngineMode.ChakraActiveScript
89+
});
90+
EmbedAndUseHostTypes(createJsEngine);
91+
}
92+
#endif
93+
94+
[Benchmark]
95+
public void ChakraIeJsRt()
96+
{
97+
Func<MsieJsEngine> createJsEngine = () => new MsieJsEngine(new JsEngineSettings
98+
{
99+
EngineMode = JsEngineMode.ChakraIeJsRt
100+
});
101+
EmbedAndUseHostTypes(createJsEngine);
102+
}
103+
104+
[Benchmark]
105+
public void ChakraEdgeJsRt()
106+
{
107+
Func<MsieJsEngine> createJsEngine = () => new MsieJsEngine(new JsEngineSettings
108+
{
109+
EngineMode = JsEngineMode.ChakraEdgeJsRt
110+
});
111+
EmbedAndUseHostTypes(createJsEngine);
112+
}
113+
}
114+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
using System;
2+
using System.Drawing;
3+
using System.Linq;
4+
using System.Text;
5+
6+
namespace MsieJavaScriptEngine.Benchmarks.Interop.TypesEmbedding
7+
{
8+
public static class SomeClass
9+
{
10+
public static bool Field1;
11+
public static int Field2;
12+
public static double Field3;
13+
public static string Field4;
14+
public static Point Field5;
15+
16+
public static bool Property1 { get; set; }
17+
public static int Property2 { get; set; }
18+
public static double Property3 { get; set; }
19+
public static string Property4 { get; set; }
20+
public static SomeOtherClass Property5 { get; set; }
21+
22+
23+
public static int DoSomething(bool arg1, int arg2, double arg3, string arg4)
24+
{
25+
int result = Convert.ToInt32(arg1) +
26+
arg2 +
27+
(int)Math.Round(arg3) +
28+
Encoding.UTF8.GetBytes(arg4).Sum(x => x);
29+
;
30+
31+
return result;
32+
}
33+
}
34+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
using System;
2+
using System.Globalization;
3+
using System.Text;
4+
5+
namespace MsieJavaScriptEngine.Benchmarks.Interop.TypesEmbedding
6+
{
7+
public class SomeOtherClass
8+
{
9+
public bool Field1;
10+
public int Field2;
11+
public double Field3;
12+
public string Field4;
13+
14+
public bool Property1 { get; set; }
15+
public int Property2 { get; set; }
16+
public double Property3 { get; set; }
17+
public string Property4 { get; set; }
18+
19+
20+
public SomeOtherClass(bool field1, int field2, double field3, string field4,
21+
bool property1, int property2, double property3, string property4)
22+
{
23+
Field1 = field1;
24+
Field2 = field2;
25+
Field3 = field3;
26+
Field4 = field4;
27+
28+
Property1 = property1;
29+
Property2 = property2;
30+
Property3 = property3;
31+
Property4 = property4;
32+
}
33+
34+
35+
public static string DoSomething(bool arg1, int arg2, double arg3, string arg4)
36+
{
37+
string rawResult = arg1.ToString(CultureInfo.InvariantCulture) + "|" +
38+
arg2.ToString(CultureInfo.InvariantCulture) + "|" +
39+
arg3.ToString(CultureInfo.InvariantCulture) + "|" +
40+
arg4
41+
;
42+
string result = Convert.ToBase64String(Encoding.UTF8.GetBytes(rawResult));
43+
44+
return result;
45+
}
46+
}
47+
}

0 commit comments

Comments
 (0)