Skip to content

Commit

Permalink
Make webpack benchmark synchronous
Browse files Browse the repository at this point in the history
  • Loading branch information
jhnns committed Oct 25, 2017
1 parent 07c1c4d commit f672f33
Show file tree
Hide file tree
Showing 8 changed files with 140 additions and 21 deletions.
10 changes: 8 additions & 2 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
// Copyright 2017 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

const path = require("path");

module.exports = {
watchPathIgnorePatterns: [
rootDir: path.resolve(__dirname, "src"),
transformIgnorePatterns: [
"/node_modules/",
path.resolve(__dirname, "build")
]
}
};
9 changes: 9 additions & 0 deletions src/mocks/clear-immediate.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// Copyright 2017 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

const eventLoop = require("./event-loop");

module.exports = task => {
eventLoop.cancel(task);
};
27 changes: 27 additions & 0 deletions src/mocks/event-loop.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// Copyright 2017 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

// This is a very simple event loop implementation. It does not cover the exact behavior
// in node, especially not the differences between process.nextTick and setImmediate.
// It is, however, sufficient for the benchmark.
const tasks = [];

module.exports = {
schedule(task) {
tasks.push(task);
},
cancel(task) {
const i = tasks.indexOf(task);

if (i > -1) {
tasks.splice(i, 1);
}
},
run() {
let task;
while ((task = tasks.shift())) {
task();
}
}
};
24 changes: 24 additions & 0 deletions src/mocks/process.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// Copyright 2017 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

const eventLoop = require("./event-loop");

exports.nextTick = function(fn) {
const args = Array.prototype.slice.call(arguments, 1);
eventLoop.schedule(() => {
fn.apply(null, args);
});
};

exports.platform = exports.arch = exports.execPath = exports.title = "browser";
exports.pid = 1;
exports.browser = true;
exports.env = {};
exports.argv = [];
exports.cwd = () => "/";
exports.binding = name => {
throw new Error("No such module. (Possibly not yet loaded)");
};
exports.exit = exports.kill = exports.umask = exports.dlopen = exports.uptime = exports.memoryUsage = exports.uvCounters = () => {};
exports.features = {};
14 changes: 14 additions & 0 deletions src/mocks/set-immediate.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// Copyright 2017 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

const eventLoop = require("./event-loop");

module.exports = function(fn) {
const args = Array.prototype.slice.call(arguments, 1);
const task = () => {
fn.apply(null, args);
};
eventLoop.schedule(task);
return task;
};
34 changes: 23 additions & 11 deletions src/webpack-benchmark.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// found in the LICENSE file.

const webpack = require("webpack");
const eventLoop = require("./mocks/event-loop");

const payloads = [
{
Expand All @@ -22,16 +23,27 @@ const payloads = [

module.exports = {
name: "webpack",
defer: true,
fn(deferred) {
return Promise.all(
payloads.map(
config =>
new Promise((resolve, reject) => {
const compiler = webpack(config);
compiler.run((err, stats) => void (err ? reject(err) : resolve()));
})
)
).then(() => deferred.resolve());
fn() {
payloads.forEach(config => {
let finished = false;

eventLoop.schedule(() => {
const compiler = webpack(config);
compiler.run((err, stats) => {
if (err) {
throw err;
}
if (stats.hasErrors()) {
throw stats.compilation.errors[0];
}
finished = true;
});
});
eventLoop.run();

if (finished !== true) {
throw new Error("Webpack did not finish synchronously");
}
});
}
};
8 changes: 2 additions & 6 deletions src/webpack-benchmark.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@ beforeAll(
new Promise((resolve, reject) => {
const baseConfig = webpackConfig[0];
const config = Object.assign({}, baseConfig, {
entry: require.resolve("./webpack-benchmark.js"),
bail: true
entry: require.resolve("./webpack-benchmark.js")
});
config.output = Object.assign({}, baseConfig.output, {
libraryTarget: "commonjs2",
Expand All @@ -39,7 +38,4 @@ beforeAll(
})
);

it("webpack runs to completion", () =>
new Promise(resolve => {
webpackBenchmark.fn({ resolve });
}));
it("webpack runs to completion", () => void webpackBenchmark.fn());
35 changes: 33 additions & 2 deletions webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,26 @@ module.exports = [
"graceful-fs": require.resolve("./src/vfs"),
module: require.resolve("./src/mocks/dummy"),
chokidar: require.resolve("./src/mocks/chokidar"),
"uglify-js": require.resolve("./src/mocks/dummy")
"uglify-js": require.resolve("./src/mocks/dummy"),
// These modules are used by virtualfs to fake async fs calls
"core-js/library/fn/set-immediate": require.resolve(
"./src/mocks/set-immediate"
),
"core-js/library/fn/clear-immediate": require.resolve(
"./src/mocks/clear-immediate"
)
}
},
node: {
setImmediate: false, // this disables also clearImmediate
process: false
},
plugins: [
new webpack.ProvidePlugin({
setImmediate: require.resolve("./src/mocks/set-immediate"),
clearImmediate: require.resolve("./src/mocks/clear-immediate"),
process: require.resolve("./src/mocks/process")
}),
new webpack.BannerPlugin({
banner:
"// Required for JavaScript engine shells.\n" +
Expand All @@ -52,10 +68,25 @@ module.exports = [
"graceful-fs": require.resolve("./src/vfs"),
module: require.resolve("./src/mocks/dummy"),
chokidar: require.resolve("./src/mocks/chokidar"),
"uglify-js": require.resolve("./src/mocks/dummy")
"uglify-js": require.resolve("./src/mocks/dummy"),
"core-js/library/fn/set-immediate": require.resolve(
"./src/mocks/set-immediate"
),
"core-js/library/fn/clear-immediate": require.resolve(
"./src/mocks/clear-immediate"
)
}
},
node: {
setImmediate: false,
process: false
},
plugins: [
new webpack.ProvidePlugin({
setImmediate: require.resolve("./src/mocks/set-immediate"),
clearImmediate: require.resolve("./src/mocks/clear-immediate"),
process: require.resolve("./src/mocks/process")
}),
new CopyWebpackPlugin([{ from: "style.css" }, { from: "Logo.png" }]),
new webpack.BannerPlugin({
banner:
Expand Down

0 comments on commit f672f33

Please sign in to comment.