SunSpiderBenchmark.cs 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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 SunSpiderBenchmark
  11. {
  12. private static readonly Dictionary<string, string> files = new Dictionary<string, string>
  13. {
  14. {"3d-cube", null},
  15. {"3d-morph", null},
  16. {"3d-raytrace", null},
  17. {"access-binary-trees", null},
  18. {"access-fannkuch", null},
  19. {"access-nbody", null},
  20. {"access-nsieve", null},
  21. {"bitops-3bit-bits-in-byte", null},
  22. {"bitops-bits-in-byte", null},
  23. {"bitops-bitwise-and", null},
  24. {"bitops-nsieve-bits", null},
  25. {"controlflow-recursive", null},
  26. {"crypto-aes", null},
  27. {"crypto-md5", null},
  28. {"crypto-sha1", null},
  29. {"date-format-tofte", null},
  30. {"date-format-xparb", null},
  31. {"math-cordic", null},
  32. {"math-partial-sums", null},
  33. {"math-spectral-norm", null},
  34. {"regexp-dna", null},
  35. {"string-base64", null},
  36. {"string-fasta", null},
  37. {"string-tagcloud", null},
  38. {"string-unpack-code", null},
  39. {"string-validate-input", null}
  40. };
  41. private Engine engine;
  42. [GlobalSetup]
  43. public void Setup()
  44. {
  45. foreach (var fileName in files.Keys.ToList())
  46. {
  47. files[fileName] = File.ReadAllText($"SunSpider/{fileName}.js");
  48. }
  49. engine = new Engine()
  50. .SetValue("log", new Action<object>(Console.WriteLine))
  51. .SetValue("assert", new Action<bool>(b => { }));
  52. }
  53. [ParamsSource(nameof(FileNames))]
  54. public string FileName { get; set; }
  55. public IEnumerable<string> FileNames()
  56. {
  57. foreach (var entry in files)
  58. {
  59. yield return entry.Key;
  60. }
  61. }
  62. [Benchmark]
  63. public void Run()
  64. {
  65. engine.Execute(files[FileName]);
  66. }
  67. }
  68. }