-
-
Notifications
You must be signed in to change notification settings - Fork 560
/
EngineComparisonBenchmark.cs
98 lines (86 loc) · 2.83 KB
/
EngineComparisonBenchmark.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
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Configs;
using BenchmarkDotNet.Order;
namespace Jint.Benchmark;
[RankColumn]
[MemoryDiagnoser]
[Orderer(SummaryOrderPolicy.FastestToSlowest)]
[GroupBenchmarksBy(BenchmarkLogicalGroupRule.ByParams)]
[HideColumns("Error", "Gen0", "Gen1", "Gen2")]
[BenchmarkCategory("EngineComparison")]
public class EngineComparisonBenchmark
{
private static readonly Dictionary<string, Prepared<Script>> _parsedScripts = new();
private static readonly Dictionary<string, string> _files = new()
{
{ "array-stress", null },
{ "evaluation", null },
{ "linq-js", null },
{ "minimal", null },
{ "stopwatch", null },
{ "dromaeo-3d-cube", null },
{ "dromaeo-core-eval", null },
{ "dromaeo-object-array", null },
{ "dromaeo-object-regexp", null },
{ "dromaeo-object-string", null },
{ "dromaeo-string-base64", null },
};
private static readonly string _dromaeoHelpers = @"
var startTest = function () { };
var test = function (name, fn) { fn(); };
var endTest = function () { };
var prep = function (fn) { fn(); };";
[GlobalSetup]
public void Setup()
{
foreach (var fileName in _files.Keys.ToList())
{
var script = File.ReadAllText($"Scripts/{fileName}.js");
if (fileName.Contains("dromaeo"))
{
script = _dromaeoHelpers + Environment.NewLine + Environment.NewLine + script;
}
_files[fileName] = script;
_parsedScripts[fileName] = Engine.PrepareScript(script, strict: true);
}
}
[ParamsSource(nameof(FileNames))]
public string FileName { get; set; }
public IEnumerable<string> FileNames()
{
return _files.Select(entry => entry.Key);
}
[Benchmark]
public void Jint()
{
var engine = new Engine(static options => options.Strict());
engine.Execute(_files[FileName]);
}
[Benchmark]
public void Jint_ParsedScript()
{
var engine = new Engine(static options => options.Strict());
engine.Execute(_parsedScripts[FileName]);
}
[Benchmark]
public void Jurassic()
{
var engine = new Jurassic.ScriptEngine { ForceStrictMode = true };
engine.Execute(_files[FileName]);
}
[Benchmark]
public void NilJS()
{
var engine = new NiL.JS.Core.Context(strict: true);
engine.Eval(_files[FileName]);
}
[Benchmark]
public void YantraJS()
{
var engine = new YantraJS.Core.JSContext();
// By default YantraJS is strict mode only, in strict mode
// we need to pass `this` explicitly in global context
// if script is expecting global context as `this`
engine.Eval(_files[FileName], null, engine);
}
}