differentialFlamegraph.spec.tsx 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. import {DifferentialFlamegraph} from 'sentry/utils/profiling/differentialFlamegraph';
  2. import {Flamegraph} from 'sentry/utils/profiling/flamegraph';
  3. import {FlamegraphTheme} from 'sentry/utils/profiling/flamegraph/flamegraphTheme';
  4. import {EventedProfile} from 'sentry/utils/profiling/profile/eventedProfile';
  5. import {createFrameIndex} from 'sentry/utils/profiling/profile/utils';
  6. const makeEvent = (times: number, frame: number): Profiling.Event[] => {
  7. return new Array(times * 2).fill(0).map((_, i) => {
  8. return {type: i % 2 === 0 ? 'O' : 'C', at: i, frame};
  9. });
  10. };
  11. const baseProfile: Profiling.EventedProfile = {
  12. name: 'profile',
  13. startValue: 0,
  14. endValue: 1000,
  15. unit: 'milliseconds',
  16. threadID: 0,
  17. type: 'evented',
  18. events: [],
  19. };
  20. const makeFlamegraph = (profile: Profiling.EventedProfile) => {
  21. return new Flamegraph(
  22. EventedProfile.FromProfile(
  23. profile,
  24. createFrameIndex('mobile', [
  25. {name: 'f0'},
  26. {name: 'f1'},
  27. {name: 'f2'},
  28. {name: 'f3'},
  29. ]),
  30. {type: 'flamechart'}
  31. ),
  32. 0,
  33. {
  34. inverted: false,
  35. leftHeavy: false,
  36. }
  37. );
  38. };
  39. const THEME = {
  40. COLORS: {
  41. DIFFERENTIAL_DECREASE: [0, 0, 1],
  42. DIFFERENTIAL_INCREASE: [1, 0, 0],
  43. },
  44. } as FlamegraphTheme;
  45. describe('differentialFlamegraph', () => {
  46. it('INCREASE: color encodes new frames red', () => {
  47. const from = makeFlamegraph({
  48. ...baseProfile,
  49. events: [],
  50. });
  51. const to = makeFlamegraph({
  52. ...baseProfile,
  53. events: [...makeEvent(1, 0)],
  54. });
  55. const flamegraph = DifferentialFlamegraph.Diff(from, to, THEME);
  56. expect(flamegraph.colors?.get('f0')).toEqual([1, 0, 0, 1]);
  57. });
  58. it('INCREASE: color encodes relative increase in red', () => {
  59. const from = makeFlamegraph({
  60. ...baseProfile,
  61. events: [...makeEvent(2, 0)],
  62. });
  63. const to = makeFlamegraph({
  64. ...baseProfile,
  65. events: [...makeEvent(3, 0)],
  66. });
  67. const flamegraph = DifferentialFlamegraph.Diff(from, to, THEME);
  68. expect(flamegraph.colors?.get('f0')).toEqual([1, 0, 0, 0.5]);
  69. });
  70. it('DECREASE: color encodes relative decrease in blue', () => {
  71. const from = makeFlamegraph({
  72. ...baseProfile,
  73. events: [...makeEvent(4, 0)],
  74. });
  75. const to = makeFlamegraph({
  76. ...baseProfile,
  77. events: [...makeEvent(2, 0)],
  78. });
  79. const flamegraph = DifferentialFlamegraph.Diff(from, to, THEME);
  80. expect(flamegraph.colors?.get('f0')).toEqual([0, 0, 1, 0.5]);
  81. });
  82. it('No change: no color is set', () => {
  83. const from = makeFlamegraph({
  84. ...baseProfile,
  85. events: [...makeEvent(2, 0)],
  86. });
  87. const to = makeFlamegraph({
  88. ...baseProfile,
  89. events: [...makeEvent(2, 0)],
  90. });
  91. const flamegraph = DifferentialFlamegraph.Diff(from, to, THEME);
  92. expect(flamegraph.colors?.get('f0')).toBe(undefined);
  93. });
  94. });