sampledProfile.spec.tsx 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. import {SampledProfile} from 'sentry/utils/profiling/profile/sampledProfile';
  2. import {createFrameIndex} from 'sentry/utils/profiling/profile/utils';
  3. import {firstCallee, makeTestingBoilerplate} from './profile.spec';
  4. describe('SampledProfile', () => {
  5. it('imports the base properties', () => {
  6. const trace: Profiling.SampledProfile = {
  7. name: 'profile',
  8. startValue: 0,
  9. endValue: 1000,
  10. unit: 'milliseconds',
  11. threadID: 0,
  12. type: 'sampled',
  13. weights: [],
  14. samples: [],
  15. };
  16. const profile = SampledProfile.FromProfile(trace, createFrameIndex([]));
  17. expect(profile.duration).toBe(1000);
  18. expect(profile.name).toBe(trace.name);
  19. expect(profile.threadId).toBe(trace.threadID);
  20. expect(profile.startedAt).toBe(0);
  21. expect(profile.endedAt).toBe(1000);
  22. });
  23. it('rebuilds the stack', () => {
  24. const trace: Profiling.SampledProfile = {
  25. name: 'profile',
  26. startValue: 0,
  27. endValue: 1000,
  28. unit: 'milliseconds',
  29. threadID: 0,
  30. type: 'sampled',
  31. weights: [1, 1],
  32. samples: [
  33. [0, 1],
  34. [0, 1],
  35. ],
  36. };
  37. const {open, close, openSpy, closeSpy, timings} = makeTestingBoilerplate();
  38. const profile = SampledProfile.FromProfile(
  39. trace,
  40. createFrameIndex([{name: 'f0'}, {name: 'f1'}])
  41. );
  42. profile.forEach(open, close);
  43. expect(timings).toEqual([
  44. ['f0', 'open'],
  45. ['f1', 'open'],
  46. ['f1', 'close'],
  47. ['f0', 'close'],
  48. ]);
  49. expect(openSpy).toHaveBeenCalledTimes(2);
  50. expect(closeSpy).toHaveBeenCalledTimes(2);
  51. const root = firstCallee(profile.appendOrderTree);
  52. expect(root.totalWeight).toEqual(2);
  53. expect(firstCallee(root).totalWeight).toEqual(2);
  54. expect(root.selfWeight).toEqual(0);
  55. expect(firstCallee(root).selfWeight).toEqual(2);
  56. });
  57. it('marks direct recursion', () => {
  58. const trace: Profiling.SampledProfile = {
  59. name: 'profile',
  60. startValue: 0,
  61. endValue: 1000,
  62. unit: 'milliseconds',
  63. threadID: 0,
  64. type: 'sampled',
  65. weights: [1],
  66. samples: [[0, 0]],
  67. };
  68. const profile = SampledProfile.FromProfile(
  69. trace,
  70. createFrameIndex([{name: 'f0'}, {name: 'f1'}])
  71. );
  72. expect(firstCallee(firstCallee(profile.appendOrderTree)).isRecursive()).toBe(true);
  73. });
  74. it('marks indirect recursion', () => {
  75. const trace: Profiling.SampledProfile = {
  76. name: 'profile',
  77. startValue: 0,
  78. endValue: 1000,
  79. unit: 'milliseconds',
  80. threadID: 0,
  81. type: 'sampled',
  82. weights: [1],
  83. samples: [[0, 1, 0]],
  84. };
  85. const profile = SampledProfile.FromProfile(
  86. trace,
  87. createFrameIndex([{name: 'f0'}, {name: 'f1'}])
  88. );
  89. expect(
  90. firstCallee(firstCallee(firstCallee(profile.appendOrderTree))).isRecursive()
  91. ).toBe(true);
  92. });
  93. it('tracks minFrameDuration', () => {
  94. const trace: Profiling.SampledProfile = {
  95. name: 'profile',
  96. startValue: 0,
  97. endValue: 1000,
  98. unit: 'milliseconds',
  99. threadID: 0,
  100. type: 'sampled',
  101. weights: [0.5, 2],
  102. samples: [
  103. [0, 1],
  104. [0, 2],
  105. ],
  106. };
  107. const profile = SampledProfile.FromProfile(
  108. trace,
  109. createFrameIndex([{name: 'f0'}, {name: 'f1'}, {name: 'f2'}])
  110. );
  111. expect(profile.minFrameDuration).toBe(0.5);
  112. });
  113. });