jsSelfProfile.spec.tsx 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. import {JSSelfProfile} from 'sentry/utils/profiling/profile/jsSelfProfile';
  2. import {createFrameIndex} from 'sentry/utils/profiling/profile/utils';
  3. import {firstCallee, makeTestingBoilerplate, nthCallee} from './profile.spec';
  4. describe('jsSelfProfile', () => {
  5. it('imports the base properties', () => {
  6. const trace: JSSelfProfiling.Trace = {
  7. resources: ['app.js', 'vendor.js'],
  8. frames: [{name: 'ReactDOM.render', line: 1, column: 1, resourceId: 0}],
  9. samples: [
  10. {
  11. timestamp: 0,
  12. },
  13. {
  14. timestamp: 1000,
  15. stackId: 0,
  16. },
  17. ],
  18. stacks: [
  19. {
  20. frameId: 0,
  21. },
  22. ],
  23. };
  24. const profile = JSSelfProfile.FromProfile(
  25. trace,
  26. createFrameIndex(trace.frames, trace)
  27. );
  28. expect(profile.duration).toBe(1000);
  29. expect(profile.startedAt).toBe(0);
  30. expect(profile.endedAt).toBe(1000);
  31. expect(profile.appendOrderTree.children[0].frame.name).toBe('ReactDOM.render');
  32. expect(profile.appendOrderTree.children[0].frame.resource).toBe('app.js');
  33. });
  34. it('handles the first stack sample differently', () => {
  35. const trace: JSSelfProfiling.Trace = {
  36. resources: ['app.js'],
  37. frames: [
  38. {name: 'main', line: 1, column: 1, resourceId: 0},
  39. {name: 'new Profiler', line: 1, column: 1, resourceId: 0},
  40. {name: 'afterProfiler.init', line: 1, column: 1, resourceId: 0},
  41. ],
  42. samples: [
  43. {
  44. stackId: 1,
  45. timestamp: 500,
  46. },
  47. {
  48. stackId: 2,
  49. timestamp: 1500,
  50. },
  51. ],
  52. stacks: [
  53. {frameId: 0, parentId: undefined},
  54. {frameId: 1, parentId: 0},
  55. {frameId: 2, parentId: 0},
  56. ],
  57. };
  58. const {open, close, openSpy, closeSpy, timings} = makeTestingBoilerplate();
  59. const profile = JSSelfProfile.FromProfile(
  60. trace,
  61. createFrameIndex(trace.frames, trace)
  62. );
  63. profile.forEach(open, close);
  64. expect(timings).toEqual([
  65. ['main', 'open'],
  66. ['new Profiler', 'open'],
  67. ['new Profiler', 'close'],
  68. ['afterProfiler.init', 'open'],
  69. ['afterProfiler.init', 'close'],
  70. ['main', 'close'],
  71. ]);
  72. expect(openSpy).toHaveBeenCalledTimes(3);
  73. expect(closeSpy).toHaveBeenCalledTimes(3);
  74. const root = firstCallee(profile.appendOrderTree);
  75. expect(root.totalWeight).toEqual(1000);
  76. expect(root.selfWeight).toEqual(0);
  77. expect(nthCallee(root, 0).selfWeight).toEqual(0);
  78. expect(nthCallee(root, 0).totalWeight).toEqual(0);
  79. expect(nthCallee(root, 1).selfWeight).toEqual(1000);
  80. expect(nthCallee(root, 1).totalWeight).toEqual(1000);
  81. });
  82. it('rebuilds the stack', () => {
  83. const trace: JSSelfProfiling.Trace = {
  84. resources: ['app.js'],
  85. frames: [
  86. {name: 'f0', line: 1, column: 1, resourceId: 0},
  87. {name: 'f1', line: 1, column: 1, resourceId: 0},
  88. ],
  89. samples: [
  90. {
  91. stackId: 0,
  92. timestamp: 0,
  93. },
  94. {
  95. timestamp: 1000,
  96. stackId: 0,
  97. },
  98. ],
  99. stacks: [{frameId: 1, parentId: 1}, {frameId: 0}],
  100. };
  101. const {open, close, openSpy, closeSpy, timings} = makeTestingBoilerplate();
  102. const profile = JSSelfProfile.FromProfile(
  103. trace,
  104. createFrameIndex(trace.frames, trace)
  105. );
  106. profile.forEach(open, close);
  107. expect(timings).toEqual([
  108. ['f0', 'open'],
  109. ['f1', 'open'],
  110. ['f1', 'close'],
  111. ['f0', 'close'],
  112. ]);
  113. expect(openSpy).toHaveBeenCalledTimes(2);
  114. expect(closeSpy).toHaveBeenCalledTimes(2);
  115. const root = firstCallee(profile.appendOrderTree);
  116. expect(root.totalWeight).toEqual(1000);
  117. expect(firstCallee(root).totalWeight).toEqual(1000);
  118. expect(root.selfWeight).toEqual(0);
  119. expect(firstCallee(root).selfWeight).toEqual(1000);
  120. });
  121. it('marks direct recursion', () => {
  122. const trace: JSSelfProfiling.Trace = {
  123. resources: ['app.js'],
  124. frames: [{name: 'f0', line: 1, column: 1, resourceId: 0}],
  125. samples: [
  126. {
  127. stackId: 0,
  128. timestamp: 0,
  129. },
  130. {
  131. stackId: 0,
  132. timestamp: 0,
  133. },
  134. ],
  135. stacks: [{frameId: 0, parentId: 1}, {frameId: 0}],
  136. };
  137. const profile = JSSelfProfile.FromProfile(
  138. trace,
  139. createFrameIndex(trace.frames, trace)
  140. );
  141. expect(firstCallee(firstCallee(profile.appendOrderTree)).isRecursive()).toBe(true);
  142. });
  143. it('marks indirect recursion', () => {
  144. const trace: JSSelfProfiling.Trace = {
  145. resources: ['app.js'],
  146. frames: [
  147. {name: 'f0', line: 1, column: 1, resourceId: 0},
  148. {name: 'f1', line: 1, column: 1, resourceId: 0},
  149. ],
  150. samples: [
  151. {
  152. stackId: 0,
  153. timestamp: 0,
  154. },
  155. {
  156. stackId: 2,
  157. timestamp: 100,
  158. },
  159. ],
  160. stacks: [
  161. {frameId: 0, parentId: undefined},
  162. {frameId: 1, parentId: 0},
  163. {frameId: 0, parentId: 1},
  164. ],
  165. };
  166. const profile = JSSelfProfile.FromProfile(
  167. trace,
  168. createFrameIndex(trace.frames, trace)
  169. );
  170. expect(
  171. firstCallee(firstCallee(firstCallee(profile.appendOrderTree))).isRecursive()
  172. ).toBe(true);
  173. });
  174. it('tracks minFrameDuration', () => {
  175. const trace: JSSelfProfiling.Trace = {
  176. resources: ['app.js'],
  177. frames: [
  178. {name: 'f0', line: 1, column: 1, resourceId: 0},
  179. {name: 'f1', line: 1, column: 1, resourceId: 0},
  180. {name: 'f2', line: 1, column: 1, resourceId: 0},
  181. ],
  182. samples: [
  183. {
  184. stackId: 0,
  185. timestamp: 0,
  186. },
  187. {
  188. stackId: 2,
  189. timestamp: 10,
  190. },
  191. {
  192. stackId: 3,
  193. timestamp: 100,
  194. },
  195. ],
  196. stacks: [
  197. {frameId: 0, parentId: undefined},
  198. {frameId: 1, parentId: 0},
  199. {frameId: 0, parentId: 1},
  200. {frameId: 2},
  201. ],
  202. };
  203. const profile = JSSelfProfile.FromProfile(
  204. trace,
  205. createFrameIndex(trace.frames, trace)
  206. );
  207. expect(profile.minFrameDuration).toBe(10);
  208. });
  209. it('appends gc to previous stack', () => {
  210. const trace: JSSelfProfiling.Trace = {
  211. resources: ['app.js'],
  212. frames: [
  213. {name: 'f1', line: 1, column: 1, resourceId: 0},
  214. {name: 'f0', line: 1, column: 1, resourceId: 0},
  215. ],
  216. samples: [
  217. {
  218. stackId: 0,
  219. timestamp: 0,
  220. },
  221. {
  222. timestamp: 10,
  223. marker: 'gc',
  224. },
  225. ],
  226. stacks: [
  227. {frameId: 0, parentId: 1},
  228. {frameId: 1, parentId: undefined},
  229. ],
  230. };
  231. const profile = JSSelfProfile.FromProfile(
  232. trace,
  233. createFrameIndex(trace.frames, trace)
  234. );
  235. const {open, close, openSpy, closeSpy, timings} = makeTestingBoilerplate();
  236. profile.forEach(open, close);
  237. expect(openSpy).toHaveBeenCalledTimes(3);
  238. expect(closeSpy).toHaveBeenCalledTimes(3);
  239. expect(timings).toEqual([
  240. ['f0', 'open'],
  241. ['f1', 'open'],
  242. ['Garbage Collection', 'open'],
  243. ['Garbage Collection', 'close'],
  244. ['f1', 'close'],
  245. ['f0', 'close'],
  246. ]);
  247. });
  248. });