DromaeoBenchmark.cs 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. using BenchmarkDotNet.Attributes;
  2. namespace Jint.Benchmark;
  3. [MemoryDiagnoser]
  4. public class DromaeoBenchmark
  5. {
  6. public static readonly Dictionary<string, string> files = new Dictionary<string, string>
  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 Engine engine;
  16. [GlobalSetup]
  17. public void Setup()
  18. {
  19. foreach (var fileName in files.Keys.ToList())
  20. {
  21. files[fileName] = File.ReadAllText($"Scripts/{fileName}.js");
  22. }
  23. engine = new Engine()
  24. .SetValue("log", new Action<object>(Console.WriteLine))
  25. .SetValue("assert", new Action<bool>(b => { }));
  26. engine.Execute(@"
  27. var startTest = function () { };
  28. var test = function (name, fn) { fn(); };
  29. var endTest = function () { };
  30. var prep = function (fn) { fn(); };
  31. ");
  32. }
  33. [ParamsSource(nameof(FileNames))]
  34. public string FileName { get; set; }
  35. public IEnumerable<string> FileNames()
  36. {
  37. foreach (var entry in files)
  38. {
  39. yield return entry.Key;
  40. }
  41. }
  42. [Benchmark]
  43. public void Run()
  44. {
  45. engine.Execute(files[FileName]);
  46. }
  47. }