flamegraph.spec.tsx 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  1. import {Flamegraph} from 'sentry/utils/profiling/flamegraph';
  2. import {Rect} from 'sentry/utils/profiling/gl/utils';
  3. import {EventedProfile} from 'sentry/utils/profiling/profile/eventedProfile';
  4. import {createFrameIndex} from 'sentry/utils/profiling/profile/utils';
  5. const makeEmptyEventedTrace = (): EventedProfile => {
  6. return EventedProfile.FromProfile(
  7. {
  8. name: 'profile',
  9. startValue: 0,
  10. endValue: 0,
  11. unit: 'microseconds',
  12. type: 'evented',
  13. threadID: 0,
  14. events: [],
  15. },
  16. createFrameIndex('mobile', [])
  17. );
  18. };
  19. describe('flamegraph', () => {
  20. it('sets default timeline for empty flamegraph', () => {
  21. const flamegraph = new Flamegraph(makeEmptyEventedTrace(), 0, {
  22. inverted: false,
  23. leftHeavy: false,
  24. });
  25. expect(flamegraph.configSpace.equals(new Rect(0, 0, 1_000_000, 0))).toBe(true);
  26. expect(flamegraph.inverted).toBe(false);
  27. expect(flamegraph.leftHeavy).toBe(false);
  28. });
  29. it('initializes formatter', () => {
  30. const trace: Profiling.EventedProfile = {
  31. name: 'profile',
  32. startValue: 0,
  33. endValue: 1000,
  34. unit: 'milliseconds',
  35. threadID: 0,
  36. type: 'evented',
  37. events: [
  38. {type: 'O', at: 0, frame: 0},
  39. {type: 'O', at: 500, frame: 1},
  40. {type: 'C', at: 600, frame: 1},
  41. {type: 'C', at: 1000, frame: 0},
  42. ],
  43. };
  44. const flamegraph = new Flamegraph(
  45. EventedProfile.FromProfile(
  46. trace,
  47. createFrameIndex('mobile', [{name: 'f0'}, {name: 'f1'}])
  48. ),
  49. 10,
  50. {
  51. inverted: true,
  52. leftHeavy: true,
  53. }
  54. );
  55. expect(flamegraph.formatter(1000)).toBe('1.00s');
  56. expect(flamegraph.formatter(500)).toBe('500.00ms');
  57. });
  58. it('stores profile properties', () => {
  59. const trace: Profiling.EventedProfile = {
  60. name: 'profile',
  61. startValue: 0,
  62. endValue: 1000,
  63. unit: 'milliseconds',
  64. threadID: 0,
  65. type: 'evented',
  66. events: [
  67. {type: 'O', at: 0, frame: 0},
  68. {type: 'O', at: 1, frame: 1},
  69. {type: 'C', at: 2, frame: 1},
  70. {type: 'C', at: 3, frame: 0},
  71. ],
  72. };
  73. const flamegraph = new Flamegraph(
  74. EventedProfile.FromProfile(
  75. trace,
  76. createFrameIndex('mobile', [{name: 'f0'}, {name: 'f1'}])
  77. ),
  78. 10,
  79. {
  80. inverted: true,
  81. leftHeavy: true,
  82. }
  83. );
  84. expect(flamegraph.inverted).toBe(true);
  85. expect(flamegraph.leftHeavy).toBe(true);
  86. expect(flamegraph.profileIndex).toBe(10);
  87. });
  88. it('creates a call order graph', () => {
  89. const trace: Profiling.EventedProfile = {
  90. name: 'profile',
  91. startValue: 0,
  92. endValue: 1000,
  93. unit: 'milliseconds',
  94. threadID: 0,
  95. type: 'evented',
  96. events: [
  97. {type: 'O', at: 0, frame: 0},
  98. {type: 'O', at: 1, frame: 1},
  99. {type: 'O', at: 2, frame: 2},
  100. {type: 'C', at: 3, frame: 2},
  101. {type: 'C', at: 4, frame: 1},
  102. {type: 'C', at: 5, frame: 0},
  103. ],
  104. };
  105. const flamegraph = new Flamegraph(
  106. EventedProfile.FromProfile(
  107. trace,
  108. createFrameIndex('mobile', [{name: 'f0'}, {name: 'f1'}, {name: 'f2'}])
  109. ),
  110. 10,
  111. {
  112. inverted: false,
  113. leftHeavy: false,
  114. }
  115. );
  116. const order = ['f0', 'f1', 'f2'];
  117. for (let i = 0; i < order.length; i++) {
  118. expect(flamegraph.frames[i].frame.name).toBe(order[i]);
  119. expect(flamegraph.frames[i].depth).toBe(i);
  120. }
  121. });
  122. it('omits 0 width frames', () => {
  123. const trace: Profiling.EventedProfile = {
  124. name: 'profile',
  125. startValue: 0,
  126. endValue: 1000,
  127. unit: 'milliseconds',
  128. threadID: 0,
  129. type: 'evented',
  130. events: [
  131. {type: 'O', at: 0, frame: 0},
  132. {type: 'O', at: 1, frame: 1},
  133. {type: 'C', at: 1, frame: 1},
  134. {type: 'C', at: 3, frame: 0},
  135. ],
  136. };
  137. const flamegraph = new Flamegraph(
  138. EventedProfile.FromProfile(
  139. trace,
  140. createFrameIndex('mobile', [{name: 'f0'}, {name: 'f1'}])
  141. ),
  142. 10,
  143. {
  144. inverted: false,
  145. leftHeavy: false,
  146. }
  147. );
  148. expect(flamegraph.frames.length).toBe(1);
  149. expect(flamegraph.frames.every(f => f.frame.name !== 'f1')).toBe(true);
  150. });
  151. it('tracks max stack depth', () => {
  152. const trace: Profiling.EventedProfile = {
  153. name: 'profile',
  154. startValue: 0,
  155. endValue: 1000,
  156. unit: 'milliseconds',
  157. threadID: 0,
  158. type: 'evented',
  159. events: [
  160. {type: 'O', at: 0, frame: 0},
  161. {type: 'O', at: 1, frame: 1},
  162. {type: 'O', at: 2, frame: 1},
  163. {type: 'C', at: 3, frame: 1},
  164. {type: 'C', at: 4, frame: 1},
  165. {type: 'C', at: 5, frame: 0},
  166. ],
  167. };
  168. const flamegraph = new Flamegraph(
  169. EventedProfile.FromProfile(
  170. trace,
  171. createFrameIndex('mobile', [{name: 'f0'}, {name: 'f1'}])
  172. ),
  173. 10,
  174. {
  175. inverted: false,
  176. leftHeavy: false,
  177. }
  178. );
  179. expect(flamegraph.depth).toBe(2);
  180. });
  181. it('throws on unbalanced stack', () => {
  182. const trace: Profiling.EventedProfile = {
  183. name: 'profile',
  184. startValue: 0,
  185. endValue: 1000,
  186. unit: 'milliseconds',
  187. threadID: 0,
  188. type: 'evented',
  189. events: [
  190. {type: 'O', at: 0, frame: 0},
  191. {type: 'O', at: 1, frame: 1},
  192. {type: 'C', at: 1, frame: 1},
  193. ],
  194. };
  195. expect(
  196. () =>
  197. new Flamegraph(
  198. EventedProfile.FromProfile(
  199. trace,
  200. createFrameIndex('mobile', [{name: 'f0'}, {name: 'f1'}])
  201. ),
  202. 10,
  203. {
  204. inverted: false,
  205. leftHeavy: false,
  206. }
  207. )
  208. ).toThrow('Unbalanced append order stack');
  209. });
  210. it('creates leftHeavy graph', () => {
  211. const trace: Profiling.EventedProfile = {
  212. name: 'profile',
  213. startValue: 0,
  214. endValue: 1000,
  215. unit: 'milliseconds',
  216. threadID: 0,
  217. type: 'evented',
  218. events: [
  219. {type: 'O', at: 0, frame: 0},
  220. {type: 'C', at: 1, frame: 0},
  221. {type: 'O', at: 2, frame: 1},
  222. {type: 'C', at: 4, frame: 1},
  223. ],
  224. };
  225. const flamegraph = new Flamegraph(
  226. EventedProfile.FromProfile(
  227. trace,
  228. createFrameIndex('mobile', [{name: 'f0'}, {name: 'f1'}])
  229. ),
  230. 10,
  231. {
  232. inverted: false,
  233. leftHeavy: true,
  234. }
  235. );
  236. expect(flamegraph.frames[0].frame.name).toBe('f0');
  237. expect(flamegraph.frames[0].frame.totalWeight).toBe(1);
  238. expect(flamegraph.frames[0].start).toBe(2);
  239. expect(flamegraph.frames[0].end).toBe(3);
  240. expect(flamegraph.frames[1].frame.name).toBe('f1');
  241. expect(flamegraph.frames[1].frame.totalWeight).toBe(2);
  242. expect(flamegraph.frames[1].start).toBe(0);
  243. expect(flamegraph.frames[1].end).toBe(2);
  244. });
  245. it('updates startTime and endTime of left heavy children graph', () => {
  246. const trace: Profiling.EventedProfile = {
  247. name: 'profile',
  248. startValue: 0,
  249. endValue: 1000,
  250. unit: 'milliseconds',
  251. threadID: 0,
  252. type: 'evented',
  253. events: [
  254. {type: 'O', at: 0, frame: 0},
  255. {type: 'O', at: 1, frame: 1},
  256. {type: 'C', at: 2, frame: 1},
  257. {type: 'O', at: 2, frame: 2},
  258. {type: 'C', at: 4, frame: 2},
  259. {type: 'C', at: 6, frame: 0},
  260. ],
  261. };
  262. const flamegraph = new Flamegraph(
  263. EventedProfile.FromProfile(
  264. trace,
  265. createFrameIndex('mobile', [{name: 'f0'}, {name: 'f1'}, {name: 'f2'}])
  266. ),
  267. 10,
  268. {
  269. inverted: false,
  270. leftHeavy: true,
  271. }
  272. );
  273. expect(flamegraph.frames[0].frame.name).toBe('f0');
  274. });
  275. it('From', () => {
  276. const trace: Profiling.EventedProfile = {
  277. name: 'profile',
  278. startValue: 0,
  279. endValue: 1000,
  280. unit: 'milliseconds',
  281. threadID: 0,
  282. type: 'evented',
  283. events: [
  284. {type: 'O', at: 0, frame: 0},
  285. {type: 'O', at: 1, frame: 1},
  286. {type: 'C', at: 2, frame: 1},
  287. {type: 'O', at: 2, frame: 2},
  288. {type: 'C', at: 4, frame: 2},
  289. {type: 'C', at: 6, frame: 0},
  290. ],
  291. };
  292. const flamegraph = new Flamegraph(
  293. EventedProfile.FromProfile(
  294. trace,
  295. createFrameIndex('mobile', [{name: 'f0'}, {name: 'f1'}, {name: 'f2'}])
  296. ),
  297. 10,
  298. {
  299. inverted: false,
  300. leftHeavy: true,
  301. }
  302. );
  303. expect(
  304. Flamegraph.From(flamegraph, {
  305. inverted: false,
  306. leftHeavy: false,
  307. }).configSpace.equals(flamegraph.configSpace)
  308. ).toBe(true);
  309. });
  310. it('Empty', () => {
  311. expect(Flamegraph.Empty().configSpace.equals(new Rect(0, 0, 1_000, 0))).toBe(true);
  312. });
  313. it('setConfigSpace', () => {
  314. expect(
  315. Flamegraph.Empty()
  316. .setConfigSpace(new Rect(0, 0, 10, 5))
  317. .configSpace.equals(new Rect(0, 0, 10, 5))
  318. ).toBe(true);
  319. });
  320. });