forked from nodejs/node
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlog.js
More file actions
78 lines (71 loc) · 1.75 KB
/
Copy pathlog.js
File metadata and controls
78 lines (71 loc) · 1.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
'use strict';
// A console throughput benchmark.
// Uses a custom Console with null Writable streams to avoid I/O latency.
const common = require('../common.js');
const { Writable } = require('stream');
const { Console } = require('console');
const bench = common.createBenchmark(main, {
n: [2e6],
variant: ['plain', 'format', 'object', 'group', 'info', 'warn', 'error'],
});
class Null extends Writable {
_write(chunk, enc, cb) { cb(); }
}
function makeConsole() {
const dn = new Null();
return new Console({ stdout: dn, stderr: dn, ignoreErrors: true, colorMode: false });
}
function main({ n, variant }) {
const c = makeConsole();
switch (variant) {
case 'plain': {
bench.start();
for (let i = 0; i < n; i++) c.log('hello world');
bench.end(n);
break;
}
case 'format': {
bench.start();
for (let i = 0; i < n; i++) c.log('%s %d %j', 'a', 42, { x: 1 });
bench.end(n);
break;
}
case 'object': {
const obj = { a: 1, b: 2, c: 3 };
bench.start();
for (let i = 0; i < n; i++) c.log(obj);
bench.end(n);
break;
}
case 'group': {
bench.start();
for (let i = 0; i < n; i++) {
c.group('g');
c.log('x');
c.groupEnd();
}
bench.end(n);
break;
}
case 'info': {
bench.start();
for (let i = 0; i < n; i++) c.info('hello world');
bench.end(n);
break;
}
case 'warn': {
bench.start();
for (let i = 0; i < n; i++) c.warn('hello world');
bench.end(n);
break;
}
case 'error': {
bench.start();
for (let i = 0; i < n; i++) c.error('hello world');
bench.end(n);
break;
}
default:
throw new Error('unknown variant');
}
}