utils.tsx 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. import {Flamegraph} from 'sentry/utils/profiling/flamegraph';
  2. import {EventedProfile} from 'sentry/utils/profiling/profile/eventedProfile';
  3. import {createFrameIndex} from 'sentry/utils/profiling/profile/utils';
  4. export const makeContextMock = (
  5. partialMock: Partial<WebGLRenderingContext> = {}
  6. ): WebGLRenderingContext => {
  7. const context: Partial<WebGLRenderingContext> = {
  8. attachShader: jest.fn(),
  9. bufferData: jest.fn(),
  10. blendFuncSeparate: jest.fn(),
  11. bindBuffer: jest.fn(),
  12. clearColor: jest.fn(),
  13. clear: jest.fn(),
  14. createShader: jest.fn().mockReturnValue({}),
  15. createProgram: jest.fn().mockReturnValue({}),
  16. createBuffer: jest.fn().mockReturnValue([]),
  17. compileShader: jest.fn(),
  18. drawArrays: jest.fn(),
  19. enable: jest.fn(),
  20. enableVertexAttribArray: jest.fn(),
  21. getShaderParameter: jest.fn().mockReturnValue(1),
  22. getProgramParameter: jest.fn().mockReturnValue({}),
  23. getUniformLocation: jest.fn().mockReturnValue({}),
  24. getAttribLocation: jest.fn().mockReturnValue({}),
  25. linkProgram: jest.fn(),
  26. shaderSource: jest.fn(),
  27. uniformMatrix3fv: jest.fn(),
  28. uniform1i: jest.fn(),
  29. uniform2f: jest.fn(),
  30. useProgram: jest.fn(),
  31. vertexAttribPointer: jest.fn(),
  32. viewport: jest.fn(),
  33. canvas: {
  34. width: 1000,
  35. height: 1000,
  36. } as HTMLCanvasElement,
  37. ...partialMock,
  38. };
  39. return context as WebGLRenderingContext;
  40. };
  41. export const makeCanvasMock = (
  42. partialMock: Partial<HTMLCanvasElement> = {}
  43. ): HTMLCanvasElement => {
  44. const canvas: Partial<HTMLCanvasElement> = {
  45. getContext: jest.fn().mockReturnValue(makeContextMock()),
  46. height: 1000,
  47. width: 1000,
  48. ...partialMock,
  49. };
  50. return canvas as HTMLCanvasElement;
  51. };
  52. const base: Profiling.EventedProfile = {
  53. name: 'profile',
  54. startValue: 0,
  55. endValue: 10,
  56. unit: 'milliseconds',
  57. type: 'evented',
  58. threadID: 0,
  59. events: [
  60. {type: 'O', at: 0, frame: 0},
  61. {type: 'C', at: 10, frame: 0},
  62. ],
  63. };
  64. export const makeFlamegraph = (
  65. trace?: Partial<Profiling.EventedProfile>,
  66. frames?: Profiling.Schema['shared']['frames']
  67. ): Flamegraph => {
  68. return new Flamegraph(
  69. EventedProfile.FromProfile(
  70. trace ? {...base, ...trace} : base,
  71. createFrameIndex('mobile', frames ?? [{name: 'f0'}]),
  72. {type: 'flamechart'}
  73. ),
  74. {inverted: false, sort: 'call order'}
  75. );
  76. };