flamegraphTextRenderer.benchmark.ts 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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 '../../../bootstrap/initializeLocale';
  7. import {Flamegraph} from '../flamegraph';
  8. import {FlamegraphSearch} from '../flamegraph/flamegraphStateProvider/reducers/flamegraphSearch';
  9. import {LightFlamegraphTheme} from '../flamegraph/flamegraphTheme';
  10. import {FlamegraphFrame, getFlamegraphFrameSearchId} from '../flamegraphFrame';
  11. import {transformMatrixBetweenRect} from '../gl/utils';
  12. import androidTrace from '../profile/formats/android/trace.json';
  13. import ios from '../profile/formats/ios/trace.json';
  14. import {importProfile} from '../profile/importProfile';
  15. import {Rect} from '../speedscope';
  16. import {FlamegraphTextRenderer} from './flamegraphTextRenderer';
  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: FlamegraphTextRenderer, 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 = (
  50. renderer: FlamegraphTextRenderer,
  51. flamegraph: Flamegraph
  52. ) => {
  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 = transformMatrixBetweenRect(configView, new Rect(0, 0, 1000, 1000));
  60. return (searchResults?: FlamegraphSearch) => {
  61. renderer.draw(configView, transform, searchResults);
  62. };
  63. };
  64. const makeDrawRightSideOfScreen = (
  65. renderer: FlamegraphTextRenderer,
  66. flamegraph: Flamegraph
  67. ) => {
  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 = transformMatrixBetweenRect(configView, new Rect(0, 0, 1000, 1000));
  75. return (searchResults?: FlamegraphSearch) => {
  76. renderer.draw(configView, transform, searchResults);
  77. };
  78. };
  79. const androidProfile = importProfile(androidTrace as any, '', 'flamechart');
  80. const androidFlamegraph = new Flamegraph(
  81. androidProfile.profiles[androidProfile.activeProfileIndex] as any,
  82. 0,
  83. {
  84. inverted: false,
  85. sort: 'call order',
  86. }
  87. );
  88. const iosProfile = importProfile(ios as any, '', 'flamechart');
  89. const iosFlamegraph = new Flamegraph(
  90. iosProfile.profiles[iosProfile.activeProfileIndex] as any,
  91. 0,
  92. {
  93. inverted: false,
  94. sort: 'call order',
  95. }
  96. );
  97. const makeTextRenderer = flamegraph =>
  98. new FlamegraphTextRenderer(
  99. {
  100. clientWidth: 1000,
  101. clientHeight: 1000,
  102. clientLeft: 0,
  103. clientTop: 0,
  104. getContext: () => {
  105. return {
  106. fillRect: () => {},
  107. fillText: () => {},
  108. measureText: (t: string) => {
  109. return {
  110. width: t.length,
  111. };
  112. },
  113. };
  114. },
  115. },
  116. LightFlamegraphTheme,
  117. flamegraph
  118. );
  119. interface FramePartitionData {
  120. count: number;
  121. frames: Set<FlamegraphFrame>;
  122. }
  123. const makeSearchResults = (flamegraph: Flamegraph): FlamegraphSearch => {
  124. const framesPartitionedByWords = flamegraph.frames.reduce(
  125. (acc, frame) => {
  126. const words = frame.frame.name.split(' ');
  127. words.forEach(w => {
  128. if (!acc[w]) {
  129. acc[w] = {
  130. count: 0,
  131. frames: new Set(),
  132. };
  133. }
  134. const node = acc[w];
  135. node.count++;
  136. node.frames.add(frame);
  137. });
  138. return acc;
  139. },
  140. {} as Record<string, FramePartitionData>
  141. );
  142. const [word, data] = maxBy(
  143. Object.entries(framesPartitionedByWords),
  144. ([_, partitionData]) => {
  145. return partitionData.frames.size;
  146. }
  147. )!;
  148. return {
  149. results: Array.from(data.frames.values()).reduce((acc, frame) => {
  150. acc[getFlamegraphFrameSearchId(frame)] = frame;
  151. return acc;
  152. }, {}),
  153. query: word,
  154. };
  155. };
  156. const suite = (
  157. name: string,
  158. textRenderer: FlamegraphTextRenderer,
  159. flamegraph: Flamegraph
  160. ) => {
  161. const results = makeSearchResults(flamegraph);
  162. benchmark(`${name} (full profile)`, () =>
  163. makeDrawFullScreen(textRenderer, flamegraph)(new Map())
  164. );
  165. benchmark(`${name} (center half)`, () =>
  166. makeDrawCenterScreen(textRenderer, flamegraph)(new Map())
  167. );
  168. benchmark(`${name} (right quarter)`, () =>
  169. makeDrawRightSideOfScreen(textRenderer, flamegraph)(new Map())
  170. );
  171. benchmark(
  172. `${name} (full profile, w/ search matching ${flamegraph.frames.length} of ${flamegraph.frames.length})`,
  173. () => makeDrawFullScreen(textRenderer, flamegraph)(results)
  174. );
  175. benchmark(
  176. `${name} (center half, w/ search ${flamegraph.frames.length} of ${flamegraph.frames.length})`,
  177. () => makeDrawCenterScreen(textRenderer, flamegraph)(results)
  178. );
  179. benchmark(
  180. `${name} (right quarter, w/ search ${flamegraph.frames.length} of ${flamegraph.frames.length})`,
  181. () => makeDrawRightSideOfScreen(textRenderer, flamegraph)(results)
  182. );
  183. };
  184. suite('android', makeTextRenderer(androidFlamegraph), androidFlamegraph);
  185. suite('ios', makeTextRenderer(iosFlamegraph), iosFlamegraph);