No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

BenchmarkGenerator.cs 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. using NUnit.Framework;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Diagnostics;
  5. using System.Reflection;
  6. using UnityEngine;
  7. namespace Unity.PerformanceTesting.Benchmark
  8. {
  9. /// <summary>
  10. /// Generates and saves a markdown file after running benchmarks.
  11. /// </summary>
  12. public static class BenchmarkGenerator
  13. {
  14. // This must have the same number of elements as there are bits in the flags parameter for GetFlagSuperscripts
  15. static string[] superscripts = { "¹", "²", "³", "⁴", "⁵", "⁶", "⁷", "⁸", "⁹",
  16. "¹⁰", "¹¹", "¹²", "¹³", "¹⁴", "¹⁵", "¹⁶", "¹⁷", "¹⁸", "¹⁹",
  17. "²⁰", "²¹", "²²", "²³", "²⁴", "²⁵", "²⁶", "²⁷", "²⁸", "²⁹",
  18. "³⁰", "³¹", "³²"
  19. };
  20. static string[] superscriptDesc =
  21. {
  22. "Optimizations were disabled to perform this benchmark",
  23. "Benchmark run on parallel job workers - results may vary",
  24. };
  25. static string GetFlagSuperscripts(uint flags)
  26. {
  27. string ret = "";
  28. for (int f = 0; f < sizeof(uint) * 8; f++)
  29. {
  30. if ((flags & (1 << f)) != 0)
  31. {
  32. if (ret.Length > 0)
  33. ret += "˒";
  34. ret += superscripts[f];
  35. }
  36. }
  37. return ret;
  38. }
  39. /// <summary>
  40. /// First, runs benchmarks for all benchmark methods in all types attributed with [Benchmark(benchmarkEnumType)].
  41. /// Then, generates a report in markdown with these results, and saves to the requested file path.<para />
  42. /// A common integration method is to call this directly from a menu item handler.
  43. /// </summary>
  44. /// <param name="title">The title of the entire benchmark report</param>
  45. /// <param name="benchmarkEnumType">An enum with a <see cref="BenchmarkComparisonAttribute"/> which is specified in all <see cref="BenchmarkAttribute"/>s marking
  46. /// classes which contain performance methods to be benchmarked. All performance test methods in the class
  47. /// must contain a parameter of the enum marked with <see cref="BenchmarkComparisonAttribute"/> which is specified in the class's
  48. /// <see cref="BenchmarkAttribute"/>, and may not contain any other parameter with another enum marked with <see cref="BenchmarkComparisonAttribute"/>.</param>
  49. /// <param name="filePath">The output file path to save the generated markdown to.</param>
  50. /// <param name="description">A global description for the entire benchmark report, or null.</param>
  51. /// <param name="notesTitle">The title for a global "notes" section for the entire benchmark report, or null.</param>
  52. /// <param name="notes">An array of notes in the previously mentioned global "notes" section for the entire benchmark report, or null.</param>
  53. /// <exception cref="ArgumentException">Thrown for any errors in defining the benchmarks.</exception>
  54. public static void GenerateMarkdown(string title, Type benchmarkEnumType, string filePath, string description = null, string notesTitle = null, string[] notes = null)
  55. {
  56. var attrBenchmarkComparison = benchmarkEnumType.GetCustomAttribute<BenchmarkComparisonAttribute>();
  57. if (attrBenchmarkComparison == null)
  58. throw new ArgumentException($"{benchmarkEnumType.Name} is not a valid benchmark comparison enum type as it is not decorated with [{nameof(BenchmarkComparisonAttribute)}]");
  59. Stopwatch timer = new Stopwatch();
  60. timer.Start();
  61. var assemblies = AppDomain.CurrentDomain.GetAssemblies();
  62. var benchmarkTypes = new List<Type>();
  63. foreach (Assembly assembly in assemblies)
  64. {
  65. var types = assembly.GetTypes();
  66. foreach(var t in types)
  67. {
  68. var cads = t.GetCustomAttributesData();
  69. foreach (var cad in cads)
  70. {
  71. if (cad.AttributeType != typeof(BenchmarkAttribute))
  72. continue;
  73. if ((Type)cad.ConstructorArguments[0].Value == benchmarkEnumType &&
  74. (bool)cad.ConstructorArguments[1].Value == false)
  75. benchmarkTypes.Add(t);
  76. }
  77. }
  78. }
  79. UnityEngine.Debug.Log($"Took {timer.Elapsed}s to find all types with [Benchmark(typeof({benchmarkEnumType.Name}))]");
  80. timer.Restart();
  81. GenerateMarkdown(title, benchmarkTypes.ToArray(), filePath, description, notesTitle, notes);
  82. UnityEngine.Debug.Log($"Took {timer.Elapsed}s to benchmark all types with [Benchmark(typeof({benchmarkEnumType.Name}))]");
  83. }
  84. /// <summary>
  85. /// First, runs benchmarks for all benchmark methods in all given types.<br />
  86. /// Then, generates a report in markdown with these results, and saves to the requested file path.
  87. /// </summary>
  88. /// <param name="title">The title of the entire benchmark report</param>
  89. /// <param name="benchmarkTypes">An array of Types each annotated with a <see cref="BenchmarkAttribute"/> for comparison. Each Type may
  90. /// refer to a class with different arguments to the <see cref="BenchmarkAttribute"/> if desired, but all performance test methods in the class
  91. /// must each contain a parameter of the enum marked with <see cref="BenchmarkComparisonAttribute"/> which is specified in the class's
  92. /// <see cref="BenchmarkAttribute"/>, and may not contain any other parameter with another enum marked with <see cref="BenchmarkComparisonAttribute"/>.</param>
  93. /// <param name="filePath">The output file path to save the generated markdown to.</param>
  94. /// <param name="description">A global description for the entire benchmark report, or null.</param>
  95. /// <param name="notesTitle">The title for a global "notes" section for the entire benchmark report, or null.</param>
  96. /// <param name="notes">An array of notes in the previously mentioned global "notes" section for the entire benchmark report, or null.</param>
  97. /// <exception cref="ArgumentException">Thrown for any errors in defining the benchmarks.</exception>
  98. public static void GenerateMarkdown(string title, Type[] benchmarkTypes, string filePath, string description = null, string notesTitle = null, string[] notes = null)
  99. {
  100. using (var reports = BenchmarkRunner.RunBenchmarks(title, benchmarkTypes))
  101. {
  102. MarkdownBuilder md = new MarkdownBuilder();
  103. md.Header(1, $"Performance Comparison: {reports.reportName}");
  104. int versionFilter = Application.unityVersion.IndexOf('-');
  105. md.Note($"<span style=\"color:red\">This file is auto-generated</span>",
  106. $"All measurments were taken on {SystemInfo.processorType} with {SystemInfo.processorCount} logical cores.",
  107. $"Unity Editor version: {Application.unityVersion.Substring(0, versionFilter == -1 ? Application.unityVersion.Length : versionFilter)}",
  108. "To regenerate this file locally use: **DOTS -> Unity.Collections -> Generate &ast;&ast;&ast;** menu.");
  109. // Generate ToC
  110. const string kSectionBenchmarkResults = "Benchmark Results";
  111. md.Header(2, "Table of Contents");
  112. md.ListItem(0).LinkHeader(kSectionBenchmarkResults).Br();
  113. foreach (var group in reports.groups)
  114. md.ListItem(1).LinkHeader(group.groupName.ToString()).Br();
  115. // Generate benchmark tables
  116. md.Header(2, kSectionBenchmarkResults);
  117. // Report description and notes first
  118. if (description != null && description.Length > 0)
  119. {
  120. md.AppendLine(description);
  121. md.BrParagraph();
  122. }
  123. if (notes != null && notes.Length > 0)
  124. {
  125. if (notesTitle != null && notesTitle.Length > 0)
  126. md.Note(notesTitle, notes);
  127. else
  128. md.Note(notes);
  129. }
  130. // Report each group results as ordered in the table of contents
  131. foreach (var group in reports.groups)
  132. {
  133. md.BrParagraph().Header(3, $"*{group.groupName}*");
  134. string[] titles = new string[group.variantNames.Length];
  135. for (int i = 0; i < titles.Length; i++)
  136. {
  137. titles[i] = group.variantNames[i].ToString();
  138. switch (group.resultTypes[i])
  139. {
  140. case BenchmarkResultType.ExternalBaseline:
  141. case BenchmarkResultType.External:
  142. titles[i] = $"*{titles[i]}*";
  143. break;
  144. }
  145. }
  146. md.TableHeader(false, "Functionality", true, titles);
  147. uint tableFlags = 0;
  148. // Find max amount of alignment spacing needed
  149. int[] ratioSpace = new int[group.variantNames.Length];
  150. foreach (var comparison in group.comparisons)
  151. {
  152. for (int i = 0; i < ratioSpace.Length; i++)
  153. {
  154. if (comparison.results[i].ranking == BenchmarkRankingType.Ignored)
  155. continue;
  156. int ratio10 = Mathf.RoundToInt((float)(comparison.results[i].baselineRatio * 10));
  157. int pow10 = 0;
  158. while (ratio10 >= 100)
  159. {
  160. pow10++;
  161. ratio10 /= 10;
  162. }
  163. ratioSpace[i] = Mathf.Max(ratioSpace[i], pow10);
  164. }
  165. }
  166. foreach (var comparison in group.comparisons)
  167. {
  168. uint rowFlags = comparison.footnoteFlags;
  169. int items = comparison.results.Length;
  170. var tableData = new string[items];
  171. for (int i = 0; i < items; i++)
  172. {
  173. if (comparison.results[i].ranking == BenchmarkRankingType.Ignored)
  174. {
  175. tableData[i] = "---";
  176. continue;
  177. }
  178. string format = $"{{0:F{group.resultDecimalPlaces}}}";
  179. string result = $"{string.Format(format, comparison.results[i].Comparator)}{comparison.results[i].UnitSuffix}";
  180. string speedup = $"({comparison.results[i].baselineRatio:F1}x)";
  181. rowFlags |= comparison.results[i].resultFlags;
  182. int ratio10 = Mathf.RoundToInt((float)(comparison.results[i].baselineRatio * 10));
  183. if (ratio10 > 10)
  184. speedup = $"<span style=\"color:green\">{speedup}</span>";
  185. else if (ratio10 < 10)
  186. speedup = $"<span style=\"color:red\">{speedup}</span>";
  187. else
  188. speedup = $"<span style=\"color:grey\">{speedup}</span>";
  189. int alignSpaces = ratioSpace[i];
  190. while (ratio10 >= 100)
  191. {
  192. alignSpaces--;
  193. ratio10 /= 10;
  194. }
  195. speedup = $"{new string(' ', alignSpaces)}{speedup}";
  196. tableData[i] = $"{result} {speedup}";
  197. switch (group.resultTypes[i])
  198. {
  199. case BenchmarkResultType.ExternalBaseline:
  200. case BenchmarkResultType.External:
  201. tableData[i] = $"*{tableData[i]}*";
  202. break;
  203. }
  204. switch (comparison.results[i].ranking)
  205. {
  206. case BenchmarkRankingType.Normal:
  207. tableData[i] = $"{tableData[i]}&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"; // those 2 spaces are unicode en-space because >1 ASCII code spaces collapse
  208. break;
  209. case BenchmarkRankingType.Best:
  210. tableData[i] = $"{tableData[i]}&nbsp;🟢";
  211. break;
  212. case BenchmarkRankingType.Worst:
  213. tableData[i] = $"{tableData[i]}&nbsp;🟠";
  214. break;
  215. }
  216. }
  217. tableFlags |= rowFlags;
  218. if (rowFlags != 0)
  219. md.TableRow($"`{comparison.comparisonName}`*{GetFlagSuperscripts(rowFlags)}*", tableData);
  220. else
  221. md.TableRow($"`{comparison.comparisonName}`", tableData);
  222. }
  223. md.Br();
  224. for (int f = 0; f < 32; f++)
  225. {
  226. if ((tableFlags & (1 << f)) != 0)
  227. {
  228. if (f < superscriptDesc.Length)
  229. md.AppendLine($"*{superscripts[f]}* {superscriptDesc[f]}");
  230. else
  231. md.AppendLine($"*{superscripts[f]}* {group.customFootnotes[1u << f]}");
  232. }
  233. }
  234. md.HorizontalLine();
  235. }
  236. md.Save(filePath);
  237. }
  238. }
  239. }
  240. }