differentialFlamegraph.spec.tsx 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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', [{name: 'f0'}, {name: 'f1'}, {name: 'f2'}, {name: 'f3'}])
  25. ),
  26. 0,
  27. {
  28. inverted: false,
  29. leftHeavy: false,
  30. }
  31. );
  32. };
  33. const THEME = {
  34. COLORS: {
  35. DIFFERENTIAL_DECREASE: [0, 0, 1],
  36. DIFFERENTIAL_INCREASE: [1, 0, 0],
  37. },
  38. } as FlamegraphTheme;
  39. describe('differentialFlamegraph', () => {
  40. it('INCREASE: color encodes new frames red', () => {
  41. const from = makeFlamegraph({
  42. ...baseProfile,
  43. events: [],
  44. });
  45. const to = makeFlamegraph({
  46. ...baseProfile,
  47. events: [...makeEvent(1, 0)],
  48. });
  49. const flamegraph = DifferentialFlamegraph.Diff(from, to, THEME);
  50. expect(flamegraph.colors?.get('f0')).toEqual([1, 0, 0, 1]);
  51. });
  52. it('INCREASE: color encodes relative increase in red', () => {
  53. const from = makeFlamegraph({
  54. ...baseProfile,
  55. events: [...makeEvent(2, 0)],
  56. });
  57. const to = makeFlamegraph({
  58. ...baseProfile,
  59. events: [...makeEvent(3, 0)],
  60. });
  61. const flamegraph = DifferentialFlamegraph.Diff(from, to, THEME);
  62. expect(flamegraph.colors?.get('f0')).toEqual([1, 0, 0, 0.5]);
  63. });
  64. it('DECREASE: color encodes relative decrease in blue', () => {
  65. const from = makeFlamegraph({
  66. ...baseProfile,
  67. events: [...makeEvent(4, 0)],
  68. });
  69. const to = makeFlamegraph({
  70. ...baseProfile,
  71. events: [...makeEvent(2, 0)],
  72. });
  73. const flamegraph = DifferentialFlamegraph.Diff(from, to, THEME);
  74. expect(flamegraph.colors?.get('f0')).toEqual([0, 0, 1, 0.5]);
  75. });
  76. it('No change: no color is set', () => {
  77. const from = makeFlamegraph({
  78. ...baseProfile,
  79. events: [...makeEvent(2, 0)],
  80. });
  81. const to = makeFlamegraph({
  82. ...baseProfile,
  83. events: [...makeEvent(2, 0)],
  84. });
  85. const flamegraph = DifferentialFlamegraph.Diff(from, to, THEME);
  86. expect(flamegraph.colors?.get('f0')).toBe(undefined);
  87. });
  88. });