Skip to content

Commit ed6bd3e

Browse files
committed
Added a host types embedding benchmark
1 parent ca5b3e8 commit ed6bd3e

File tree

3 files changed

+242
-0
lines changed

3 files changed

+242
-0
lines changed
Lines changed: 161 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,161 @@
1+
using System;
2+
using System.Drawing;
3+
4+
using BenchmarkDotNet.Attributes;
5+
using BenchmarkDotNet.Diagnosers;
6+
using BenchmarkDotNet.Order;
7+
8+
using JavaScriptEngineSwitcher.ChakraCore;
9+
using JavaScriptEngineSwitcher.Core;
10+
using JavaScriptEngineSwitcher.Jint;
11+
using JavaScriptEngineSwitcher.Jurassic;
12+
//using JavaScriptEngineSwitcher.Msie;
13+
using JavaScriptEngineSwitcher.NiL;
14+
#if NET46
15+
using JavaScriptEngineSwitcher.V8;
16+
#endif
17+
18+
using JavaScriptEngineSwitcher.Benchmarks.Interop.TypesEmbedding;
19+
20+
namespace JavaScriptEngineSwitcher.Benchmarks
21+
{
22+
[MemoryDiagnoser]
23+
[Orderer(SummaryOrderPolicy.Method, MethodOrderPolicy.Declared)]
24+
public class HostTypesEmbeddingBenchmark
25+
{
26+
private static void EmbedAndUseHostTypes(Func<IJsEngine> createJsEngine)
27+
{
28+
// Arrange
29+
var someType = typeof(SomeClass);
30+
var pointType = typeof(Point);
31+
var someOtherType = typeof(SomeOtherClass);
32+
33+
const string input = @"(function(SomeClass, Point, SomeOtherClass, undefined) {
34+
var arg1, arg2, arg3, arg4, interimResult, result;
35+
36+
SomeClass.Field1 = false;
37+
SomeClass.Field2 = 678;
38+
SomeClass.Field3 = 2.20;
39+
SomeClass.Field4 = 'QWERTY';
40+
SomeClass.Field5 = new Point(2, 4);
41+
42+
SomeClass.Property1 = true;
43+
SomeClass.Property2 = 711;
44+
SomeClass.Property3 = 5.5;
45+
SomeClass.Property4 = 'ЙЦУКЕН';
46+
SomeClass.Property5 = new SomeOtherClass(true, 611, 69.82, 'ASDF',
47+
false, 555, 79.99, 'ФЫВА');
48+
49+
arg1 = SomeClass.Field1 || SomeClass.Property1;
50+
arg2 = SomeClass.Field2 + SomeClass.Property2 + SomeClass.Field5.X;
51+
arg3 = SomeClass.Field3 + SomeClass.Property3 + SomeClass.Field5.Y;
52+
arg4 = SomeClass.Field4 + SomeClass.Property4;
53+
54+
interimResult = SomeClass.DoSomething(arg1, arg2, arg3, arg4);
55+
56+
arg1 = SomeClass.Property5.Field1 && SomeClass.Property5.Property1;
57+
arg2 = interimResult - SomeClass.Property5.Field2 - SomeClass.Property5.Property2;
58+
arg3 = SomeClass.Property5.Field3 / SomeClass.Property5.Property3;
59+
arg4 = SomeClass.Property5.Field4 + SomeClass.Property5.Property4;
60+
61+
result = SomeOtherClass.DoSomething(arg1, arg2, arg3, arg4);
62+
63+
return result;
64+
}(SomeClass, Point, SomeOtherClass));";
65+
const string targetOutput = "RmFsc2V8MjkyMHwwLjg3Mjg1OTEwNzM4ODQyNHxBU0RG0KTQq9CS0JA=";
66+
67+
// Act
68+
string output;
69+
70+
using (var jsEngine = createJsEngine())
71+
{
72+
jsEngine.EmbedHostType("SomeClass", someType);
73+
jsEngine.EmbedHostType("Point", pointType);
74+
jsEngine.EmbedHostType("SomeOtherClass", someOtherType);
75+
76+
output = jsEngine.Evaluate<string>(input);
77+
}
78+
79+
// Assert
80+
Assert.Equal(targetOutput, output);
81+
}
82+
83+
[Benchmark]
84+
public void ChakraCore()
85+
{
86+
Func<IJsEngine> createJsEngine = () => new ChakraCoreJsEngine();
87+
EmbedAndUseHostTypes(createJsEngine);
88+
}
89+
90+
[Benchmark]
91+
public void Jint()
92+
{
93+
Func<IJsEngine> createJsEngine = () => new JintJsEngine();
94+
EmbedAndUseHostTypes(createJsEngine);
95+
}
96+
97+
[Benchmark]
98+
public void Jurassic()
99+
{
100+
Func<IJsEngine> createJsEngine = () => new JurassicJsEngine();
101+
EmbedAndUseHostTypes(createJsEngine);
102+
}
103+
//#if NET46
104+
105+
// [Benchmark]
106+
// public void MsieClassic()
107+
// {
108+
// Func<IJsEngine> createJsEngine = () => new MsieJsEngine(new MsieSettings
109+
// {
110+
// EngineMode = JsEngineMode.Classic
111+
// });
112+
// EmbedAndUseHostTypes(createJsEngine);
113+
// }
114+
115+
// [Benchmark]
116+
// public void MsieChakraActiveScript()
117+
// {
118+
// Func<IJsEngine> createJsEngine = () => new MsieJsEngine(new MsieSettings
119+
// {
120+
// EngineMode = JsEngineMode.ChakraActiveScript
121+
// });
122+
// EmbedAndUseHostTypes(createJsEngine);
123+
// }
124+
//#endif
125+
//[Benchmark]
126+
//public void MsieChakraIeJsRt()
127+
//{
128+
// Func<IJsEngine> createJsEngine = () => new MsieJsEngine(new MsieSettings
129+
// {
130+
// EngineMode = JsEngineMode.ChakraIeJsRt
131+
// });
132+
// EmbedAndUseHostTypes(createJsEngine);
133+
//}
134+
135+
//[Benchmark]
136+
//public void MsieChakraEdgeJsRt()
137+
//{
138+
// Func<IJsEngine> createJsEngine = () => new MsieJsEngine(new MsieSettings
139+
// {
140+
// EngineMode = JsEngineMode.ChakraEdgeJsRt
141+
// });
142+
// EmbedAndUseHostTypes(createJsEngine);
143+
//}
144+
145+
[Benchmark]
146+
public void NiL()
147+
{
148+
Func<IJsEngine> createJsEngine = () => new NiLJsEngine();
149+
EmbedAndUseHostTypes(createJsEngine);
150+
}
151+
#if NET46
152+
153+
[Benchmark]
154+
public void V8()
155+
{
156+
Func<IJsEngine> createJsEngine = () => new V8JsEngine();
157+
EmbedAndUseHostTypes(createJsEngine);
158+
}
159+
#endif
160+
}
161+
}
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 JavaScriptEngineSwitcher.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 JavaScriptEngineSwitcher.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)