|
| 1 | +import { expect } from '@playwright/test'; |
| 2 | +import type { ProfileChunkEnvelope } from '@sentry/core'; |
| 3 | +import { sentryTest } from '../../../utils/fixtures'; |
| 4 | +import { |
| 5 | + countEnvelopes, |
| 6 | + getMultipleSentryEnvelopeRequests, |
| 7 | + properFullEnvelopeRequestParser, |
| 8 | + shouldSkipTracingTest, |
| 9 | +} from '../../../utils/helpers'; |
| 10 | +import { validateProfile, validateProfilePayloadMetadata } from '../test-utils'; |
| 11 | + |
| 12 | +sentryTest( |
| 13 | + 'does not send profile envelope when document-policy is not set', |
| 14 | + async ({ page, getLocalTestUrl, browserName }) => { |
| 15 | + if (shouldSkipTracingTest() || browserName !== 'chromium') { |
| 16 | + // Profiling only works when tracing is enabled |
| 17 | + sentryTest.skip(); |
| 18 | + } |
| 19 | + |
| 20 | + const url = await getLocalTestUrl({ testDir: __dirname }); |
| 21 | + |
| 22 | + // Assert that no profile_chunk envelope is sent without policy header |
| 23 | + const chunkCount = await countEnvelopes(page, { url, envelopeType: 'profile_chunk', timeout: 1500 }); |
| 24 | + expect(chunkCount).toBe(0); |
| 25 | + }, |
| 26 | +); |
| 27 | + |
| 28 | +sentryTest('sends profile_chunk envelopes in manual mode', async ({ page, getLocalTestUrl, browserName }) => { |
| 29 | + if (shouldSkipTracingTest() || browserName !== 'chromium') { |
| 30 | + // Profiling only works when tracing is enabled |
| 31 | + sentryTest.skip(); |
| 32 | + } |
| 33 | + |
| 34 | + const url = await getLocalTestUrl({ testDir: __dirname, responseHeaders: { 'Document-Policy': 'js-profiling' } }); |
| 35 | + |
| 36 | + // In manual mode we start and stop once -> expect exactly one chunk |
| 37 | + const profileChunkEnvelopes = await getMultipleSentryEnvelopeRequests<ProfileChunkEnvelope>( |
| 38 | + page, |
| 39 | + 2, |
| 40 | + { url, envelopeType: 'profile_chunk', timeout: 8000 }, |
| 41 | + properFullEnvelopeRequestParser, |
| 42 | + ); |
| 43 | + |
| 44 | + expect(profileChunkEnvelopes.length).toBe(2); |
| 45 | + |
| 46 | + // Validate the first chunk thoroughly |
| 47 | + const profileChunkEnvelopeItem = profileChunkEnvelopes[0][1][0]; |
| 48 | + const envelopeItemHeader = profileChunkEnvelopeItem[0]; |
| 49 | + const envelopeItemPayload1 = profileChunkEnvelopeItem[1]; |
| 50 | + |
| 51 | + expect(envelopeItemHeader).toHaveProperty('type', 'profile_chunk'); |
| 52 | + expect(envelopeItemPayload1.profile).toBeDefined(); |
| 53 | + |
| 54 | + const profilerId1 = envelopeItemPayload1.profiler_id; |
| 55 | + |
| 56 | + validateProfilePayloadMetadata(envelopeItemPayload1); |
| 57 | + |
| 58 | + validateProfile(envelopeItemPayload1.profile, { |
| 59 | + expectedFunctionNames: ['startJSSelfProfile', 'fibonacci', 'largeSum'], |
| 60 | + minSampleDurationMs: 20, |
| 61 | + isChunkFormat: true, |
| 62 | + }); |
| 63 | + |
| 64 | + // only contains fibonacci |
| 65 | + const functionNames1 = envelopeItemPayload1.profile.frames.map(frame => frame.function).filter(name => name !== ''); |
| 66 | + expect(functionNames1).toEqual(expect.not.arrayContaining(['fibonacci1', 'fibonacci2', 'fibonacci3'])); |
| 67 | + |
| 68 | + // === PROFILE CHUNK 2 === |
| 69 | + |
| 70 | + const profileChunkEnvelopeItem2 = profileChunkEnvelopes[1][1][0]; |
| 71 | + const envelopeItemHeader2 = profileChunkEnvelopeItem2[0]; |
| 72 | + const envelopeItemPayload2 = profileChunkEnvelopeItem2[1]; |
| 73 | + |
| 74 | + expect(envelopeItemHeader2).toHaveProperty('type', 'profile_chunk'); |
| 75 | + expect(envelopeItemPayload2.profile).toBeDefined(); |
| 76 | + |
| 77 | + expect(envelopeItemPayload2.profiler_id).toBe(profilerId1); // same profiler id for the whole session |
| 78 | + |
| 79 | + validateProfilePayloadMetadata(envelopeItemPayload2); |
| 80 | + |
| 81 | + validateProfile(envelopeItemPayload2.profile, { |
| 82 | + expectedFunctionNames: [ |
| 83 | + 'startJSSelfProfile', |
| 84 | + 'fibonacci1', // called by fibonacci2 |
| 85 | + 'fibonacci2', |
| 86 | + ], |
| 87 | + isChunkFormat: true, |
| 88 | + }); |
| 89 | + |
| 90 | + // does not contain notProfiledFib (called during unprofiled part) |
| 91 | + const functionNames2 = envelopeItemPayload2.profile.frames.map(frame => frame.function).filter(name => name !== ''); |
| 92 | + expect(functionNames2).toEqual(expect.not.arrayContaining(['notProfiledFib'])); |
| 93 | +}); |
0 commit comments