Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
import * as Sentry from '@sentry/browser';
import { browserProfilingIntegration } from '@sentry/browser';

window.Sentry = Sentry;

Sentry.init({
dsn: 'https://public@dsn.ingest.sentry.io/1337',
integrations: [browserProfilingIntegration()],
tracesSampleRate: 1,
profileSessionSampleRate: 1,
profileLifecycle: 'manual',
});

function largeSum(amount = 1000000) {
let sum = 0;
for (let i = 0; i < amount; i++) {
sum += Math.sqrt(i) * Math.sin(i);
}
}

function fibonacci(n) {
if (n <= 1) {
return n;
}
return fibonacci(n - 1) + fibonacci(n - 2);
}

function fibonacci1(n) {
if (n <= 1) {
return n;
}
return fibonacci1(n - 1) + fibonacci1(n - 2);
}

function fibonacci2(n) {
if (n <= 1) {
return n;
}
return fibonacci1(n - 1) + fibonacci1(n - 2);
}

function notProfiledFib(n) {
if (n <= 1) {
return n;
}
return fibonacci1(n - 1) + fibonacci1(n - 2);
}

// Adding setTimeout to ensure we cross the sampling interval to avoid flakes

// ---

Sentry.profiler.startProfiler();

fibonacci(40);
await new Promise(resolve => setTimeout(resolve, 25));

largeSum();
await new Promise(resolve => setTimeout(resolve, 25));

Sentry.profiler.stopProfiler();

// ---

notProfiledFib(40);
await new Promise(resolve => setTimeout(resolve, 25));

// ---

Sentry.profiler.startProfiler();

fibonacci2(40);
await new Promise(resolve => setTimeout(resolve, 25));

Sentry.profiler.stopProfiler();

// ---

const client = Sentry.getClient();
await client?.flush(8000);
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
import { expect } from '@playwright/test';
import type { ProfileChunkEnvelope } from '@sentry/core';
import { sentryTest } from '../../../utils/fixtures';
import {
countEnvelopes,
getMultipleSentryEnvelopeRequests,
properFullEnvelopeRequestParser,
shouldSkipTracingTest,
} from '../../../utils/helpers';
import { validateProfile, validateProfilePayloadMetadata } from '../test-utils';

sentryTest(
'does not send profile envelope when document-policy is not set',
async ({ page, getLocalTestUrl, browserName }) => {
if (shouldSkipTracingTest() || browserName !== 'chromium') {
// Profiling only works when tracing is enabled
sentryTest.skip();
}

const url = await getLocalTestUrl({ testDir: __dirname });

// Assert that no profile_chunk envelope is sent without policy header
const chunkCount = await countEnvelopes(page, { url, envelopeType: 'profile_chunk', timeout: 1500 });
expect(chunkCount).toBe(0);
},
);

sentryTest('sends profile_chunk envelopes in manual mode', async ({ page, getLocalTestUrl, browserName }) => {
if (shouldSkipTracingTest() || browserName !== 'chromium') {
// Profiling only works when tracing is enabled
sentryTest.skip();
}

const url = await getLocalTestUrl({ testDir: __dirname, responseHeaders: { 'Document-Policy': 'js-profiling' } });

// In manual mode we start and stop once -> expect exactly one chunk
const profileChunkEnvelopes = await getMultipleSentryEnvelopeRequests<ProfileChunkEnvelope>(
page,
2,
{ url, envelopeType: 'profile_chunk', timeout: 8000 },
properFullEnvelopeRequestParser,
);

expect(profileChunkEnvelopes.length).toBe(2);

// Validate the first chunk thoroughly
const profileChunkEnvelopeItem = profileChunkEnvelopes[0][1][0];
const envelopeItemHeader = profileChunkEnvelopeItem[0];
const envelopeItemPayload1 = profileChunkEnvelopeItem[1];

expect(envelopeItemHeader).toHaveProperty('type', 'profile_chunk');
expect(envelopeItemPayload1.profile).toBeDefined();

validateProfilePayloadMetadata(envelopeItemPayload1);

validateProfile(envelopeItemPayload1.profile, {
expectedFunctionNames: ['startJSSelfProfile', 'fibonacci', 'largeSum'],
minSampleDurationMs: 20,
isChunkFormat: true,
});

// only contains fibonacci
const functionNames1 = envelopeItemPayload1.profile.frames.map(frame => frame.function).filter(name => name !== '');
expect(functionNames1).toEqual(expect.not.arrayContaining(['fibonacci1', 'fibonacci2', 'fibonacci3']));

// === PROFILE CHUNK 2 ===

const profileChunkEnvelopeItem2 = profileChunkEnvelopes[1][1][0];
const envelopeItemHeader2 = profileChunkEnvelopeItem2[0];
const envelopeItemPayload2 = profileChunkEnvelopeItem2[1];

expect(envelopeItemHeader2).toHaveProperty('type', 'profile_chunk');
expect(envelopeItemPayload2.profile).toBeDefined();

validateProfilePayloadMetadata(envelopeItemPayload2);

validateProfile(envelopeItemPayload2.profile, {
expectedFunctionNames: [
'startJSSelfProfile',
'fibonacci1', // called by fibonacci2
'fibonacci2',
],
isChunkFormat: true,
});

// does not contain fibonacci3 (called during unprofiled part)
const functionNames2 = envelopeItemPayload1.profile.frames.map(frame => frame.function).filter(name => name !== '');
expect(functionNames2).toEqual(expect.not.arrayContaining(['fibonacci3']));
});
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ export function validateProfile(
}
}

// Frames
// FRAMES
expect(profile.frames.length).toBeGreaterThan(0);
for (const frame of profile.frames) {
expect(frame).toHaveProperty('function');
Expand Down
Loading
Loading