DromaeoBenchmark.cs 1.5 KB

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