DromaeoBenchmark.cs 1.6 KB

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