123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517 |
- import merge from 'lodash/merge';
- import {DeepPartial} from 'sentry/types/utils';
- import {Frame} from '../frame';
- import {makeTestingBoilerplate} from './profile.spec';
- import {SentrySampledProfile} from './sentrySampledProfile';
- import {createSentrySampleProfileFrameIndex} from './utils';
- export const makeSentrySampledProfile = (
- profile?: DeepPartial<Profiling.SentrySampledProfile>
- ) => {
- return merge(
- {
- event_id: '1',
- version: '1',
- os: {
- name: 'iOS',
- version: '16.0',
- build_number: '19H253',
- },
- device: {
- architecture: 'arm64e',
- is_emulator: false,
- locale: 'en_US',
- manufacturer: 'Apple',
- model: 'iPhone14,3',
- },
- timestamp: '2022-09-01T09:45:00.000Z',
- platform: 'cocoa',
- profile: {
- samples: [
- {
- stack_id: 0,
- thread_id: '0',
- elapsed_since_start_ns: 0,
- },
- {
- stack_id: 1,
- thread_id: '0',
- elapsed_since_start_ns: 1000,
- },
- ],
- frames: [
- {
- function: 'main',
- instruction_addr: '',
- lineno: 1,
- colno: 1,
- file: 'main.c',
- },
- {
- function: 'foo',
- instruction_addr: '',
- lineno: 2,
- colno: 2,
- file: 'main.c',
- },
- ],
- stacks: [[1, 0], [0]],
- },
- transaction: {
- id: '',
- name: 'foo',
- active_thread_id: 0,
- trace_id: '1',
- },
- },
- profile
- ) as Profiling.SentrySampledProfile;
- };
- describe('SentrySampledProfile', () => {
- it('constructs a profile', () => {
- const sampledProfile: Profiling.SentrySampledProfile = makeSentrySampledProfile();
- const profile = SentrySampledProfile.FromProfile(
- sampledProfile,
- createSentrySampleProfileFrameIndex(sampledProfile.profile.frames, 'javascript'),
- {type: 'flamechart'}
- );
- const {open, close, timings} = makeTestingBoilerplate();
- profile.forEach(open, close);
- expect(profile.duration).toBe(1000);
- expect(timings).toEqual([
- ['main', 'open'],
- ['foo', 'open'],
- ['foo', 'close'],
- ['main', 'close'],
- ]);
- expect(profile.startedAt).toEqual(0);
- expect(profile.endedAt).toEqual(1000);
- });
- it('tracks discarded samples', () => {
- const sampledProfile = makeSentrySampledProfile({
- transaction: {
- id: '',
- name: 'foo',
- active_thread_id: 1,
- trace_id: '1',
- },
- profile: {
- samples: [
- {
- stack_id: 0,
- elapsed_since_start_ns: 1000,
- thread_id: '0',
- },
- {
- stack_id: 0,
- elapsed_since_start_ns: 1000,
- thread_id: '0',
- },
- ],
- thread_metadata: {
- '0': {
- name: 'bar',
- },
- },
- },
- });
- const profile = SentrySampledProfile.FromProfile(
- sampledProfile,
- createSentrySampleProfileFrameIndex(sampledProfile.profile.frames, 'javascript'),
- {type: 'flamechart'}
- );
- expect(profile.stats.discardedSamplesCount).toBe(2);
- });
- it('tracks negative samples', () => {
- const sampledProfile = makeSentrySampledProfile({
- transaction: {
- id: '',
- name: 'foo',
- active_thread_id: 1,
- trace_id: '1',
- },
- profile: {
- samples: [
- {
- stack_id: 0,
- elapsed_since_start_ns: 1000,
- thread_id: '0',
- },
- {
- stack_id: 0,
- elapsed_since_start_ns: -1000,
- thread_id: '0',
- },
- ],
- thread_metadata: {
- '0': {
- name: 'bar',
- },
- },
- },
- });
- const profile = SentrySampledProfile.FromProfile(
- sampledProfile,
- createSentrySampleProfileFrameIndex(sampledProfile.profile.frames, 'javascript'),
- {type: 'flamechart'}
- );
- expect(profile.stats.negativeSamplesCount).toBe(1);
- });
- it('tracks raw weights', () => {
- const sampledProfile = makeSentrySampledProfile({
- transaction: {
- id: '',
- name: 'foo',
- active_thread_id: 1,
- trace_id: '1',
- },
- profile: {
- samples: [
- {
- stack_id: 0,
- elapsed_since_start_ns: 1000,
- thread_id: '0',
- },
- {
- stack_id: 0,
- elapsed_since_start_ns: 2000,
- thread_id: '0',
- },
- {
- stack_id: 0,
- elapsed_since_start_ns: 3000,
- thread_id: '0',
- },
- ],
- thread_metadata: {
- '0': {
- name: 'bar',
- },
- },
- },
- });
- const profile = SentrySampledProfile.FromProfile(
- sampledProfile,
- createSentrySampleProfileFrameIndex(sampledProfile.profile.frames, 'javascript'),
- {type: 'flamechart'}
- );
- expect(profile.rawWeights.length).toBe(2);
- });
- it('derives a profile name from the transaction.name and thread_id', () => {
- const sampledProfile = makeSentrySampledProfile({
- transaction: {
- id: '',
- name: 'foo',
- active_thread_id: 1,
- trace_id: '1',
- },
- profile: {
- samples: [
- {
- stack_id: 0,
- elapsed_since_start_ns: 1000,
- thread_id: '0',
- },
- ],
- thread_metadata: {
- '0': {
- name: 'bar',
- },
- },
- },
- });
- const profile = SentrySampledProfile.FromProfile(
- sampledProfile,
- createSentrySampleProfileFrameIndex(sampledProfile.profile.frames, 'javascript'),
- {type: 'flamechart'}
- );
- expect(profile.name).toBe('bar');
- expect(profile.threadId).toBe(0);
- });
- it('derives a profile name from just thread_id', () => {
- const sampledProfile = makeSentrySampledProfile({
- platform: 'python',
- profile: {
- samples: [
- {
- stack_id: 0,
- elapsed_since_start_ns: 1000,
- thread_id: '0',
- },
- ],
- thread_metadata: {},
- },
- });
- const profile = SentrySampledProfile.FromProfile(
- sampledProfile,
- createSentrySampleProfileFrameIndex(sampledProfile.profile.frames, 'javascript'),
- {type: 'flamechart'}
- );
- expect(profile.name).toBe('');
- expect(profile.threadId).toBe(0);
- });
- it('derives a profile name from just thread name', () => {
- const sampledProfile = makeSentrySampledProfile({
- profile: {
- samples: [
- {
- stack_id: 0,
- elapsed_since_start_ns: 1000,
- thread_id: '0',
- },
- ],
- thread_metadata: {
- '0': {
- name: 'foo',
- },
- },
- },
- });
- const profile = SentrySampledProfile.FromProfile(
- sampledProfile,
- createSentrySampleProfileFrameIndex(sampledProfile.profile.frames, 'javascript'),
- {type: 'flamechart'}
- );
- expect(profile.name).toBe('foo');
- expect(profile.threadId).toBe(0);
- });
- it('derives a coca profile name from active thread id', () => {
- const sampledProfile = makeSentrySampledProfile({
- profile: {
- samples: [
- {
- stack_id: 0,
- elapsed_since_start_ns: 1000,
- thread_id: '0',
- },
- ],
- thread_metadata: {},
- },
- });
- const profile = SentrySampledProfile.FromProfile(
- sampledProfile,
- createSentrySampleProfileFrameIndex(sampledProfile.profile.frames, 'javascript'),
- {type: 'flamechart'}
- );
- expect(profile.name).toBe('com.apple.main-thread');
- expect(profile.threadId).toBe(0);
- });
- it('derives a coca profile name from queue label', () => {
- const sampledProfile = makeSentrySampledProfile({
- profile: {
- samples: [
- {
- stack_id: 0,
- elapsed_since_start_ns: 1000,
- thread_id: '1',
- queue_address: '0x000000016bec7180',
- },
- ],
- thread_metadata: {},
- queue_metadata: {
- '0x000000016bec7180': {label: 'sentry-http-transport'},
- },
- },
- });
- const profile = SentrySampledProfile.FromProfile(
- sampledProfile,
- createSentrySampleProfileFrameIndex(sampledProfile.profile.frames, 'javascript'),
- {type: 'flamechart'}
- );
- expect(profile.name).toBe('sentry-http-transport');
- expect(profile.threadId).toBe(1);
- });
- it('derives a coca profile name from queue label thats main thread', () => {
- const sampledProfile = makeSentrySampledProfile({
- transaction: {
- id: '',
- name: 'foo',
- active_thread_id: 1,
- trace_id: '1',
- },
- profile: {
- samples: [
- {
- stack_id: 0,
- elapsed_since_start_ns: 1000,
- thread_id: '1',
- queue_address: '0x000000016bec7180',
- },
- ],
- thread_metadata: {},
- queue_metadata: {
- '0x000000016bec7180': {label: 'com.apple.main-thread'},
- },
- },
- });
- const profile = SentrySampledProfile.FromProfile(
- sampledProfile,
- createSentrySampleProfileFrameIndex(sampledProfile.profile.frames, 'javascript'),
- {type: 'flamechart'}
- );
- expect(profile.name).toBe('com.apple.main-thread');
- expect(profile.threadId).toBe(1);
- });
- it('derives a coca profile name from queue label thats not main thread', () => {
- const sampledProfile = makeSentrySampledProfile({
- transaction: {
- id: '',
- name: 'foo',
- active_thread_id: 0,
- trace_id: '1',
- },
- profile: {
- samples: [
- {
- stack_id: 0,
- elapsed_since_start_ns: 1000,
- thread_id: '1',
- queue_address: '0x000000016bec7180',
- },
- ],
- thread_metadata: {},
- queue_metadata: {
- '0x000000016bec7180': {label: 'com.apple.main-thread'},
- },
- },
- });
- const profile = SentrySampledProfile.FromProfile(
- sampledProfile,
- createSentrySampleProfileFrameIndex(sampledProfile.profile.frames, 'javascript'),
- {type: 'flamechart'}
- );
- expect(profile.name).toBe('');
- expect(profile.threadId).toBe(1);
- });
- it('flamegraph tracks node occurrences', () => {
- const sampledProfile = makeSentrySampledProfile({
- transaction: {
- id: '',
- name: 'foo',
- active_thread_id: 1,
- trace_id: '1',
- },
- profile: {
- samples: [
- {
- stack_id: 0,
- elapsed_since_start_ns: 1000,
- thread_id: '0',
- },
- {
- stack_id: 1,
- elapsed_since_start_ns: 2000,
- thread_id: '0',
- },
- {
- stack_id: 0,
- elapsed_since_start_ns: 3000,
- thread_id: '0',
- },
- ],
- thread_metadata: {
- '0': {
- name: 'bar',
- },
- },
- // Frame 0 occurs 3 times, frame 1 occurs once
- stacks: [[0], [1, 0], [0]],
- frames: [{function: 'f0'}, {function: 'f1'}, {function: 'f2'}],
- },
- });
- const profile = SentrySampledProfile.FromProfile(
- sampledProfile,
- createSentrySampleProfileFrameIndex(sampledProfile.profile.frames, 'javascript'),
- {type: 'flamegraph'}
- );
- expect(profile.callTree.children[0].count).toBe(2);
- expect(profile.callTree.children[0].children[0].count).toBe(1);
- });
- it('filters frames', () => {
- const sampledProfile = makeSentrySampledProfile({
- transaction: {
- id: '',
- name: 'foo',
- active_thread_id: 1,
- trace_id: '1',
- },
- profile: {
- samples: [
- {
- stack_id: 0,
- elapsed_since_start_ns: 1000,
- thread_id: '0',
- },
- {
- stack_id: 0,
- elapsed_since_start_ns: 2000,
- thread_id: '0',
- },
- ],
- thread_metadata: {
- '0': {
- name: 'bar',
- },
- },
- stacks: [[1, 0]],
- frames: [{function: 'f0'}, {function: 'f1'}],
- },
- });
- const profile = SentrySampledProfile.FromProfile(
- sampledProfile,
- createSentrySampleProfileFrameIndex(sampledProfile.profile.frames, 'javascript'),
- {
- type: 'flamegraph',
- frameFilter: frame => frame.name === 'f0',
- }
- );
- expect(profile.callTree.frame).toBe(Frame.Root);
- expect(profile.callTree.children).toHaveLength(1);
- expect(profile.callTree.children[0].frame.name).toEqual('f0');
- // the f1 frame is filtered out, so the f0 frame has no children
- expect(profile.callTree.children[0].children).toHaveLength(0);
- });
- });
|