|
1 | 1 | "use strict"; |
2 | | -const inv = require("install-npm-version"); |
3 | | -const benchmarkVersion = "1.0.1"; |
| 2 | + |
| 3 | +const benchmarkVersion = [...process.argv].pop(); |
| 4 | + |
| 5 | +const noVersionMessage = `Aborted benchmark |
| 6 | +
|
| 7 | +Please specify a bench version. E.g: npm run benchmark -- 2.0.0 |
| 8 | +`; |
| 9 | + |
| 10 | +if (benchmarkVersion.match(/\d+\.\d+\.\d+/) === null) { |
| 11 | + console.log(noVersionMessage); |
| 12 | + return process.exit(1); |
| 13 | +} |
| 14 | + |
4 | 15 | const benchmarkLib = `postcode@${benchmarkVersion}`; |
5 | 16 |
|
6 | 17 | const message = ` |
7 | | - Benchmarking master against ${benchmarkLib} |
8 | | -
|
| 18 | + Benchmarking master against postcode@${benchmarkLib} |
9 | 19 | `; |
10 | 20 |
|
11 | | -inv |
| 21 | +console.log(message); |
| 22 | + |
| 23 | +require("install-npm-version") |
12 | 24 | .Install(benchmarkLib, { Verbosity: "debug" }) |
13 | 25 | .then(() => runBenchmarks()) |
14 | 26 | .catch(error => { |
|
18 | 30 |
|
19 | 31 | const runBenchmarks = () => { |
20 | 32 | const { Suite } = require("benchmark"); |
21 | | - const suite = new Suite(); |
| 33 | + const newSuite = name => new Suite({ name }); |
22 | 34 |
|
23 | 35 | const Postcode = require("./dist/index"); |
24 | 36 | const PreviousPostcode = require(benchmarkLib); |
25 | 37 |
|
26 | 38 | const extractInputs = ({ tests }) => tests.map(({ base }) => base); |
27 | 39 |
|
28 | 40 | const validation = extractInputs(require("./test/data/validation.json")); |
29 | | - suite |
30 | | - .add(`(version: ${benchmarkVersion}) Postcode#valid`, () => { |
31 | | - validation.forEach(postcode => { |
32 | | - const result = new PreviousPostcode(postcode).valid(); |
33 | | - }); |
34 | | - }) |
35 | | - .add("(version: master) Postcode.isValid", () => { |
36 | | - validation.forEach(postcode => { |
37 | | - const result = Postcode.isValid(postcode); |
38 | | - }); |
| 41 | + const areas = extractInputs(require("./test/data/areas.json")); |
| 42 | + const districts = extractInputs(require("./test/data/districts.json")); |
| 43 | + const incodes = extractInputs(require("./test/data/incodes.json")); |
| 44 | + const normalisation = extractInputs( |
| 45 | + require("./test/data/normalisation.json") |
| 46 | + ); |
| 47 | + const outcodes = extractInputs(require("./test/data/outcodes.json")); |
| 48 | + const sectors = extractInputs(require("./test/data/sectors.json")); |
| 49 | + const subDistricts = extractInputs(require("./test/data/sub-districts.json")); |
| 50 | + const units = extractInputs(require("./test/data/units.json")); |
| 51 | + |
| 52 | + newSuite(".isValid") |
| 53 | + .add(benchmarkVersion, () => { |
| 54 | + validation.forEach(postcode => PreviousPostcode.isValid(postcode)); |
| 55 | + }) |
| 56 | + .add("master", () => { |
| 57 | + validation.forEach(postcode => Postcode.isValid(postcode)); |
| 58 | + }) |
| 59 | + .on("cycle", event => { |
| 60 | + console.log(String(event.target)); |
| 61 | + }) |
| 62 | + .run({}); |
| 63 | + |
| 64 | + newSuite(".toArea") |
| 65 | + .add(benchmarkVersion, () => { |
| 66 | + areas.forEach(postcode => PreviousPostcode.toArea(postcode)); |
| 67 | + }) |
| 68 | + .add("master", () => { |
| 69 | + areas.forEach(postcode => Postcode.toArea(postcode)); |
| 70 | + }) |
| 71 | + .on("cycle", event => { |
| 72 | + console.log(String(event.target)); |
| 73 | + }) |
| 74 | + .run({}); |
| 75 | + |
| 76 | + newSuite(".toDistrict") |
| 77 | + .add(benchmarkVersion, () => { |
| 78 | + districts.forEach(postcode => PreviousPostcode.toDistrict(postcode)); |
| 79 | + }) |
| 80 | + .add("master", () => { |
| 81 | + districts.forEach(postcode => Postcode.toDistrict(postcode)); |
| 82 | + }) |
| 83 | + .on("cycle", event => { |
| 84 | + console.log(String(event.target)); |
| 85 | + }) |
| 86 | + .run({}); |
| 87 | + |
| 88 | + newSuite(".toNormalised") |
| 89 | + .add(benchmarkVersion, () => { |
| 90 | + incodes.forEach(postcode => PreviousPostcode.toIncode(postcode)); |
| 91 | + }) |
| 92 | + .add("master", () => { |
| 93 | + incodes.forEach(postcode => Postcode.toIncode(postcode)); |
| 94 | + }) |
| 95 | + .on("cycle", event => { |
| 96 | + console.log(String(event.target)); |
| 97 | + }) |
| 98 | + .run({}); |
| 99 | + |
| 100 | + newSuite(".toNormalised") |
| 101 | + .add(benchmarkVersion, () => { |
| 102 | + normalisation.forEach(postcode => |
| 103 | + PreviousPostcode.toNormalised(postcode) |
| 104 | + ); |
| 105 | + }) |
| 106 | + .add("master", () => { |
| 107 | + normalisation.forEach(postcode => Postcode.toNormalised(postcode)); |
| 108 | + }) |
| 109 | + .on("cycle", event => { |
| 110 | + console.log(String(event.target)); |
| 111 | + }) |
| 112 | + .run({}); |
| 113 | + |
| 114 | + newSuite(".toOutcode") |
| 115 | + .add(benchmarkVersion, () => { |
| 116 | + outcodes.forEach(postcode => PreviousPostcode.toOutcode(postcode)); |
| 117 | + }) |
| 118 | + .add("master", () => { |
| 119 | + outcodes.forEach(postcode => Postcode.toOutcode(postcode)); |
| 120 | + }) |
| 121 | + .on("cycle", event => { |
| 122 | + console.log(String(event.target)); |
| 123 | + }) |
| 124 | + .run({}); |
| 125 | + |
| 126 | + newSuite(".toSector") |
| 127 | + .add(benchmarkVersion, () => { |
| 128 | + sectors.forEach(postcode => PreviousPostcode.toSector(postcode)); |
| 129 | + }) |
| 130 | + .add("master", () => { |
| 131 | + sectors.forEach(postcode => Postcode.toSector(postcode)); |
| 132 | + }) |
| 133 | + .on("cycle", event => { |
| 134 | + console.log(String(event.target)); |
| 135 | + }) |
| 136 | + .run({}); |
| 137 | + |
| 138 | + newSuite("toSubDistrict") |
| 139 | + .add(benchmarkVersion, () => { |
| 140 | + subDistricts.forEach(postcode => |
| 141 | + PreviousPostcode.toSubDistrict(postcode) |
| 142 | + ); |
| 143 | + }) |
| 144 | + .add("master", () => { |
| 145 | + subDistricts.forEach(postcode => Postcode.toSubDistrict(postcode)); |
| 146 | + }) |
| 147 | + .on("cycle", event => { |
| 148 | + console.log(String(event.target)); |
| 149 | + }) |
| 150 | + .run({}); |
| 151 | + |
| 152 | + newSuite("toUnit") |
| 153 | + .add(benchmarkVersion, () => { |
| 154 | + units.forEach(postcode => PreviousPostcode.toUnit(postcode)); |
| 155 | + }) |
| 156 | + .add("master", () => { |
| 157 | + units.forEach(postcode => Postcode.toUnit(postcode)); |
39 | 158 | }) |
40 | 159 | .on("cycle", event => { |
41 | 160 | console.log(String(event.target)); |
|
0 commit comments