2
0

DromaeoBenchmark.cs 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. using BenchmarkDotNet.Attributes;
  2. using Esprima.Ast;
  3. namespace Jint.Benchmark;
  4. [MemoryDiagnoser]
  5. public class DromaeoBenchmark
  6. {
  7. private static readonly Dictionary<string, string> _files = new()
  8. {
  9. {"dromaeo-3d-cube", null},
  10. {"dromaeo-core-eval", null},
  11. {"dromaeo-object-array", null},
  12. {"dromaeo-object-regexp", null},
  13. {"dromaeo-object-string", null},
  14. {"dromaeo-string-base64", null}
  15. };
  16. private readonly Dictionary<string, Script> _prepared = new();
  17. private Engine engine;
  18. [GlobalSetup]
  19. public void Setup()
  20. {
  21. foreach (var fileName in _files.Keys)
  22. {
  23. var script = File.ReadAllText($"Scripts/{fileName}.js");
  24. _files[fileName] = script;
  25. _prepared[fileName] = Engine.PrepareScript(script);
  26. }
  27. engine = new Engine()
  28. .SetValue("log", new Action<object>(Console.WriteLine))
  29. .SetValue("assert", new Action<bool>(b => { }));
  30. engine.Execute(@"
  31. var startTest = function () { };
  32. var test = function (name, fn) { fn(); };
  33. var endTest = function () { };
  34. var prep = function (fn) { fn(); };
  35. ");
  36. }
  37. [ParamsSource(nameof(FileNames))]
  38. public string FileName { get; set; }
  39. [Params(true, false)]
  40. public bool Prepared { get; set; }
  41. public IEnumerable<string> FileNames()
  42. {
  43. foreach (var entry in _files)
  44. {
  45. yield return entry.Key;
  46. }
  47. }
  48. [Benchmark]
  49. public void Run()
  50. {
  51. if (Prepared)
  52. {
  53. engine.Execute(_prepared[FileName]);
  54. }
  55. else
  56. {
  57. engine.Execute(_files[FileName]);
  58. }
  59. }
  60. }