Skip to content

Commit 2c512a9

Browse files
committed
bench logger msg
1 parent a39189a commit 2c512a9

File tree

2 files changed

+63
-1
lines changed

2 files changed

+63
-1
lines changed

packages/logger/tests/logger.bench.ts

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
import { jsonStringify, Logger } from '@graphql-hive/logger';
2+
import { bench, describe } from 'vitest';
3+
4+
const voidlog = new Logger({
5+
writers: [
6+
{
7+
write() {
8+
// void
9+
},
10+
},
11+
],
12+
});
13+
14+
describe.each([
15+
{ name: 'string' as const, value: 'hello' },
16+
{ name: 'integer' as const, value: 7 },
17+
{ name: 'float' as const, value: 7.77 },
18+
{ name: 'object' as const, value: { hello: 'world' } },
19+
])('log formatting of $name', ({ name, value }) => {
20+
// we switch outside of the bench to avoid the overhead of the switch
21+
switch (name) {
22+
case 'string':
23+
bench('template literals', () => {
24+
voidlog.info(`hi there ${value}`);
25+
});
26+
bench('interpolation', () => {
27+
voidlog.info('hi there %s', value);
28+
});
29+
break;
30+
case 'integer':
31+
bench('template literals', () => {
32+
voidlog.info(`hi there ${value}`);
33+
});
34+
bench('interpolation', () => {
35+
voidlog.info('hi there %i', value);
36+
});
37+
break;
38+
case 'float':
39+
bench('template literals', () => {
40+
voidlog.info(`hi there ${value}`);
41+
});
42+
bench('interpolation', () => {
43+
voidlog.info('hi there %d', value);
44+
});
45+
break;
46+
case 'object':
47+
bench('template literals native stringify', () => {
48+
voidlog.info(`hi there ${JSON.stringify(value)}`);
49+
});
50+
bench('template literals logger stringify', () => {
51+
voidlog.info(`hi there ${jsonStringify(value)}`);
52+
});
53+
bench('interpolation', () => {
54+
voidlog.info('hi there %o', value);
55+
});
56+
break;
57+
}
58+
});

vitest.projects.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,11 @@ export default defineWorkspace([
4040
hookTimeout: testTimeout,
4141
testTimeout,
4242
benchmark: {
43-
include: ['bench/**/*.bench.ts', 'e2e/**/*.bench.ts'],
43+
include: [
44+
'bench/**/*.bench.ts',
45+
'e2e/**/*.bench.ts',
46+
'**/tests/**/*.bench.ts',
47+
],
4448
reporters: ['verbose'],
4549
outputJson: 'bench/results.json',
4650
},

0 commit comments

Comments
 (0)