profile.benchmark.ts 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. // Benchmarks allow us to make changes and evaluate performance before the code gets shipped to production.
  2. // They can be used to make performance improvements or to test impact of newly added functionality.
  3. // Run with: yarn run ts-node --project ./config/tsconfig.benchmark.json -r tsconfig-paths/register static/app/utils/profiling/profile/profile.benchmark.ts
  4. import benchmarkjs from 'benchmark';
  5. import {initializeLocale} from 'sentry/bootstrap/initializeLocale';
  6. import eventedTrace from './formats/android/trace.json';
  7. import sampledTrace from './formats/ios/trace.json';
  8. import jsSelfProfileTrace from './formats/jsSelfProfile/trace.json';
  9. import nodeTrace from './formats/node/trace.json';
  10. import typescriptTrace from './formats/typescript/trace.json';
  11. import {importProfile} from './importProfile';
  12. // This logs an error which is annoying to see in the outputs
  13. initializeLocale({} as any);
  14. // Note: You MUST import @sentry/tracing package before @sentry/profiling-node
  15. // eslint-disable-next-line simple-import-sort/imports
  16. import * as Sentry from '@sentry/node';
  17. import '@sentry/tracing';
  18. import {ProfilingIntegration} from '@sentry/profiling-node';
  19. if (process.env.PROFILE) {
  20. Sentry.init({
  21. dsn: 'https://7fa19397baaf433f919fbe02228d5470@o1137848.ingest.sentry.io/6625302',
  22. integrations: [
  23. // Add our Profilling integration
  24. new ProfilingIntegration(),
  25. ],
  26. debug: true,
  27. tracesSampleRate: 1.0,
  28. // Set sampling rate for profiling
  29. profilesSampleRate: 1.0,
  30. });
  31. }
  32. // We dont compare benchmark results, as we are testing a single version of the code, so we run this as a baseline,
  33. // store the results somewhere locally and then compare the results with the new version of our code.
  34. function benchmark(name: string, callback: () => void) {
  35. const suite = new benchmarkjs.Suite();
  36. suite
  37. .add(name, callback, {minSamples: 50})
  38. .on('cycle', event => {
  39. // well, we need to see the results somewhere
  40. // eslint-disable-next-line
  41. console.log(event.target.toString(), (event.target.stats.mean * 1e3).toFixed(2));
  42. })
  43. .on('error', event => {
  44. // If something goes wrong, fail early
  45. throw event;
  46. })
  47. .on('fini');
  48. suite.run({async: true, minSamples: 100});
  49. }
  50. benchmark('typescript', () => importProfile(typescriptTrace as any, '', 'flamechart'));
  51. benchmark('js self profile', () =>
  52. importProfile(jsSelfProfileTrace as any, '', 'flamechart')
  53. );
  54. benchmark('evented profile', () => importProfile(eventedTrace as any, '', 'flamechart'));
  55. benchmark('sampled profile', () => importProfile(sampledTrace as any, '', 'flamechart'));
  56. benchmark('sampled node profile', () =>
  57. importProfile(nodeTrace as any, '', 'flamechart')
  58. );
  59. // importProfile(nodeTrace, '', 'flamechart');