textRenderer.benchmark.ts 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. // Benchmarks allow us to make changes and evaluate performance before the code gets shipped to production.
  2. // They can be used to make performance improvements or to test impact of newly added functionality.
  3. // Run with: yarn run ts-node --project ./config/tsconfig.benchmark.json -r tsconfig-paths/register static/app/utils/profiling/renderers/textRenderer.benchmark.ts
  4. import benchmarkjs from 'benchmark';
  5. import maxBy from 'lodash/maxBy';
  6. import {initializeLocale} from 'sentry/bootstrap/initializeLocale';
  7. import {FlamegraphSearch} from 'sentry/utils/profiling/flamegraph/flamegraphStateProvider/flamegraphSearch';
  8. import {TextRenderer} from 'sentry/utils/profiling/renderers/textRenderer';
  9. import {Flamegraph} from '../flamegraph';
  10. import {LightFlamegraphTheme} from '../flamegraph/flamegraphTheme';
  11. import {Rect, Transform} from '../gl/utils';
  12. import androidTrace from '../profile/formats/android/trace.json';
  13. import ios from '../profile/formats/ios/trace.json';
  14. import typescriptTrace from '../profile/formats/typescript/trace.json';
  15. import {importProfile} from '../profile/importProfile';
  16. import {FlamegraphFrame, getFlamegraphFrameSearchId} from './../flamegraphFrame';
  17. // This logs an error which is annoying to see in the outputs
  18. initializeLocale({} as any);
  19. // We dont compare benchmark results, as we are testing a single version of the code, so we run this as a baseline,
  20. // store the results somewhere locally and then compare the results with the new version of our code.
  21. function benchmark(name: string, callback: () => void) {
  22. const suite = new benchmarkjs.Suite();
  23. suite
  24. .add(name, callback)
  25. .on('cycle', event => {
  26. // well, we need to see the results somewhere
  27. // eslint-disable-next-line
  28. console.log(event.target.toString());
  29. })
  30. .on('error', event => {
  31. // If something goes wrong, fail early
  32. throw event;
  33. });
  34. suite.run({async: false});
  35. }
  36. global.window = {devicePixelRatio: 1};
  37. const makeDrawFullScreen = (renderer: TextRenderer, flamegraph: Flamegraph) => {
  38. const configView = new Rect(
  39. 0,
  40. 0,
  41. flamegraph.configSpace.width, // 50% width
  42. 1000
  43. ).withY(0);
  44. const transform = Transform.transformMatrixBetweenRect(
  45. configView,
  46. new Rect(0, 0, 1000, 1000)
  47. );
  48. return (searchResults?: FlamegraphSearch) => {
  49. renderer.draw(flamegraph.configSpace, transform, searchResults);
  50. };
  51. };
  52. const makeDrawCenterScreen = (renderer: TextRenderer, flamegraph: Flamegraph) => {
  53. const configView = new Rect(
  54. flamegraph.configSpace.width * 0.25, // 25% to left
  55. 0,
  56. flamegraph.configSpace.width * 0.5, // 50% width
  57. 1000
  58. ).withY(0);
  59. const transform = Transform.transformMatrixBetweenRect(
  60. configView,
  61. new Rect(0, 0, 1000, 1000)
  62. );
  63. return (searchResults?: FlamegraphSearch) => {
  64. renderer.draw(configView, transform, searchResults);
  65. };
  66. };
  67. const makeDrawRightSideOfScreen = (renderer: TextRenderer, flamegraph: Flamegraph) => {
  68. const configView = new Rect(
  69. flamegraph.configSpace.width * 0.75, // 75% to left
  70. 0,
  71. flamegraph.configSpace.width * 0.25, // 25% width
  72. 1000
  73. ).withY(0);
  74. const transform = Transform.transformMatrixBetweenRect(
  75. configView,
  76. new Rect(0, 0, 1000, 1000)
  77. );
  78. return (searchResults?: FlamegraphSearch) => {
  79. renderer.draw(configView, transform, searchResults);
  80. };
  81. };
  82. const tsProfile = importProfile(typescriptTrace as any, '');
  83. const tsFlamegraph = new Flamegraph(tsProfile.profiles[0], 0, {
  84. inverted: false,
  85. leftHeavy: false,
  86. });
  87. const androidProfile = importProfile(androidTrace as any, '');
  88. const androidFlamegraph = new Flamegraph(
  89. androidProfile.profiles[androidProfile.activeProfileIndex] as any,
  90. 0,
  91. {
  92. inverted: false,
  93. leftHeavy: false,
  94. }
  95. );
  96. const iosProfile = importProfile(ios as any, '');
  97. const iosFlamegraph = new Flamegraph(
  98. iosProfile.profiles[iosProfile.activeProfileIndex] as any,
  99. 0,
  100. {
  101. inverted: false,
  102. leftHeavy: false,
  103. }
  104. );
  105. const makeTextRenderer = flamegraph =>
  106. new TextRenderer(
  107. {
  108. clientWidth: 1000,
  109. clientHeight: 1000,
  110. clientLeft: 0,
  111. clientTop: 0,
  112. getContext: () => {
  113. return {
  114. fillRect: () => {},
  115. fillText: () => {},
  116. measureText: (t: string) => {
  117. return {
  118. width: t.length,
  119. };
  120. },
  121. };
  122. },
  123. },
  124. flamegraph,
  125. LightFlamegraphTheme
  126. );
  127. interface FramePartitionData {
  128. count: number;
  129. frames: Set<FlamegraphFrame>;
  130. }
  131. const makeSearchResults = (flamegraph: Flamegraph): FlamegraphSearch => {
  132. const framesPartitionedByWords = flamegraph.frames.reduce((acc, frame) => {
  133. const words = frame.frame.name.split(' ');
  134. words.forEach(w => {
  135. if (!acc[w]) {
  136. acc[w] = {
  137. count: 0,
  138. frames: new Set(),
  139. };
  140. }
  141. const node = acc[w];
  142. node.count++;
  143. node.frames.add(frame);
  144. });
  145. return acc;
  146. }, {} as Record<string, FramePartitionData>);
  147. const [word, data] = maxBy(
  148. Object.entries(framesPartitionedByWords),
  149. ([_, partitionData]) => {
  150. return partitionData.frames.size;
  151. }
  152. )!;
  153. return {
  154. results: Array.from(data.frames.values()).reduce((acc, frame) => {
  155. acc[getFlamegraphFrameSearchId(frame)] = frame;
  156. return acc;
  157. }, {}),
  158. query: word,
  159. };
  160. };
  161. const suite = (name: string, textRenderer: TextRenderer, flamegraph: Flamegraph) => {
  162. const results = makeSearchResults(flamegraph);
  163. benchmark(`${name} (full profile)`, () =>
  164. makeDrawFullScreen(textRenderer, flamegraph)(new Map())
  165. );
  166. benchmark(`${name} (center half)`, () =>
  167. makeDrawCenterScreen(textRenderer, flamegraph)(new Map())
  168. );
  169. benchmark(`${name} (right quarter)`, () =>
  170. makeDrawRightSideOfScreen(textRenderer, flamegraph)(new Map())
  171. );
  172. benchmark(
  173. `${name} (full profile, w/ search matching ${flamegraph.frames.length} of ${flamegraph.frames.length})`,
  174. () => makeDrawFullScreen(textRenderer, flamegraph)(results)
  175. );
  176. benchmark(
  177. `${name} (center half, w/ search ${flamegraph.frames.length} of ${flamegraph.frames.length})`,
  178. () => makeDrawCenterScreen(textRenderer, flamegraph)(results)
  179. );
  180. benchmark(
  181. `${name} (right quarter, w/ search ${flamegraph.frames.length} of ${flamegraph.frames.length})`,
  182. () => makeDrawRightSideOfScreen(textRenderer, flamegraph)(results)
  183. );
  184. };
  185. suite('typescript', makeTextRenderer(tsFlamegraph), tsFlamegraph);
  186. suite('android', makeTextRenderer(androidFlamegraph), androidFlamegraph);
  187. suite('ios', makeTextRenderer(iosFlamegraph), iosFlamegraph);