profile.tsx 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. export function isSchema(input: any): input is Profiling.Schema {
  2. return (
  3. typeof input === 'object' &&
  4. // 'metadata' in input &&
  5. 'profiles' in input &&
  6. Array.isArray(input.profiles) &&
  7. 'shared' in input
  8. );
  9. }
  10. export function isEventedProfile(profile: any): profile is Profiling.EventedProfile {
  11. return 'type' in profile && profile.type === 'evented';
  12. }
  13. export function isSampledProfile(profile: any): profile is Profiling.SampledProfile {
  14. return 'type' in profile && profile.type === 'sampled';
  15. }
  16. export function isJSProfile(profile: any): profile is JSSelfProfiling.Trace {
  17. return !('type' in profile) && Array.isArray(profile.resources);
  18. }
  19. export function isSentrySampledProfile(
  20. profile: any
  21. ): profile is Profiling.SentrySampledProfile {
  22. return (
  23. 'profile' in profile &&
  24. 'samples' in profile.profile &&
  25. 'stacks' in profile.profile &&
  26. 'frames' in profile.profile
  27. );
  28. }
  29. export function isChromeTraceObjectFormat(
  30. profile: any
  31. ): profile is ChromeTrace.ObjectFormat {
  32. return typeof profile === 'object' && 'traceEvents' in profile;
  33. }
  34. // We check for the presence of at least one ProfileChunk event in the trace
  35. export function isChromeTraceArrayFormat(
  36. profile: any
  37. ): profile is ChromeTrace.ProfileType {
  38. return (
  39. Array.isArray(profile) && profile.some(p => p.ph === 'P' && p.name === 'ProfileChunk')
  40. );
  41. }
  42. // Typescript uses only a subset of the event types (only B and E cat),
  43. // so we need to inspect the contents of the trace to determine the type of the profile.
  44. // The TS trace can still contain other event types like metadata events, meaning we cannot
  45. // use array.every() and need to check all the events to make sure no P events are present
  46. export function isTypescriptChromeTraceArrayFormat(
  47. profile: any
  48. ): profile is ChromeTrace.ArrayFormat {
  49. return (
  50. Array.isArray(profile) &&
  51. !profile.some(p => p.ph === 'P' && p.name === 'ProfileChunk')
  52. );
  53. }
  54. export function isChromeTraceFormat(profile: any): profile is ChromeTrace.ArrayFormat {
  55. return (
  56. isTypescriptChromeTraceArrayFormat(profile) ||
  57. isChromeTraceObjectFormat(profile) ||
  58. isChromeTraceArrayFormat(profile)
  59. );
  60. }