|
| 1 | +// This benchmark is meant to exercise a grab bag of code paths that would |
| 2 | +// be expected to run slower under coverage. To use this benchmark, create an |
| 3 | +// alternate node bin that enables coverage and compare it against ./node |
| 4 | +// |
| 5 | +// ./node-coverage |
| 6 | +// #!/bin/bash |
| 7 | +// NODE_V8_COVERAGE=./coverage ./node $@ |
| 8 | +'use strict'; |
| 9 | +const common = require('../common.js'); |
| 10 | +const bench = common.createBenchmark(main, { |
| 11 | + n: [1e5] |
| 12 | +}); |
| 13 | + |
| 14 | +// Exercise coverage of a class. Note, this logic is silly and exists solely |
| 15 | +// to generate branch coverage code paths: |
| 16 | +class CoveredClass { |
| 17 | + constructor(x, y, opts) { |
| 18 | + this.x = x; |
| 19 | + this.y = y; |
| 20 | + // Exercise coverage of nullish coalescing: |
| 21 | + this.opts = opts ?? (Math.random() > 0.5 ? {} : undefined); |
| 22 | + } |
| 23 | + add() { |
| 24 | + return this.x + this.y; |
| 25 | + } |
| 26 | + addSpecial() { |
| 27 | + // Exercise coverage of optional chains: |
| 28 | + if (this.opts?.special && this.opts?.special?.x && this.opts?.special?.y) { |
| 29 | + return this.opts.special.x + this.opts.special.y; |
| 30 | + } |
| 31 | + return add(); |
| 32 | + } |
| 33 | + mult() { |
| 34 | + return this.x * this.y; |
| 35 | + } |
| 36 | + multSpecial() { |
| 37 | + if (this.opts?.special && this.opts?.special?.x && this.opts?.special?.y) { |
| 38 | + return this.opts.special.x * this.opts.special.y; |
| 39 | + } |
| 40 | + return mult(); |
| 41 | + } |
| 42 | +} |
| 43 | + |
| 44 | +// Excercise coverage of functions: |
| 45 | +function add(x, y) { |
| 46 | + const mt = new CoveredClass(x, y); |
| 47 | + return mt.add(); |
| 48 | +} |
| 49 | + |
| 50 | +function addSpecial(x, y) { |
| 51 | + let mt; |
| 52 | + if (Math.random() > 0.5) { |
| 53 | + mt = new CoveredClass(x, y); |
| 54 | + } else { |
| 55 | + mt = new CoveredClass(x, y, { |
| 56 | + special: { |
| 57 | + x: Math.random() * x, |
| 58 | + y: Math.random() * y |
| 59 | + } |
| 60 | + }); |
| 61 | + } |
| 62 | + return mt.addSpecial(); |
| 63 | +} |
| 64 | + |
| 65 | +function mult(x, y) { |
| 66 | + const mt = new CoveredClass(x, y); |
| 67 | + return mt.mult(); |
| 68 | +} |
| 69 | + |
| 70 | +function multSpecial(x, y) { |
| 71 | + let mt; |
| 72 | + if (Math.random() > 0.5) { |
| 73 | + mt = new CoveredClass(x, y); |
| 74 | + } else { |
| 75 | + mt = new CoveredClass(x, y, { |
| 76 | + special: { |
| 77 | + x: Math.random() * x, |
| 78 | + y: Math.random() * y |
| 79 | + } |
| 80 | + }); |
| 81 | + } |
| 82 | + return mt.multSpecial(); |
| 83 | +} |
| 84 | + |
| 85 | +function main({ n }) { |
| 86 | + bench.start(); |
| 87 | + for (let i = 0; i < n; i++) { |
| 88 | + const operations = ['add', 'addSpecial', 'mult', 'multSpecial']; |
| 89 | + for (const operation of operations) { |
| 90 | + // Exercise coverage of switch statements: |
| 91 | + switch (operation) { |
| 92 | + case 'add': |
| 93 | + if (add(Math.random() * 10, Math.random() * 10) > 10) { |
| 94 | + // Exercise coverage of ternary operations: |
| 95 | + let r = addSpecial(Math.random() * 10, Math.random() * 10) > 10 ? |
| 96 | + mult(Math.random() * 10, Math.random() * 10) : |
| 97 | + add(Math.random() * 10, Math.random() * 10); |
| 98 | + // Exercise && and || |
| 99 | + if (r && Math.random() > 0.5 || Math.random() < 0.5) r++; |
| 100 | + } |
| 101 | + break; |
| 102 | + case 'addSpecial': |
| 103 | + if (addSpecial(Math.random() * 10, Math.random() * 10) > 10 && |
| 104 | + add(Math.random() * 10, Math.random() * 10) > 10) { |
| 105 | + let r = mult(Math.random() * 10, Math.random() * 10) > 10 ? |
| 106 | + add(Math.random() * 10, Math.random() * 10) > 10 : |
| 107 | + mult(Math.random() * 10, Math.random() * 10); |
| 108 | + if (r && Math.random() > 0.5 || Math.random() < 0.5) r++; |
| 109 | + } |
| 110 | + break; |
| 111 | + case 'mult': |
| 112 | + if (mult(Math.random() * 10, Math.random() * 10) > 10) { |
| 113 | + let r = multSpecial(Math.random() * 10, Math.random() * 10) > 10 ? |
| 114 | + add(Math.random() * 10, Math.random() * 10) : |
| 115 | + mult(Math.random() * 10, Math.random() * 10); |
| 116 | + if (r && Math.random() > 0.5 || Math.random() < 0.5) r++; |
| 117 | + } |
| 118 | + break; |
| 119 | + case 'multSpecial': |
| 120 | + while (multSpecial(Math.random() * 10, Math.random() * 10) < 10) { |
| 121 | + mult(Math.random() * 10, Math.random() * 10); |
| 122 | + } |
| 123 | + break; |
| 124 | + default: |
| 125 | + break; |
| 126 | + } |
| 127 | + } |
| 128 | + } |
| 129 | + bench.end(n); |
| 130 | +} |
0 commit comments