textRenderer.benchmark.ts 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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, transformMatrixBetweenRect} 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 = transformMatrixBetweenRect(configView, new Rect(0, 0, 1000, 1000));
  45. return (searchResults?: FlamegraphSearch) => {
  46. renderer.draw(flamegraph.configSpace, transform, searchResults);
  47. };
  48. };
  49. const makeDrawCenterScreen = (renderer: TextRenderer, flamegraph: Flamegraph) => {
  50. const configView = new Rect(
  51. flamegraph.configSpace.width * 0.25, // 25% to left
  52. 0,
  53. flamegraph.configSpace.width * 0.5, // 50% width
  54. 1000
  55. ).withY(0);
  56. const transform = transformMatrixBetweenRect(configView, new Rect(0, 0, 1000, 1000));
  57. return (searchResults?: FlamegraphSearch) => {
  58. renderer.draw(configView, transform, searchResults);
  59. };
  60. };
  61. const makeDrawRightSideOfScreen = (renderer: TextRenderer, flamegraph: Flamegraph) => {
  62. const configView = new Rect(
  63. flamegraph.configSpace.width * 0.75, // 75% to left
  64. 0,
  65. flamegraph.configSpace.width * 0.25, // 25% width
  66. 1000
  67. ).withY(0);
  68. const transform = transformMatrixBetweenRect(configView, new Rect(0, 0, 1000, 1000));
  69. return (searchResults?: FlamegraphSearch) => {
  70. renderer.draw(configView, transform, searchResults);
  71. };
  72. };
  73. const tsProfile = importProfile(typescriptTrace as any, '');
  74. const tsFlamegraph = new Flamegraph(tsProfile.profiles[0], 0, {
  75. inverted: false,
  76. leftHeavy: false,
  77. });
  78. const androidProfile = importProfile(androidTrace as any, '');
  79. const androidFlamegraph = new Flamegraph(
  80. androidProfile.profiles[androidProfile.activeProfileIndex] as any,
  81. 0,
  82. {
  83. inverted: false,
  84. leftHeavy: false,
  85. }
  86. );
  87. const iosProfile = importProfile(ios as any, '');
  88. const iosFlamegraph = new Flamegraph(
  89. iosProfile.profiles[iosProfile.activeProfileIndex] as any,
  90. 0,
  91. {
  92. inverted: false,
  93. leftHeavy: false,
  94. }
  95. );
  96. const makeTextRenderer = flamegraph =>
  97. new TextRenderer(
  98. {
  99. clientWidth: 1000,
  100. clientHeight: 1000,
  101. clientLeft: 0,
  102. clientTop: 0,
  103. getContext: () => {
  104. return {
  105. fillRect: () => {},
  106. fillText: () => {},
  107. measureText: (t: string) => {
  108. return {
  109. width: t.length,
  110. };
  111. },
  112. };
  113. },
  114. },
  115. flamegraph,
  116. LightFlamegraphTheme
  117. );
  118. interface FramePartitionData {
  119. count: number;
  120. frames: Set<FlamegraphFrame>;
  121. }
  122. const makeSearchResults = (flamegraph: Flamegraph): FlamegraphSearch => {
  123. const framesPartitionedByWords = flamegraph.frames.reduce((acc, frame) => {
  124. const words = frame.frame.name.split(' ');
  125. words.forEach(w => {
  126. if (!acc[w]) {
  127. acc[w] = {
  128. count: 0,
  129. frames: new Set(),
  130. };
  131. }
  132. const node = acc[w];
  133. node.count++;
  134. node.frames.add(frame);
  135. });
  136. return acc;
  137. }, {} as Record<string, FramePartitionData>);
  138. const [word, data] = maxBy(
  139. Object.entries(framesPartitionedByWords),
  140. ([_, partitionData]) => {
  141. return partitionData.frames.size;
  142. }
  143. )!;
  144. return {
  145. results: Array.from(data.frames.values()).reduce((acc, frame) => {
  146. acc[getFlamegraphFrameSearchId(frame)] = frame;
  147. return acc;
  148. }, {}),
  149. query: word,
  150. };
  151. };
  152. const suite = (name: string, textRenderer: TextRenderer, flamegraph: Flamegraph) => {
  153. const results = makeSearchResults(flamegraph);
  154. benchmark(`${name} (full profile)`, () =>
  155. makeDrawFullScreen(textRenderer, flamegraph)(new Map())
  156. );
  157. benchmark(`${name} (center half)`, () =>
  158. makeDrawCenterScreen(textRenderer, flamegraph)(new Map())
  159. );
  160. benchmark(`${name} (right quarter)`, () =>
  161. makeDrawRightSideOfScreen(textRenderer, flamegraph)(new Map())
  162. );
  163. benchmark(
  164. `${name} (full profile, w/ search matching ${flamegraph.frames.length} of ${flamegraph.frames.length})`,
  165. () => makeDrawFullScreen(textRenderer, flamegraph)(results)
  166. );
  167. benchmark(
  168. `${name} (center half, w/ search ${flamegraph.frames.length} of ${flamegraph.frames.length})`,
  169. () => makeDrawCenterScreen(textRenderer, flamegraph)(results)
  170. );
  171. benchmark(
  172. `${name} (right quarter, w/ search ${flamegraph.frames.length} of ${flamegraph.frames.length})`,
  173. () => makeDrawRightSideOfScreen(textRenderer, flamegraph)(results)
  174. );
  175. };
  176. suite('typescript', makeTextRenderer(tsFlamegraph), tsFlamegraph);
  177. suite('android', makeTextRenderer(androidFlamegraph), androidFlamegraph);
  178. suite('ios', makeTextRenderer(iosFlamegraph), iosFlamegraph);