EngineComparisonBenchmark.cs 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. using BenchmarkDotNet.Attributes;
  2. using BenchmarkDotNet.Configs;
  3. using BenchmarkDotNet.Order;
  4. namespace Jint.Benchmark;
  5. [RankColumn]
  6. [MemoryDiagnoser]
  7. [Orderer(SummaryOrderPolicy.FastestToSlowest)]
  8. [GroupBenchmarksBy(BenchmarkLogicalGroupRule.ByParams)]
  9. [HideColumns("Error", "Gen0", "Gen1", "Gen2")]
  10. [BenchmarkCategory("EngineComparison")]
  11. public class EngineComparisonBenchmark
  12. {
  13. private static readonly Dictionary<string, Prepared<Script>> _parsedScripts = new();
  14. private static readonly Dictionary<string, string> _files = new()
  15. {
  16. { "array-stress", null },
  17. { "evaluation", null },
  18. { "linq-js", null },
  19. { "minimal", null },
  20. { "stopwatch", null },
  21. { "dromaeo-3d-cube", null },
  22. { "dromaeo-core-eval", null },
  23. { "dromaeo-object-array", null },
  24. { "dromaeo-object-regexp", null },
  25. { "dromaeo-object-string", null },
  26. { "dromaeo-string-base64", null },
  27. };
  28. private static readonly string _dromaeoHelpers = @"
  29. var startTest = function () { };
  30. var test = function (name, fn) { fn(); };
  31. var endTest = function () { };
  32. var prep = function (fn) { fn(); };";
  33. [GlobalSetup]
  34. public void Setup()
  35. {
  36. foreach (var fileName in _files.Keys.ToList())
  37. {
  38. var script = File.ReadAllText($"Scripts/{fileName}.js");
  39. if (fileName.Contains("dromaeo"))
  40. {
  41. script = _dromaeoHelpers + Environment.NewLine + Environment.NewLine + script;
  42. }
  43. _files[fileName] = script;
  44. _parsedScripts[fileName] = Engine.PrepareScript(script, strict: true);
  45. }
  46. }
  47. [ParamsSource(nameof(FileNames))]
  48. public string FileName { get; set; }
  49. public IEnumerable<string> FileNames()
  50. {
  51. return _files.Select(entry => entry.Key);
  52. }
  53. [Benchmark]
  54. public void Jint()
  55. {
  56. var engine = new Engine(static options => options.Strict());
  57. engine.Execute(_files[FileName]);
  58. }
  59. [Benchmark]
  60. public void Jint_ParsedScript()
  61. {
  62. var engine = new Engine(static options => options.Strict());
  63. engine.Execute(_parsedScripts[FileName]);
  64. }
  65. [Benchmark]
  66. public void Jurassic()
  67. {
  68. var engine = new Jurassic.ScriptEngine { ForceStrictMode = true };
  69. engine.Execute(_files[FileName]);
  70. }
  71. [Benchmark]
  72. public void NilJS()
  73. {
  74. var engine = new NiL.JS.Core.Context(strict: true);
  75. engine.Eval(_files[FileName]);
  76. }
  77. [Benchmark]
  78. public void YantraJS()
  79. {
  80. var engine = new YantraJS.Core.JSContext();
  81. // By default YantraJS is strict mode only, in strict mode
  82. // we need to pass `this` explicitly in global context
  83. // if script is expecting global context as `this`
  84. engine.Eval(_files[FileName], null, engine);
  85. }
  86. }