Skip to content

Commit 829fa7a

Browse files
committed
jest: fix running benchmarks
1 parent 3e7c139 commit 829fa7a

2 files changed

Lines changed: 98 additions & 85 deletions

File tree

‎package.json‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,8 +107,8 @@
107107
"fix": "npm run lint -- --fix",
108108
"pretty-files": "prettier \"lib/**.*\" \"bin/**.*\" \"hot/**.*\" \"buildin/**.*\" \"test/*.js\" \"test/**/webpack.config.js\" \"examples/**/webpack.config.js\" --write",
109109
"schema-lint": "node --no-deprecation --max-old-space-size=4096 --harmony node_modules/.bin/jest --testMatch '<rootDir>/test/*.lint.js'",
110-
"benchmark": "node --no-deprecation --max-old-space-size=4096 --harmony node_modules/.bin/jest --testMatch '<rootDir>/test/*.benchmark.js'",
111110
"cover": "npm run cover:init && npm run cover:all && npm run cover:report",
111+
"benchmark": "node --no-deprecation --max-old-space-size=4096 --harmony node_modules/.bin/jest --testMatch '<rootDir>/test/*.benchmark.js' --runInBand",
112112
"cover:init": "rimraf coverage",
113113
"cover:all": "node --no-deprecation --max-old-space-size=4096 --harmony node_modules/.bin/jest --coverage",
114114
"cover:integration": "node --no-deprecation --max-old-space-size=4096 --harmony node_modules/.bin/jest --testMatch '<rootDir>/test/*.test.js' --coverage",
@@ -138,6 +138,7 @@
138138
"coverageDirectory": "<rootDir>/coverage",
139139
"coveragePathIgnorePatterns": [
140140
"\\.runtime\\.js$"
141-
]
141+
],
142+
"testEnvironment": "node"
142143
}
143144
}

‎test/BenchmarkTestCases.benchmark.js‎

Lines changed: 95 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
const path = require("path");
44
const fs = require("fs");
55
const asyncLib = require("neo-async");
6-
76
const Benchmark = require("benchmark");
7+
const {remove} = require("./helpers/remove");
88

99
describe("BenchmarkTestCases", function() {
1010
const casesPath = path.join(__dirname, "benchmarkCases");
@@ -62,7 +62,11 @@ describe("BenchmarkTestCases", function() {
6262
err => {
6363
if (err) return callback(err);
6464
fs.writeFileSync(gitIndex, index);
65-
doLoadWebpack();
65+
try {
66+
doLoadWebpack();
67+
} catch (err) {
68+
callback(err);
69+
}
6670
}
6771
);
6872
}
@@ -72,7 +76,7 @@ describe("BenchmarkTestCases", function() {
7276
}
7377

7478
function doLoadWebpack() {
75-
const baselineWebpack = require(path.resolve(
79+
const baselineWebpack = require.requireActual(path.resolve(
7680
baselinePath,
7781
"lib/webpack.js"
7882
));
@@ -84,14 +88,24 @@ describe("BenchmarkTestCases", function() {
8488
callback();
8589
}
8690
},
87-
done
91+
(err) => {
92+
if (err) {
93+
done(err);
94+
}
95+
createTests();
96+
done();
97+
}
8898
);
8999
});
90100
}, 270000);
91101

102+
afterAll(() => {
103+
remove(baselinesPath);
104+
})
105+
92106
function getBaselineRevs(rootPath, callback) {
93107
const git = require("simple-git")(rootPath);
94-
const lastVersionTag = "v" + require("../package.json").version;
108+
const lastVersionTag = "v" + require.requireActual("../package.json").version;
95109
git.raw(["rev-list", "-n", "1", lastVersionTag], (err, resultVersion) => {
96110
if (err) return callback(err);
97111
const matchVersion = /^([a-f0-9]+)\s*$/.exec(resultVersion);
@@ -258,86 +272,84 @@ describe("BenchmarkTestCases", function() {
258272
});
259273
}
260274

261-
tests.forEach(testName => {
262-
const testDirectory = path.join(casesPath, testName);
263-
let headStats = null;
264-
describe(`${testName} create benchmarks`, function() {
265-
baselines.forEach(baseline => {
266-
let baselineStats = null;
267-
it(
268-
`should benchmark ${baseline.name} (${baseline.rev})`,
269-
function(done) {
270-
const outputDirectory = path.join(
271-
__dirname,
272-
"js",
273-
"benchmark",
274-
`baseline-${baseline.name}`,
275-
testName
276-
);
277-
const config = Object.create(
278-
require(path.join(testDirectory, "webpack.config.js"))
279-
);
280-
config.output = Object.create(config.output || {});
281-
if (!config.context) config.context = testDirectory;
282-
if (!config.output.path) config.output.path = outputDirectory;
283-
runBenchmark(baseline.webpack, config, (err, stats) => {
284-
if (err) return done(err);
285-
console.log(` ${baseline.name} ${stats.text}`);
286-
if (baseline.name === "HEAD") headStats = stats;
287-
else baselineStats = stats;
288-
done();
289-
});
290-
},
291-
180000
292-
);
293-
294-
it(
295-
`should benchmark ${baseline.name} (${baseline.rev})`,
296-
done => {
297-
const outputDirectory = path.join(
298-
__dirname,
299-
"js",
300-
"benchmark",
301-
`baseline-${baseline.name}`,
302-
testName
303-
);
304-
const config = Object.create(
305-
require(path.join(testDirectory, "webpack.config.js"))
306-
);
307-
config.output = Object.create(config.output || {});
308-
if (!config.context) config.context = testDirectory;
309-
if (!config.output.path) config.output.path = outputDirectory;
310-
runBenchmark(baseline.webpack, config, (err, stats) => {
311-
if (err) return done(err);
312-
console.log(` ${baseline.name} ${stats.text}`);
313-
if (baseline.name === "HEAD") headStats = stats;
314-
else baselineStats = stats;
315-
done();
316-
});
317-
},
318-
180000
319-
);
320-
321-
if (baseline.name !== "HEAD") {
322-
it(`HEAD should not be slower than ${baseline.name} (${
323-
baseline.rev
324-
})`, function() {
325-
if (baselineStats.maxConfidence < headStats.minConfidence) {
326-
throw new Error(
327-
`HEAD (${headStats.text}) is slower than ${baseline.name} (${
328-
baselineStats.text
329-
}) (90% confidence)`
275+
function createTests() {
276+
tests.forEach(testName => {
277+
const testDirectory = path.join(casesPath, testName);
278+
let headStats = null;
279+
describe(`${testName} create benchmarks`, function() {
280+
baselines.forEach(baseline => {
281+
let baselineStats = null;
282+
it(
283+
`should benchmark ${baseline.name} (${baseline.rev})`,
284+
function(done) {
285+
const outputDirectory = path.join(
286+
__dirname,
287+
"js",
288+
"benchmark",
289+
`baseline-${baseline.name}`,
290+
testName
330291
);
331-
} else if (baselineStats.minConfidence > headStats.maxConfidence) {
332-
console.log(
333-
`======> HEAD is ${Math.round(
334-
baselineStats.mean / headStats.mean * 100 - 100
335-
)}% faster than ${baseline.name} (90% confidence)!`
292+
const config = require.requireActual(path.join(testDirectory, "webpack.config.js")) || {}
293+
config.output = config.output || {};
294+
if (!config.context) config.context = testDirectory;
295+
if (!config.output.path) config.output.path = outputDirectory;
296+
runBenchmark(baseline.webpack, config, (err, stats) => {
297+
if (err) return done(err);
298+
console.log(` ${baseline.name} ${stats.text}`);
299+
if (baseline.name === "HEAD") headStats = stats;
300+
else baselineStats = stats;
301+
done();
302+
});
303+
},
304+
180000
305+
);
306+
307+
it(
308+
`should benchmark ${baseline.name} (${baseline.rev})`,
309+
done => {
310+
const outputDirectory = path.join(
311+
__dirname,
312+
"js",
313+
"benchmark",
314+
`baseline-${baseline.name}`,
315+
testName
336316
);
337-
}
338-
});
339-
}
317+
const config = require.requireActual(path.join(testDirectory, "webpack.config.js")) || {};
318+
config.output = config.output || {};
319+
if (!config.context) config.context = testDirectory;
320+
if (!config.output.path) config.output.path = outputDirectory;
321+
runBenchmark(baseline.webpack, config, (err, stats) => {
322+
if (err) return done(err);
323+
console.log(` ${baseline.name} ${stats.text}`);
324+
if (baseline.name === "HEAD") headStats = stats;
325+
else baselineStats = stats;
326+
done();
327+
});
328+
},
329+
180000
330+
);
331+
332+
if (baseline.name !== "HEAD") {
333+
it(`HEAD should not be slower than ${baseline.name} (${
334+
baseline.rev
335+
})`, function() {
336+
if (baselineStats.maxConfidence < headStats.minConfidence) {
337+
throw new Error(
338+
`HEAD (${headStats.text}) is slower than ${baseline.name} (${
339+
baselineStats.text
340+
}) (90% confidence)`
341+
);
342+
} else if (baselineStats.minConfidence > headStats.maxConfidence) {
343+
console.log(
344+
`======> HEAD is ${Math.round(
345+
baselineStats.mean / headStats.mean * 100 - 100
346+
)}% faster than ${baseline.name} (90% confidence)!`
347+
);
348+
}
349+
});
350+
}
351+
});
340352
});
341353
});
342-
});
354+
}
343355
});

0 commit comments

Comments
 (0)