Skip to content

Commit b6a7208

Browse files
committed
first benchmark
1 parent 50438f9 commit b6a7208

3 files changed

Lines changed: 129 additions & 0 deletions

File tree

packages/core/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@
5454
"lint:fix": "OXLINT_TSGOLINT_DANGEROUSLY_SUPPRESS_PROGRAM_DIAGNOSTICS=true oxlint . --fix --type-aware",
5555
"lint": "OXLINT_TSGOLINT_DANGEROUSLY_SUPPRESS_PROGRAM_DIAGNOSTICS=true oxlint . --type-aware",
5656
"lint:es-compatibility": "es-check es2020 ./build/cjs/*.js && es-check es2020 ./build/esm/*.js --module",
57+
"bench": "vitest bench",
5758
"test": "vitest run",
5859
"test:watch": "vitest --watch",
5960
"yalc:publish": "yalc publish --push --sig"
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import { bench, describe } from 'vitest';
2+
import {
3+
addBreadcrumb,
4+
captureException,
5+
getCurrentScope,
6+
getIsolationScope,
7+
setCurrentClient,
8+
} from '../../src';
9+
import { getDefaultTestClientOptions, TestClient } from '../mocks/client';
10+
import { clearGlobalScope } from '../testutils';
11+
12+
function setupClient() {
13+
clearGlobalScope();
14+
getCurrentScope().clear();
15+
getIsolationScope().clear();
16+
17+
const client = new TestClient(
18+
getDefaultTestClientOptions({
19+
dsn: 'https://username@domain/123',
20+
enableSend: true,
21+
release: '1.0.0',
22+
environment: 'production',
23+
}),
24+
);
25+
setCurrentClient(client);
26+
client.init();
27+
return client;
28+
}
29+
30+
describe('captureException - minimal scope', () => {
31+
setupClient();
32+
33+
bench('captureException(new Error(...))', () => {
34+
captureException(new Error('Something went wrong'));
35+
});
36+
});
37+
38+
describe('captureException - realistic scope', () => {
39+
setupClient();
40+
41+
getCurrentScope().setUser({ id: '123', email: 'user@example.com' });
42+
getCurrentScope().setTag('service', 'api-gateway');
43+
getCurrentScope().setTag('region', 'us-east-1');
44+
getCurrentScope().setTag('version', '2.1.0');
45+
getCurrentScope().setExtra('request_id', 'req-abc-123');
46+
for (let i = 0; i < 10; i++) {
47+
addBreadcrumb({ message: `Action ${i}`, category: 'http', level: 'info' });
48+
}
49+
50+
bench('captureException(new Error(...))', () => {
51+
captureException(new Error('Something went wrong'));
52+
});
53+
});
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
import { bench, describe } from 'vitest';
2+
import {
3+
addBreadcrumb,
4+
getCurrentScope,
5+
getIsolationScope,
6+
setCurrentClient,
7+
startSpan,
8+
} from '../../src';
9+
import { getDefaultTestClientOptions, TestClient } from '../mocks/client';
10+
import { clearGlobalScope } from '../testutils';
11+
12+
function setupClient() {
13+
clearGlobalScope();
14+
getCurrentScope().clear();
15+
getIsolationScope().clear();
16+
17+
const client = new TestClient(
18+
getDefaultTestClientOptions({
19+
dsn: 'https://username@domain/123',
20+
enableSend: true,
21+
tracesSampleRate: 1,
22+
release: '1.0.0',
23+
environment: 'production',
24+
}),
25+
);
26+
setCurrentClient(client);
27+
client.init();
28+
return client;
29+
}
30+
31+
function addRealisticScopeData() {
32+
getCurrentScope().setUser({ id: '123', email: 'user@example.com' });
33+
getCurrentScope().setTag('service', 'api-gateway');
34+
getCurrentScope().setTag('region', 'us-east-1');
35+
getCurrentScope().setTag('version', '2.1.0');
36+
getCurrentScope().setExtra('request_id', 'req-abc-123');
37+
for (let i = 0; i < 10; i++) {
38+
addBreadcrumb({ message: `Action ${i}`, category: 'http', level: 'info' });
39+
}
40+
}
41+
42+
describe('startSpan pipeline - realistic scope', () => {
43+
setupClient();
44+
addRealisticScopeData();
45+
46+
bench('single root span', () => {
47+
startSpan({ name: 'GET /api/users', op: 'http.server' }, () => {
48+
// span lifecycle only
49+
});
50+
});
51+
52+
bench('root span + 5 child spans', () => {
53+
startSpan({ name: 'GET /api/users', op: 'http.server' }, () => {
54+
for (let i = 0; i < 5; i++) {
55+
startSpan({ name: `SELECT * FROM users WHERE id = $${i + 1}`, op: 'db' }, span => {
56+
span.setAttribute('db.system', 'postgresql');
57+
span.setAttribute('db.name', 'mydb');
58+
});
59+
}
60+
});
61+
});
62+
63+
bench('root span + 10 child spans', () => {
64+
startSpan({ name: 'GET /api/users', op: 'http.server' }, () => {
65+
for (let i = 0; i < 10; i++) {
66+
startSpan(
67+
{ name: i < 5 ? `db.query.${i}` : `http.request.${i}`, op: i < 5 ? 'db' : 'http.client' },
68+
span => {
69+
span.setAttribute('key', 'value');
70+
},
71+
);
72+
}
73+
});
74+
});
75+
});

0 commit comments

Comments
 (0)