profile.tsx 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. export function isSchema(input: any): input is Profiling.Schema {
  2. return (
  3. typeof input === 'object' &&
  4. 'transactionName' 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 isChromeTraceObjectFormat(input: any): input is ChromeTrace.ObjectFormat {
  20. return typeof input === 'object' && 'traceEvents' in input;
  21. }
  22. // We check for the presence of at least one ProfileChunk event in the trace
  23. export function isChromeTraceArrayFormat(input: any): input is ChromeTrace.ProfileType {
  24. return (
  25. Array.isArray(input) && input.some(p => p.ph === 'P' && p.name === 'ProfileChunk')
  26. );
  27. }
  28. // Typescript uses only a subset of the event types (only B and E cat),
  29. // so we need to inspect the contents of the trace to determine the type of the profile.
  30. // The TS trace can still contain other event types like metadata events, meaning we cannot
  31. // use array.every() and need to check all the events to make sure no P events are present
  32. export function isTypescriptChromeTraceArrayFormat(
  33. input: any
  34. ): input is ChromeTrace.ArrayFormat {
  35. return (
  36. Array.isArray(input) && !input.some(p => p.ph === 'P' && p.name === 'ProfileChunk')
  37. );
  38. }
  39. export function isChromeTraceFormat(input: any): input is ChromeTrace.ArrayFormat {
  40. return (
  41. isTypescriptChromeTraceArrayFormat(input) ||
  42. isChromeTraceObjectFormat(input) ||
  43. isChromeTraceArrayFormat(input)
  44. );
  45. }