forked from sebastienros/jint
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDromaeoBenchmark.cs
71 lines (60 loc) · 1.64 KB
/
DromaeoBenchmark.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
using BenchmarkDotNet.Attributes;
using Esprima.Ast;
namespace Jint.Benchmark;
[MemoryDiagnoser]
public class DromaeoBenchmark
{
private static readonly Dictionary<string, string> _files = new()
{
{"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 Dictionary<string, Script> _prepared = new();
private Engine engine;
[GlobalSetup]
public void Setup()
{
foreach (var fileName in _files.Keys)
{
var script = File.ReadAllText($"Scripts/{fileName}.js");
_files[fileName] = script;
_prepared[fileName] = Engine.PrepareScript(script);
}
engine = new Engine()
.SetValue("log", new Action<object>(Console.WriteLine))
.SetValue("assert", new Action<bool>(b => { }));
engine.Execute(@"
var startTest = function () { };
var test = function (name, fn) { fn(); };
var endTest = function () { };
var prep = function (fn) { fn(); };
");
}
[ParamsSource(nameof(FileNames))]
public string FileName { get; set; }
[Params(true, false)]
public bool Prepared { get; set; }
public IEnumerable<string> FileNames()
{
foreach (var entry in _files)
{
yield return entry.Key;
}
}
[Benchmark]
public void Run()
{
if (Prepared)
{
engine.Execute(_prepared[FileName]);
}
else
{
engine.Execute(_files[FileName]);
}
}
}