123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422 |
- import {JSSelfProfile} from 'sentry/utils/profiling/profile/jsSelfProfile';
- import {createFrameIndex} from 'sentry/utils/profiling/profile/utils';
- import {firstCallee, makeTestingBoilerplate, nthCallee} from './profile.spec';
- describe('jsSelfProfile', () => {
- it('imports the base properties', () => {
- const trace: JSSelfProfiling.Trace = {
- resources: ['app.js', 'vendor.js'],
- frames: [{name: 'ReactDOM.render', line: 1, column: 1, resourceId: 0}],
- samples: [
- {
- timestamp: 0,
- stackId: 0,
- },
- {
- timestamp: 1000,
- stackId: 0,
- },
- ],
- stacks: [
- {
- frameId: 0,
- },
- ],
- };
- const profile = JSSelfProfile.FromProfile(
- trace,
- createFrameIndex('javascript', trace.frames, trace),
- {type: 'flamechart'}
- );
- expect(profile.duration).toBe(1000);
- expect(profile.startedAt).toBe(0);
- expect(profile.endedAt).toBe(1000);
- expect(profile.callTree.children[0].frame.name).toBe('ReactDOM.render');
- expect(profile.callTree.children[0].frame.resource).toBe('app.js');
- });
- it('tracks discarded samples', () => {
- const trace: JSSelfProfiling.Trace = {
- resources: ['app.js', 'vendor.js'],
- frames: [{name: 'ReactDOM.render', line: 1, column: 1, resourceId: 0}],
- samples: [
- {
- timestamp: 0,
- stackId: 0,
- },
- ],
- stacks: [
- {
- frameId: 0,
- },
- ],
- };
- const profile = JSSelfProfile.FromProfile(
- trace,
- createFrameIndex('javascript', [{name: 'f0'}]),
- {type: 'flamechart'}
- );
- expect(profile.stats.discardedSamplesCount).toBe(1);
- });
- it('tracks negative samples', () => {
- const trace: JSSelfProfiling.Trace = {
- resources: ['app.js', 'vendor.js'],
- frames: [{name: 'ReactDOM.render', line: 1, column: 1, resourceId: 0}],
- samples: [
- {
- timestamp: 0,
- stackId: 0,
- },
- {
- timestamp: -1,
- stackId: 0,
- },
- ],
- stacks: [
- {
- frameId: 0,
- },
- ],
- };
- const profile = JSSelfProfile.FromProfile(
- trace,
- createFrameIndex('javascript', [{name: 'f0'}]),
- {type: 'flamechart'}
- );
- expect(profile.stats.negativeSamplesCount).toBe(1);
- });
- it('tracks raw weights', () => {
- const trace: JSSelfProfiling.Trace = {
- resources: ['app.js', 'vendor.js'],
- frames: [{name: 'ReactDOM.render', line: 1, column: 1, resourceId: 0}],
- samples: [
- {
- timestamp: 5,
- stackId: 0,
- },
- {
- timestamp: 10,
- stackId: 0,
- },
- {
- timestamp: 15,
- stackId: 0,
- },
- ],
- stacks: [
- {
- frameId: 0,
- },
- ],
- };
- const profile = JSSelfProfile.FromProfile(
- trace,
- createFrameIndex('javascript', [{name: 'f0'}]),
- {type: 'flamechart'}
- );
- // For JsSelfProfile, first sample is appended with 0 weight because it
- // contains the stack sample of when startProfile was called
- expect(profile.rawWeights.length).toBe(2);
- });
- it('handles the first stack sample differently', () => {
- const trace: JSSelfProfiling.Trace = {
- resources: ['app.js'],
- frames: [
- {name: 'main', line: 1, column: 1, resourceId: 0},
- {name: 'new Profiler', line: 1, column: 1, resourceId: 0},
- {name: 'afterProfiler.init', line: 1, column: 1, resourceId: 0},
- ],
- samples: [
- {
- stackId: 1,
- timestamp: 500,
- },
- {
- stackId: 2,
- timestamp: 1500,
- },
- ],
- stacks: [
- {frameId: 0, parentId: undefined},
- {frameId: 1, parentId: 0},
- {frameId: 2, parentId: 0},
- ],
- };
- const {open, close, openSpy, closeSpy, timings} = makeTestingBoilerplate();
- const profile = JSSelfProfile.FromProfile(
- trace,
- createFrameIndex('javascript', trace.frames, trace),
- {type: 'flamechart'}
- );
- profile.forEach(open, close);
- expect(timings).toEqual([
- ['main', 'open'],
- ['new Profiler', 'open'],
- ['new Profiler', 'close'],
- ['afterProfiler.init', 'open'],
- ['afterProfiler.init', 'close'],
- ['main', 'close'],
- ]);
- expect(openSpy).toHaveBeenCalledTimes(3);
- expect(closeSpy).toHaveBeenCalledTimes(3);
- const root = firstCallee(profile.callTree);
- if (!root) {
- throw new Error('root is null');
- }
- expect(root.totalWeight).toEqual(1000);
- expect(root.selfWeight).toEqual(0);
- expect(nthCallee(root, 0).selfWeight).toEqual(0);
- expect(nthCallee(root, 0).totalWeight).toEqual(0);
- expect(nthCallee(root, 1).selfWeight).toEqual(1000);
- expect(nthCallee(root, 1).totalWeight).toEqual(1000);
- });
- it('rebuilds the stack', () => {
- const trace: JSSelfProfiling.Trace = {
- resources: ['app.js'],
- frames: [
- {name: 'f0', line: 1, column: 1, resourceId: 0},
- {name: 'f1', line: 1, column: 1, resourceId: 0},
- ],
- samples: [
- {
- stackId: 0,
- timestamp: 0,
- },
- {
- timestamp: 1000,
- stackId: 0,
- },
- ],
- stacks: [{frameId: 1, parentId: 1}, {frameId: 0}],
- };
- const {open, close, openSpy, closeSpy, timings} = makeTestingBoilerplate();
- const profile = JSSelfProfile.FromProfile(
- trace,
- createFrameIndex('javascript', trace.frames, trace),
- {type: 'flamechart'}
- );
- profile.forEach(open, close);
- expect(timings).toEqual([
- ['f0', 'open'],
- ['f1', 'open'],
- ['f1', 'close'],
- ['f0', 'close'],
- ]);
- expect(openSpy).toHaveBeenCalledTimes(2);
- expect(closeSpy).toHaveBeenCalledTimes(2);
- const root = firstCallee(profile.callTree);
- expect(root.totalWeight).toEqual(1000);
- expect(firstCallee(root).totalWeight).toEqual(1000);
- expect(root.selfWeight).toEqual(0);
- expect(firstCallee(root).selfWeight).toEqual(1000);
- });
- it('marks direct recursion', () => {
- const trace: JSSelfProfiling.Trace = {
- resources: ['app.js'],
- frames: [{name: 'f0', line: 1, column: 1, resourceId: 0}],
- samples: [
- {
- stackId: 0,
- timestamp: 0,
- },
- {
- stackId: 0,
- timestamp: 0,
- },
- ],
- stacks: [{frameId: 0, parentId: 1}, {frameId: 0}],
- };
- const profile = JSSelfProfile.FromProfile(
- trace,
- createFrameIndex('javascript', trace.frames, trace),
- {type: 'flamechart'}
- );
- expect(!!firstCallee(firstCallee(profile.callTree)).recursive).toBe(true);
- });
- it('marks indirect recursion', () => {
- const trace: JSSelfProfiling.Trace = {
- resources: ['app.js'],
- frames: [
- {name: 'f0', line: 1, column: 1, resourceId: 0},
- {name: 'f1', line: 1, column: 1, resourceId: 0},
- ],
- samples: [
- {
- stackId: 0,
- timestamp: 0,
- },
- {
- stackId: 2,
- timestamp: 100,
- },
- ],
- stacks: [
- {frameId: 0, parentId: undefined},
- {frameId: 1, parentId: 0},
- {frameId: 0, parentId: 1},
- ],
- };
- const profile = JSSelfProfile.FromProfile(
- trace,
- createFrameIndex('javascript', trace.frames, trace),
- {type: 'flamechart'}
- );
- expect(!!firstCallee(firstCallee(firstCallee(profile.callTree))).recursive).toBe(
- true
- );
- });
- it('tracks minFrameDuration', () => {
- const trace: JSSelfProfiling.Trace = {
- resources: ['app.js'],
- frames: [
- {name: 'f0', line: 1, column: 1, resourceId: 0},
- {name: 'f1', line: 1, column: 1, resourceId: 0},
- {name: 'f2', line: 1, column: 1, resourceId: 0},
- ],
- samples: [
- {
- stackId: 0,
- timestamp: 0,
- },
- {
- stackId: 2,
- timestamp: 10,
- },
- {
- stackId: 3,
- timestamp: 100,
- },
- ],
- stacks: [
- {frameId: 0, parentId: undefined},
- {frameId: 1, parentId: 0},
- {frameId: 0, parentId: 1},
- {frameId: 2},
- ],
- };
- const profile = JSSelfProfile.FromProfile(
- trace,
- createFrameIndex('javascript', trace.frames, trace),
- {type: 'flamechart'}
- );
- expect(profile.minFrameDuration).toBe(10);
- });
- it('appends gc to previous stack', () => {
- const trace: JSSelfProfiling.Trace = {
- resources: ['app.js'],
- frames: [
- {name: 'f1', line: 1, column: 1, resourceId: 0},
- {name: 'f0', line: 1, column: 1, resourceId: 0},
- ],
- samples: [
- {
- stackId: 0,
- timestamp: 0,
- },
- {
- timestamp: 10,
- marker: 'gc',
- stackId: 0,
- },
- ],
- stacks: [
- {frameId: 0, parentId: 1},
- {frameId: 1, parentId: undefined},
- ],
- };
- const profile = JSSelfProfile.FromProfile(
- trace,
- createFrameIndex('javascript', trace.frames, trace),
- {type: 'flamechart'}
- );
- const {open, close, openSpy, closeSpy, timings} = makeTestingBoilerplate();
- profile.forEach(open, close);
- expect(openSpy).toHaveBeenCalledTimes(3);
- expect(closeSpy).toHaveBeenCalledTimes(3);
- expect(timings).toEqual([
- ['f0', 'open'],
- ['f1', 'open'],
- ['Garbage Collection', 'open'],
- ['Garbage Collection', 'close'],
- ['f1', 'close'],
- ['f0', 'close'],
- ]);
- });
- it('flamegraph tracks node occurences', () => {
- const trace: JSSelfProfiling.Trace = {
- resources: ['app.js'],
- frames: [
- {name: 'f1', line: 1, column: 1, resourceId: 0},
- {name: 'f0', line: 1, column: 1, resourceId: 0},
- ],
- samples: [
- {
- stackId: 0,
- timestamp: 0,
- },
- {
- timestamp: 10,
- stackId: 1,
- },
- {
- timestamp: 20,
- stackId: 0,
- },
- ],
- stacks: [
- {frameId: 0, parentId: undefined},
- {frameId: 1, parentId: 0},
- ],
- };
- const profile = JSSelfProfile.FromProfile(
- trace,
- createFrameIndex('javascript', trace.frames, trace),
- {type: 'flamechart'}
- );
- expect(profile.callTree.children[0].count).toBe(3);
- expect(profile.callTree.children[0].children[0].count).toBe(1);
- });
- });
|