Skip to content

Commit dcd34b0

Browse files
bcoetargos
authored andcommittedFeb 2, 2021
benchmark: add benchmark for NODE_V8_COVERAGE
PR-URL: #36972 Reviewed-By: Rich Trott <rtrott@gmail.com> Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com> Reviewed-By: Richard Lau <rlau@redhat.com>
1 parent 6f54a14 commit dcd34b0

File tree

2 files changed

+147
-0
lines changed

2 files changed

+147
-0
lines changed
 
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
'use strict';
2+
3+
// Exercise coverage of a class. Note, this logic is silly and exists solely
4+
// to generate branch coverage code paths:
5+
class CoveredClass {
6+
constructor(x, y, opts) {
7+
this.x = x;
8+
this.y = y;
9+
// Exercise coverage of nullish coalescing:
10+
this.opts = opts ?? (Math.random() > 0.5 ? {} : undefined);
11+
}
12+
add() {
13+
return this.x + this.y;
14+
}
15+
addSpecial() {
16+
// Exercise coverage of optional chains:
17+
if (this.opts?.special && this.opts?.special?.x && this.opts?.special?.y) {
18+
return this.opts.special.x + this.opts.special.y;
19+
}
20+
return add();
21+
}
22+
mult() {
23+
return this.x * this.y;
24+
}
25+
multSpecial() {
26+
if (this.opts?.special && this.opts?.special?.x && this.opts?.special?.y) {
27+
return this.opts.special.x * this.opts.special.y;
28+
}
29+
return mult();
30+
}
31+
}
32+
33+
// Excercise coverage of functions:
34+
function add(x, y) {
35+
const mt = new CoveredClass(x, y);
36+
return mt.add();
37+
}
38+
39+
function addSpecial(x, y) {
40+
let mt;
41+
if (Math.random() > 0.5) {
42+
mt = new CoveredClass(x, y);
43+
} else {
44+
mt = new CoveredClass(x, y, {
45+
special: {
46+
x: Math.random() * x,
47+
y: Math.random() * y
48+
}
49+
});
50+
}
51+
return mt.addSpecial();
52+
}
53+
54+
function mult(x, y) {
55+
const mt = new CoveredClass(x, y);
56+
return mt.mult();
57+
}
58+
59+
function multSpecial(x, y) {
60+
let mt;
61+
if (Math.random() > 0.5) {
62+
mt = new CoveredClass(x, y);
63+
} else {
64+
mt = new CoveredClass(x, y, {
65+
special: {
66+
x: Math.random() * x,
67+
y: Math.random() * y
68+
}
69+
});
70+
}
71+
return mt.multSpecial();
72+
}
73+
74+
for (let i = 0; i < parseInt(process.env.N); i++) {
75+
const operations = ['add', 'addSpecial', 'mult', 'multSpecial'];
76+
for (const operation of operations) {
77+
// Exercise coverage of switch statements:
78+
switch (operation) {
79+
case 'add':
80+
if (add(Math.random() * 10, Math.random() * 10) > 10) {
81+
// Exercise coverage of ternary operations:
82+
let r = addSpecial(Math.random() * 10, Math.random() * 10) > 10 ?
83+
mult(Math.random() * 10, Math.random() * 10) :
84+
add(Math.random() * 10, Math.random() * 10);
85+
// Exercise && and ||
86+
if (r && Math.random() > 0.5 || Math.random() < 0.5) r++;
87+
}
88+
break;
89+
case 'addSpecial':
90+
if (addSpecial(Math.random() * 10, Math.random() * 10) > 10 &&
91+
add(Math.random() * 10, Math.random() * 10) > 10) {
92+
let r = mult(Math.random() * 10, Math.random() * 10) > 10 ?
93+
add(Math.random() * 10, Math.random() * 10) > 10 :
94+
mult(Math.random() * 10, Math.random() * 10);
95+
if (r && Math.random() > 0.5 || Math.random() < 0.5) r++;
96+
}
97+
break;
98+
case 'mult':
99+
if (mult(Math.random() * 10, Math.random() * 10) > 10) {
100+
let r = multSpecial(Math.random() * 10, Math.random() * 10) > 10 ?
101+
add(Math.random() * 10, Math.random() * 10) :
102+
mult(Math.random() * 10, Math.random() * 10);
103+
if (r && Math.random() > 0.5 || Math.random() < 0.5) r++;
104+
}
105+
break;
106+
case 'multSpecial':
107+
while (multSpecial(Math.random() * 10, Math.random() * 10) < 10) {
108+
mult(Math.random() * 10, Math.random() * 10);
109+
}
110+
break;
111+
default:
112+
break;
113+
}
114+
}
115+
}

‎benchmark/process/coverage.js

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// This benchmark is meant to exercise a grab bag of code paths that would
2+
// be expected to run slower under coverage.
3+
'use strict';
4+
5+
const common = require('../common.js');
6+
const bench = common.createBenchmark(main, {
7+
n: [1e5]
8+
});
9+
const path = require('path');
10+
const { rmSync } = require('fs');
11+
const { spawnSync } = require('child_process');
12+
const tmpdir = require('../../test/common/tmpdir');
13+
14+
const coverageDir = path.join(tmpdir.path, `./cov-${Date.now()}`);
15+
16+
function main({ n }) {
17+
bench.start();
18+
const result = spawnSync(process.execPath, [
19+
require.resolve('../fixtures/coverage-many-branches'),
20+
], {
21+
env: {
22+
NODE_V8_COVERAGE: coverageDir,
23+
N: n,
24+
...process.env
25+
}
26+
});
27+
bench.end(n);
28+
rmSync(coverageDir, { recursive: true, force: true });
29+
if (result.status !== 0) {
30+
throw new Error(result.stderr.toString('utf8'));
31+
}
32+
}

0 commit comments

Comments
 (0)