Skip to content

Commit 9855636

Browse files
mayurkale22danielkhan
authored andcommitted
Add initial benchmark suite (#390)
* feat: add initial benchmark suite * fix: remove nock * feat: add propagator benchmarks * fix: use new package names
1 parent ee1533a commit 9855636

File tree

6 files changed

+152
-1
lines changed

6 files changed

+152
-1
lines changed

benchmark/benchmark.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
'use strict';
2+
3+
const Benchmark = require('benchmark');
4+
const benchmarks = require('beautify-benchmark');
5+
6+
Benchmark.options.maxTime = 0;
7+
// @todo : Change it to between 50-100 or keep it random.
8+
Benchmark.options.minSamples = 10;
9+
10+
module.exports = () => {
11+
const suite = new Benchmark.Suite();
12+
13+
return suite
14+
.on('cycle', event => {
15+
benchmarks.add(event.target);
16+
})
17+
.on('error', event => {
18+
throw event.target.error;
19+
})
20+
.on('complete', function () {
21+
benchmarks.log();
22+
});
23+
};

benchmark/index.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
'use strict';
2+
3+
const execSync = require('child_process').execSync;
4+
const exec = cmd => execSync(cmd, { stdio: [0, 1, 2] });
5+
6+
exec('node benchmark/tracer');
7+
exec('node benchmark/propagator');

benchmark/propagator.js

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
'use strict';
2+
3+
const benchmark = require('./benchmark');
4+
const opentelemetry = require('@opentelemetry/core');
5+
6+
const setups = [
7+
{
8+
name: 'B3Format',
9+
propagator: new opentelemetry.B3Format(),
10+
injectCarrier: {},
11+
extractCarrier: {
12+
'x-b3-traceid': 'd4cda95b652f4a1592b449d5929fda1b',
13+
'x-b3-spanid': '6e0c63257de34c92'
14+
}
15+
},
16+
{
17+
name: 'HttpTraceContext',
18+
propagator: new opentelemetry.HttpTraceContext(),
19+
injectCarrier: {},
20+
extractCarrier: {
21+
traceparent: '00-d4cda95b652f4a1592b449d5929fda1b-6e0c63257de34c92-00'
22+
}
23+
}
24+
];
25+
26+
for (const setup of setups) {
27+
console.log(`Beginning ${setup.name} Benchmark...`);
28+
const propagator = setup.propagator;
29+
const suite = benchmark()
30+
.add('#Inject', function () {
31+
propagator.inject({
32+
traceId: 'd4cda95b652f4a1592b449d5929fda1b',
33+
spanId: '6e0c63257de34c92'
34+
}, setup.name, setup.injectCarrier);
35+
})
36+
.add('#Extract', function () {
37+
propagator.extract(setup.name, setup.extractCarrier);
38+
});
39+
40+
// run async
41+
suite.run({ async: false });
42+
}

benchmark/tracer.js

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
'use strict';
2+
3+
const benchmark = require('./benchmark');
4+
const opentelemetry = require('@opentelemetry/core');
5+
const { BasicTracer, BatchSpanProcessor, InMemorySpanExporter, SimpleSpanProcessor } = require('@opentelemetry/tracing');
6+
const { NodeTracer } = require('@opentelemetry/node');
7+
8+
const exporter = new InMemorySpanExporter();
9+
const logger = new opentelemetry.NoopLogger();
10+
11+
const setups = [
12+
{
13+
name: 'BasicTracer',
14+
tracer: new BasicTracer({ logger })
15+
},
16+
{
17+
name: 'NodeTracer',
18+
tracer: new NodeTracer({ logger })
19+
}
20+
];
21+
22+
for (const setup of setups) {
23+
console.log(`Beginning ${setup.name} Benchmark...`);
24+
const tracer = setup.tracer;
25+
const suite = benchmark()
26+
.add('#startSpan', function () {
27+
const span = tracer.startSpan('op');
28+
span.end();
29+
})
30+
.add('#startSpan:parent', function () {
31+
const span = tracer.startSpan('op');
32+
const childSpan = tracer.startSpan('client-op', { parent: span });
33+
childSpan.end();
34+
span.end();
35+
})
36+
.add('#startSpan with attribute', function () {
37+
const span = tracer.startSpan('op');
38+
span.setAttribute('attr-key-one', 'attr-value-one');
39+
span.end();
40+
})
41+
.add('#startSpan with 30 attributes', function () {
42+
const span = tracer.startSpan('op');
43+
for (let j = 0; j < 30; j++) {
44+
span.setAttribute('attr-key-' + j, 'attr-value-' + j);
45+
}
46+
span.end();
47+
})
48+
.add('#startSpan with 100 attributes', function () {
49+
const span = tracer.startSpan('op');
50+
for (let j = 0; j < 100; j++) {
51+
span.setAttribute('attr-key-' + j, 'attr-value-' + j);
52+
}
53+
span.end();
54+
})
55+
.add('#startSpan with SimpleSpanProcessor', function () {
56+
const simpleSpanProcessor = new SimpleSpanProcessor(exporter);
57+
58+
tracer.addSpanProcessor(simpleSpanProcessor);
59+
const span = tracer.startSpan('op');
60+
span.end();
61+
62+
simpleSpanProcessor.shutdown();
63+
})
64+
.add('#startSpan with BatchSpanProcessor', function () {
65+
const batchSpanProcessor = new BatchSpanProcessor(exporter);
66+
67+
tracer.addSpanProcessor(batchSpanProcessor);
68+
const span = tracer.startSpan('op');
69+
span.end();
70+
batchSpanProcessor.shutdown();
71+
});
72+
73+
// run async
74+
suite.run({ async: false });
75+
}

lerna.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
"npmClient": "yarn",
44
"packages": [
55
"packages/*",
6-
"examples/*"
6+
"examples/*",
7+
"benchmark/*"
78
],
89
"version": "0.1.1",
910
"changelog": {

package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
"main": "build/src/index.js",
66
"types": "build/src/index.d.ts",
77
"scripts": {
8+
"bench": "node benchmark",
89
"clean": "lerna run clean",
910
"fix": "lerna run fix",
1011
"postinstall": "yarn run bootstrap",
@@ -35,6 +36,8 @@
3536
"@commitlint/cli": "^8.2.0",
3637
"@commitlint/config-conventional": "^8.2.0",
3738
"husky": "^3.0.9",
39+
"benchmark": "^2.1.4",
40+
"beautify-benchmark": "^0.2.4",
3841
"gts": "^1.0.0",
3942
"lerna": "^3.17.0",
4043
"lerna-changelog": "^0.8.2",

0 commit comments

Comments
 (0)