Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support running DromaeoBenchmark with modern JS syntax #2041

Merged
merged 1 commit into from
Jan 26, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
106 changes: 77 additions & 29 deletions Jint.Benchmark/DromaeoBenchmark.cs
Original file line number Diff line number Diff line change
@@ -1,70 +1,118 @@
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Configs;

namespace Jint.Benchmark;

[MemoryDiagnoser]
[GroupBenchmarksBy(BenchmarkLogicalGroupRule.ByMethod)]
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}
{ "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 readonly Dictionary<string, Prepared<Script>> _prepared = new();

private Engine engine;
private Engine _engine;

[GlobalSetup]
public void Setup()
{
foreach (var fileName in _files.Keys)
foreach (var fileName in _files.Keys.ToArray())
{
var script = File.ReadAllText($"Scripts/{fileName}.js");
_files[fileName] = script;
_prepared[fileName] = Engine.PrepareScript(script);
foreach (var suffix in new[] {"", "-modern"})
{
var name = fileName + suffix;
var script = File.ReadAllText($"Scripts/{name}.js");
_files[name] = script;
_prepared[name] = Engine.PrepareScript(script, name);
}
}
}

[IterationSetup]
public void IterationSetup()
{
_engine = CreteEngine();
}

engine = new Engine()
private static Engine CreteEngine()
{
var 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(); };
");
engine.Execute("""

var startTest = function () { };
var test = function (name, fn) { fn(); };
var endTest = function () { };
var prep = function (fn) { fn(); };

""");

return engine;
}

[ParamsSource(nameof(FileNames))]
public string FileName { get; set; }
[Params(false, true, Priority = 50)]
public bool Modern { get; set; }

[Params(true, false)]
[Params(true, false, Priority = 100)]
public bool Prepared { get; set; }

public IEnumerable<string> FileNames()
[Benchmark]
public void CoreEval()
{
foreach (var entry in _files)
{
yield return entry.Key;
}
Run("dromaeo-core-eval");
}

[Benchmark]
public void Cube()
{
Run("dromaeo-3d-cube");
}

[Benchmark]
public void Run()
public void ObjectArray()
{
Run("dromaeo-object-array");
}

[Benchmark]
public void ObjectRegExp()
{
Run("dromaeo-object-regexp");
}

[Benchmark]
public void ObjectString()
{
Run("dromaeo-object-string");
}

[Benchmark]
public void StringBase64()
{
Run("dromaeo-string-base64");
}

private void Run(string fileName)
{
var finalName = Modern ? fileName + "-modern" : fileName;

if (Prepared)
{
engine.Execute(_prepared[FileName]);
_engine.Execute(_prepared[finalName]);
}
else
{
engine.Execute(_files[FileName]);
_engine.Execute(_files[finalName], finalName);
}
}
}
8 changes: 8 additions & 0 deletions Jint.Benchmark/EngineComparisonBenchmark.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,23 @@ public class EngineComparisonBenchmark
{
{ "array-stress", null },
{ "evaluation", null },
{ "evaluation-modern", null },
{ "linq-js", null },
{ "minimal", null },
{ "stopwatch", null },
{ "stopwatch-modern", null },
{ "dromaeo-3d-cube", null },
{ "dromaeo-3d-cube-modern", null },
{ "dromaeo-core-eval", null },
{ "dromaeo-core-eval-modern", null },
{ "dromaeo-object-array", null },
{ "dromaeo-object-array-modern", null },
{ "dromaeo-object-regexp", null },
{ "dromaeo-object-regexp-modern", null },
{ "dromaeo-object-string", null },
{ "dromaeo-object-string-modern", null },
{ "dromaeo-string-base64", null },
{ "dromaeo-string-base64-modern", null },
};

private static readonly string _dromaeoHelpers = @"
Expand Down
5 changes: 4 additions & 1 deletion Jint.Benchmark/EvaluationBenchmark.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,8 @@ namespace Jint.Benchmark;
[MemoryDiagnoser]
public class EvaluationBenchmark : SingleScriptBenchmark
{
protected override string FileName => "evaluation.js";
[Params(false, true)]
public bool Modern { get; set; }

protected override string FileName => Modern ? "evaluation-modern.js" : "evaluation.js";
}
Loading
Loading