Skip to content

Commit cb4effd

Browse files
authored
Support running DromaeoBenchmark with modern JS syntax (#2041)
1 parent 815fa32 commit cb4effd

12 files changed

+1275
-31
lines changed

Jint.Benchmark/DromaeoBenchmark.cs

+77-29
Original file line numberDiff line numberDiff line change
@@ -1,70 +1,118 @@
11
using BenchmarkDotNet.Attributes;
2+
using BenchmarkDotNet.Configs;
23

34
namespace Jint.Benchmark;
45

56
[MemoryDiagnoser]
7+
[GroupBenchmarksBy(BenchmarkLogicalGroupRule.ByMethod)]
68
public class DromaeoBenchmark
79
{
810
private static readonly Dictionary<string, string> _files = new()
911
{
10-
{"dromaeo-3d-cube", null},
11-
{"dromaeo-core-eval", null},
12-
{"dromaeo-object-array", null},
13-
{"dromaeo-object-regexp", null},
14-
{"dromaeo-object-string", null},
15-
{"dromaeo-string-base64", null}
12+
{ "dromaeo-3d-cube", null },
13+
{ "dromaeo-core-eval", null },
14+
{ "dromaeo-object-array", null },
15+
{ "dromaeo-object-regexp", null },
16+
{ "dromaeo-object-string", null },
17+
{ "dromaeo-string-base64", null }
1618
};
1719

1820
private readonly Dictionary<string, Prepared<Script>> _prepared = new();
1921

20-
private Engine engine;
22+
private Engine _engine;
2123

2224
[GlobalSetup]
2325
public void Setup()
2426
{
25-
foreach (var fileName in _files.Keys)
27+
foreach (var fileName in _files.Keys.ToArray())
2628
{
27-
var script = File.ReadAllText($"Scripts/{fileName}.js");
28-
_files[fileName] = script;
29-
_prepared[fileName] = Engine.PrepareScript(script);
29+
foreach (var suffix in new[] {"", "-modern"})
30+
{
31+
var name = fileName + suffix;
32+
var script = File.ReadAllText($"Scripts/{name}.js");
33+
_files[name] = script;
34+
_prepared[name] = Engine.PrepareScript(script, name);
35+
}
3036
}
37+
}
38+
39+
[IterationSetup]
40+
public void IterationSetup()
41+
{
42+
_engine = CreteEngine();
43+
}
3144

32-
engine = new Engine()
45+
private static Engine CreteEngine()
46+
{
47+
var engine = new Engine()
3348
.SetValue("log", new Action<object>(Console.WriteLine))
3449
.SetValue("assert", new Action<bool>(b => { }));
3550

36-
engine.Execute(@"
37-
var startTest = function () { };
38-
var test = function (name, fn) { fn(); };
39-
var endTest = function () { };
40-
var prep = function (fn) { fn(); };
41-
");
51+
engine.Execute("""
52+
53+
var startTest = function () { };
54+
var test = function (name, fn) { fn(); };
55+
var endTest = function () { };
56+
var prep = function (fn) { fn(); };
57+
58+
""");
59+
60+
return engine;
4261
}
4362

44-
[ParamsSource(nameof(FileNames))]
45-
public string FileName { get; set; }
63+
[Params(false, true, Priority = 50)]
64+
public bool Modern { get; set; }
4665

47-
[Params(true, false)]
66+
[Params(true, false, Priority = 100)]
4867
public bool Prepared { get; set; }
4968

50-
public IEnumerable<string> FileNames()
69+
[Benchmark]
70+
public void CoreEval()
5171
{
52-
foreach (var entry in _files)
53-
{
54-
yield return entry.Key;
55-
}
72+
Run("dromaeo-core-eval");
73+
}
74+
75+
[Benchmark]
76+
public void Cube()
77+
{
78+
Run("dromaeo-3d-cube");
5679
}
5780

5881
[Benchmark]
59-
public void Run()
82+
public void ObjectArray()
6083
{
84+
Run("dromaeo-object-array");
85+
}
86+
87+
[Benchmark]
88+
public void ObjectRegExp()
89+
{
90+
Run("dromaeo-object-regexp");
91+
}
92+
93+
[Benchmark]
94+
public void ObjectString()
95+
{
96+
Run("dromaeo-object-string");
97+
}
98+
99+
[Benchmark]
100+
public void StringBase64()
101+
{
102+
Run("dromaeo-string-base64");
103+
}
104+
105+
private void Run(string fileName)
106+
{
107+
var finalName = Modern ? fileName + "-modern" : fileName;
108+
61109
if (Prepared)
62110
{
63-
engine.Execute(_prepared[FileName]);
111+
_engine.Execute(_prepared[finalName]);
64112
}
65113
else
66114
{
67-
engine.Execute(_files[FileName]);
115+
_engine.Execute(_files[finalName], finalName);
68116
}
69117
}
70118
}

Jint.Benchmark/EngineComparisonBenchmark.cs

+8
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,23 @@ public class EngineComparisonBenchmark
1818
{
1919
{ "array-stress", null },
2020
{ "evaluation", null },
21+
{ "evaluation-modern", null },
2122
{ "linq-js", null },
2223
{ "minimal", null },
2324
{ "stopwatch", null },
25+
{ "stopwatch-modern", null },
2426
{ "dromaeo-3d-cube", null },
27+
{ "dromaeo-3d-cube-modern", null },
2528
{ "dromaeo-core-eval", null },
29+
{ "dromaeo-core-eval-modern", null },
2630
{ "dromaeo-object-array", null },
31+
{ "dromaeo-object-array-modern", null },
2732
{ "dromaeo-object-regexp", null },
33+
{ "dromaeo-object-regexp-modern", null },
2834
{ "dromaeo-object-string", null },
35+
{ "dromaeo-object-string-modern", null },
2936
{ "dromaeo-string-base64", null },
37+
{ "dromaeo-string-base64-modern", null },
3038
};
3139

3240
private static readonly string _dromaeoHelpers = @"

Jint.Benchmark/EvaluationBenchmark.cs

+4-1
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,8 @@ namespace Jint.Benchmark;
55
[MemoryDiagnoser]
66
public class EvaluationBenchmark : SingleScriptBenchmark
77
{
8-
protected override string FileName => "evaluation.js";
8+
[Params(false, true)]
9+
public bool Modern { get; set; }
10+
11+
protected override string FileName => Modern ? "evaluation-modern.js" : "evaluation.js";
912
}

0 commit comments

Comments
 (0)