EngineComparisonBenchmark.cs 2.9 KB

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