|
1 | 1 | using BenchmarkDotNet.Attributes;
|
| 2 | +using BenchmarkDotNet.Configs; |
2 | 3 |
|
3 | 4 | namespace Jint.Benchmark;
|
4 | 5 |
|
5 | 6 | [MemoryDiagnoser]
|
| 7 | +[GroupBenchmarksBy(BenchmarkLogicalGroupRule.ByMethod)] |
6 | 8 | public class DromaeoBenchmark
|
7 | 9 | {
|
8 | 10 | private static readonly Dictionary<string, string> _files = new()
|
9 | 11 | {
|
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 } |
16 | 18 | };
|
17 | 19 |
|
18 | 20 | private readonly Dictionary<string, Prepared<Script>> _prepared = new();
|
19 | 21 |
|
20 |
| - private Engine engine; |
| 22 | + private Engine _engine; |
21 | 23 |
|
22 | 24 | [GlobalSetup]
|
23 | 25 | public void Setup()
|
24 | 26 | {
|
25 |
| - foreach (var fileName in _files.Keys) |
| 27 | + foreach (var fileName in _files.Keys.ToArray()) |
26 | 28 | {
|
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 | + } |
30 | 36 | }
|
| 37 | + } |
| 38 | + |
| 39 | + [IterationSetup] |
| 40 | + public void IterationSetup() |
| 41 | + { |
| 42 | + _engine = CreteEngine(); |
| 43 | + } |
31 | 44 |
|
32 |
| - engine = new Engine() |
| 45 | + private static Engine CreteEngine() |
| 46 | + { |
| 47 | + var engine = new Engine() |
33 | 48 | .SetValue("log", new Action<object>(Console.WriteLine))
|
34 | 49 | .SetValue("assert", new Action<bool>(b => { }));
|
35 | 50 |
|
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; |
42 | 61 | }
|
43 | 62 |
|
44 |
| - [ParamsSource(nameof(FileNames))] |
45 |
| - public string FileName { get; set; } |
| 63 | + [Params(false, true, Priority = 50)] |
| 64 | + public bool Modern { get; set; } |
46 | 65 |
|
47 |
| - [Params(true, false)] |
| 66 | + [Params(true, false, Priority = 100)] |
48 | 67 | public bool Prepared { get; set; }
|
49 | 68 |
|
50 |
| - public IEnumerable<string> FileNames() |
| 69 | + [Benchmark] |
| 70 | + public void CoreEval() |
51 | 71 | {
|
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"); |
56 | 79 | }
|
57 | 80 |
|
58 | 81 | [Benchmark]
|
59 |
| - public void Run() |
| 82 | + public void ObjectArray() |
60 | 83 | {
|
| 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 | + |
61 | 109 | if (Prepared)
|
62 | 110 | {
|
63 |
| - engine.Execute(_prepared[FileName]); |
| 111 | + _engine.Execute(_prepared[finalName]); |
64 | 112 | }
|
65 | 113 | else
|
66 | 114 | {
|
67 |
| - engine.Execute(_files[FileName]); |
| 115 | + _engine.Execute(_files[finalName], finalName); |
68 | 116 | }
|
69 | 117 | }
|
70 | 118 | }
|
0 commit comments