123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411 |
- import {
- ChromeTraceProfile,
- collapseSamples,
- parseTypescriptChromeTraceArrayFormat,
- splitEventsByProcessAndTraceId,
- } from 'sentry/utils/profiling/profile/chromeTraceProfile';
- describe('splitEventsByProcessAndTraceId', () => {
- it('splits by thread id', () => {
- const trace: ChromeTrace.ArrayFormat = [
- {
- ph: 'B',
- tid: 0,
- pid: 0,
- cat: '',
- name: '',
- ts: 0,
- args: [],
- },
- {
- ph: 'B',
- tid: 1,
- pid: 0,
- cat: '',
- name: '',
- ts: 0,
- args: [],
- },
- ];
- expect(splitEventsByProcessAndTraceId(trace).get(0)?.get(0)).toEqual([trace[0]]);
- expect(splitEventsByProcessAndTraceId(trace).get(0)?.get(1)).toEqual([trace[1]]);
- });
- });
- describe('parseTypescriptChromeTraceArrayFormat', () => {
- it('returns typescript profile', () => {
- expect(
- parseTypescriptChromeTraceArrayFormat(
- [
- {
- ph: 'M',
- ts: 0,
- cat: '',
- pid: 0,
- tid: 0,
- name: 'process_name',
- args: {name: 'Process Name'},
- },
- {
- ph: 'B',
- ts: 0,
- cat: 'program',
- pid: 0,
- tid: 0,
- name: 'createProgram',
- args: {configFilePath: '/Users/jonasbadalic/Work/sentry/tsconfig.json'},
- },
- ],
- ''
- ).profiles[0]
- ).toBeInstanceOf(ChromeTraceProfile);
- });
- it('marks process name', () => {
- expect(
- parseTypescriptChromeTraceArrayFormat(
- [
- {
- ph: 'M',
- ts: 0,
- cat: '',
- pid: 0,
- tid: 0,
- name: 'process_name',
- args: {name: 'Process Name'},
- },
- {
- ph: 'B',
- ts: 0,
- cat: 'program',
- pid: 0,
- tid: 0,
- name: 'createProgram',
- args: {configFilePath: '/Users/jonasbadalic/Work/sentry/tsconfig.json'},
- },
- ],
- ''
- ).profiles[0].name
- ).toBe('Process Name (0): tid (0)');
- });
- it('marks thread name', () => {
- expect(
- parseTypescriptChromeTraceArrayFormat(
- [
- {
- ph: 'M',
- ts: 0,
- cat: '',
- pid: 0,
- tid: 0,
- name: 'thread_name',
- args: {name: 'Thread Name'},
- },
- {
- ph: 'B',
- ts: 0,
- cat: 'program',
- pid: 0,
- tid: 0,
- name: 'createProgram',
- args: {configFilePath: '/Users/jonasbadalic/Work/sentry/tsconfig.json'},
- },
- ],
- ''
- ).profiles[0].name
- ).toBe('pid (0): Thread Name (0)');
- });
- it('imports a simple trace', () => {
- const trace = parseTypescriptChromeTraceArrayFormat(
- [
- {
- ph: 'B',
- ts: 1000,
- cat: 'program',
- pid: 0,
- tid: 0,
- name: 'createProgram',
- args: {configFilePath: '/Users/jonasbadalic/Work/sentry/tsconfig.json'},
- },
- {
- ph: 'E',
- ts: 2000,
- cat: 'program',
- pid: 0,
- tid: 0,
- name: 'createProgram',
- args: {configFilePath: '/Users/jonasbadalic/Work/sentry/tsconfig.json'},
- },
- ],
- ''
- );
- expect(trace.profiles[0].startedAt).toBe(1000);
- expect(trace.profiles[0].endedAt).toBe(2000);
- expect(trace.profiles[0].duration).toBe(1000);
- expect(trace.profiles[0].appendOrderTree.children[0].totalWeight).toBe(1000);
- });
- it('closes unclosed events', () => {
- const trace = parseTypescriptChromeTraceArrayFormat(
- [
- {
- ph: 'B',
- ts: 0,
- cat: 'program',
- pid: 0,
- tid: 0,
- name: 'createProgram',
- args: {frame: '0'},
- },
- {
- ph: 'B',
- ts: 1000,
- cat: 'program',
- pid: 0,
- tid: 0,
- name: 'createProgram',
- args: {frame: '1'},
- },
- {
- ph: 'E',
- ts: 2000,
- cat: 'program',
- pid: 0,
- tid: 0,
- name: 'createProgram',
- args: {frame: '1'},
- },
- ],
- ''
- );
- expect(trace.profiles[0].duration).toBe(2000);
- expect(trace.profiles[0].appendOrderTree.children[0].selfWeight).toBe(1000);
- expect(trace.profiles[0].appendOrderTree.children[0].totalWeight).toBe(2000);
- expect(trace.profiles[0].appendOrderTree.children[0].children[0].selfWeight).toBe(
- 1000
- );
- });
- it('handles out of order E events', () => {
- const trace = parseTypescriptChromeTraceArrayFormat(
- [
- {
- ph: 'B',
- ts: 0,
- cat: '',
- pid: 0,
- tid: 0,
- name: '',
- args: {frame: '0'},
- },
- {
- ph: 'B',
- ts: 1,
- cat: '',
- pid: 0,
- tid: 0,
- name: '',
- args: {frame: '1'},
- },
- {
- ph: 'E',
- ts: 2,
- cat: '',
- pid: 0,
- tid: 0,
- name: '',
- args: {frame: '0'},
- },
- {
- ph: 'E',
- ts: 2,
- cat: '',
- pid: 0,
- tid: 0,
- name: '',
- args: {frame: '1'},
- },
- ],
- ''
- );
- expect(trace.profiles[0].duration).toBe(2);
- expect(trace.profiles[0].appendOrderTree.children[0].selfWeight).toBe(1);
- expect(trace.profiles[0].appendOrderTree.children[0].totalWeight).toBe(2);
- expect(trace.profiles[0].appendOrderTree.children[0].frame.name).toBe(
- 'Unknown {"frame":"0"}'
- );
- expect(trace.profiles[0].appendOrderTree.children[0].children[0].frame.name).toBe(
- 'Unknown {"frame":"1"}'
- );
- expect(trace.profiles[0].appendOrderTree.children[0].children[0].selfWeight).toBe(1);
- expect(trace.profiles[0].appendOrderTree.children[0].children[0].totalWeight).toBe(1);
- });
- it('handles out of order B events', () => {
- const trace = parseTypescriptChromeTraceArrayFormat(
- [
- {
- ph: 'B',
- ts: 0,
- cat: '',
- pid: 0,
- tid: 0,
- name: '',
- args: {frame: '0'},
- },
- {
- ph: 'B',
- ts: 1,
- cat: '',
- pid: 0,
- tid: 0,
- name: '',
- args: {frame: '1'},
- },
- {
- ph: 'E',
- ts: 2,
- cat: '',
- pid: 0,
- tid: 0,
- name: '',
- args: {frame: '0'},
- },
- {
- ph: 'E',
- ts: 2,
- cat: '',
- pid: 0,
- tid: 0,
- name: '',
- args: {frame: '1'},
- },
- ],
- ''
- );
- expect(trace.profiles[0].duration).toBe(2);
- expect(trace.profiles[0].appendOrderTree.children[0].selfWeight).toBe(1);
- expect(trace.profiles[0].appendOrderTree.children[0].totalWeight).toBe(2);
- expect(trace.profiles[0].appendOrderTree.children[0].frame.name).toBe(
- 'Unknown {"frame":"0"}'
- );
- expect(trace.profiles[0].appendOrderTree.children[0].children[0].frame.name).toBe(
- 'Unknown {"frame":"1"}'
- );
- expect(trace.profiles[0].appendOrderTree.children[0].children[0].selfWeight).toBe(1);
- expect(trace.profiles[0].appendOrderTree.children[0].children[0].totalWeight).toBe(1);
- });
- it('handles X trace with tdur', () => {
- const trace = parseTypescriptChromeTraceArrayFormat(
- [
- {
- ph: 'X',
- ts: 0,
- cat: '',
- pid: 0,
- tid: 0,
- tdur: 100,
- name: '',
- args: {frame: '0'},
- },
- ],
- ''
- );
- expect(trace.profiles[0].duration).toBe(100);
- });
- it('handles X trace with dur', () => {
- const trace = parseTypescriptChromeTraceArrayFormat(
- [
- {
- ph: 'X',
- ts: 0,
- cat: '',
- pid: 0,
- tid: 0,
- dur: 100,
- name: '',
- args: {frame: '0'},
- },
- ],
- ''
- );
- expect(trace.profiles[0].duration).toBe(100);
- });
- });
- describe('collapseSamples', () => {
- it.each([
- {
- samples: [1, 1],
- timeDeltas: [0, 1],
- expectedSamples: [1, 1],
- expectedTimeDeltas: [0, 1],
- },
- {
- samples: [1, 1, 1],
- timeDeltas: [0, 1, 1],
- expectedSamples: [1, 1],
- expectedTimeDeltas: [0, 2],
- },
- {
- samples: [1, 2, 1],
- timeDeltas: [0, 1, 2],
- expectedSamples: [1, 2, 1],
- expectedTimeDeltas: [0, 1, 3],
- },
- {
- samples: [1, 2, 3, 4],
- timeDeltas: [0, 1, 1, 1],
- expectedSamples: [1, 2, 3, 4],
- expectedTimeDeltas: [0, 1, 2, 3],
- },
- ])('collapses sample', test => {
- const result = collapseSamples({
- startTime: 0,
- endTime: 100,
- samples: test.samples,
- timeDeltas: test.timeDeltas,
- nodes: [],
- });
- expect(result.samples).toEqual(test.expectedSamples);
- expect(result.sampleTimes).toEqual(test.expectedTimeDeltas);
- });
- it('guards from negative samples', () => {
- const result = collapseSamples({
- startTime: 0,
- endTime: 100,
- samples: [1, 2, 3],
- timeDeltas: [1, -1, 1],
- nodes: [],
- });
- expect(result.samples).toEqual([1, 2, 3]);
- expect(result.sampleTimes).toEqual([1, 1, 2]);
- });
- it('guards from negative samples when they are being collapsed', () => {
- const result = collapseSamples({
- startTime: 0,
- endTime: 100,
- samples: [1, 1, 1],
- timeDeltas: [1, -1, 2],
- nodes: [],
- });
- expect(result.samples).toEqual([1, 1]);
- expect(result.sampleTimes).toEqual([1, 3]);
- });
- });
|