profile.tsx 2.3 KB

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