EngineComparisonBenchmark.cs 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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, 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. var javaScriptParser = new JavaScriptParser();
  37. foreach (var fileName in _files.Keys.ToList())
  38. {
  39. var script = File.ReadAllText($"Scripts/{fileName}.js");
  40. if (fileName.Contains("dromaeo"))
  41. {
  42. script = _dromaeoHelpers + Environment.NewLine + Environment.NewLine + script;
  43. }
  44. _files[fileName] = script;
  45. _parsedScripts[fileName] = javaScriptParser.ParseScript(script, strict: true);
  46. }
  47. }
  48. [ParamsSource(nameof(FileNames))]
  49. public string FileName { get; set; }
  50. public IEnumerable<string> FileNames()
  51. {
  52. return _files.Select(entry => entry.Key);
  53. }
  54. [Benchmark]
  55. public void Jint()
  56. {
  57. var engine = new Engine(static options => options.Strict());
  58. engine.Execute(_files[FileName]);
  59. }
  60. [Benchmark]
  61. public void Jint_ParsedScript()
  62. {
  63. var engine = new Engine(static options => options.Strict());
  64. engine.Execute(_parsedScripts[FileName]);
  65. }
  66. [Benchmark]
  67. public void Jurassic()
  68. {
  69. var engine = new Jurassic.ScriptEngine { ForceStrictMode = true };
  70. engine.Execute(_files[FileName]);
  71. }
  72. [Benchmark]
  73. public void NilJS()
  74. {
  75. var engine = new NiL.JS.Core.Context(strict: true);
  76. engine.Eval(_files[FileName]);
  77. }
  78. [Benchmark]
  79. public void YantraJS()
  80. {
  81. var engine = new YantraJS.Core.JSContext();
  82. // By default YantraJS is strict mode only, in strict mode
  83. // we need to pass `this` explicitly in global context
  84. // if script is expecting global context as `this`
  85. engine.Eval(_files[FileName], null, engine);
  86. }
  87. }