DromaeoBenchmark.cs 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using BenchmarkDotNet.Attributes;
  6. namespace Jint.Benchmark
  7. {
  8. [MemoryDiagnoser]
  9. public class DromaeoBenchmark
  10. {
  11. public static readonly Dictionary<string, string> files = new Dictionary<string, string>
  12. {
  13. {"dromaeo-3d-cube", null},
  14. {"dromaeo-core-eval", null},
  15. {"dromaeo-object-array", null},
  16. {"dromaeo-object-regexp", null},
  17. {"dromaeo-object-string", null},
  18. {"dromaeo-string-base64", null}
  19. };
  20. private Engine engine;
  21. [GlobalSetup]
  22. public void Setup()
  23. {
  24. foreach (var fileName in files.Keys.ToList())
  25. {
  26. files[fileName] = File.ReadAllText($"Scripts/dromaeo/{fileName}.js");
  27. }
  28. engine = new Engine()
  29. .SetValue("log", new Action<object>(Console.WriteLine))
  30. .SetValue("assert", new Action<bool>(b => { }));
  31. engine.Execute(@"
  32. var startTest = function () { };
  33. var test = function (name, fn) { fn(); };
  34. var endTest = function () { };
  35. var prep = function (fn) { fn(); };
  36. ");
  37. }
  38. [ParamsSource(nameof(FileNames))]
  39. public string FileName { 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. engine.Execute(files[FileName]);
  51. }
  52. }
  53. }