DromaeoBenchmark.cs 1.6 KB

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