utils.tsx 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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. // @ts-expect-error
  34. canvas: {
  35. width: 1000,
  36. height: 1000,
  37. },
  38. ...partialMock,
  39. };
  40. return context as WebGLRenderingContext;
  41. };
  42. export const makeCanvasMock = (
  43. partialMock: Partial<HTMLCanvasElement> = {}
  44. ): HTMLCanvasElement => {
  45. const canvas: Partial<HTMLCanvasElement> = {
  46. getContext: jest.fn().mockReturnValue(makeContextMock()),
  47. height: 1000,
  48. width: 1000,
  49. ...partialMock,
  50. };
  51. return canvas as HTMLCanvasElement;
  52. };
  53. const base: Profiling.EventedProfile = {
  54. name: 'profile',
  55. startValue: 0,
  56. endValue: 10,
  57. unit: 'milliseconds',
  58. type: 'evented',
  59. threadID: 0,
  60. events: [
  61. {type: 'O', at: 0, frame: 0},
  62. {type: 'C', at: 10, frame: 0},
  63. ],
  64. };
  65. export const makeFlamegraph = (
  66. trace?: Partial<Profiling.EventedProfile>,
  67. frames?: Profiling.Schema['shared']['frames']
  68. ): Flamegraph => {
  69. return new Flamegraph(
  70. EventedProfile.FromProfile(
  71. trace ? {...base, ...trace} : base,
  72. createFrameIndex('mobile', frames ?? [{name: 'f0'}]),
  73. {type: 'flamechart'}
  74. ),
  75. 0,
  76. {inverted: false, sort: 'call order'}
  77. );
  78. };