Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add webpack benchmark #8

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +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 = {
rootDir: path.resolve(__dirname, "src"),
transformIgnorePatterns: [
"/node_modules/",
path.resolve(__dirname, "build")
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is necessary because the webpack test needs to be built with webpack first. This watch ignore pattern fixes an infinite loop issue.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you do this as a separate PR? Just adding the jest.config.js file. This seems useful on it's own.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You don't need to ignore that path without the webpack test because other tests are not writing any files. Personally, I wouldn't add that configuration when it's not necessary. What do you think?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point.

]
};
5 changes: 5 additions & 0 deletions src/fixtures/webpack/a.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import b from "./b.js";

const message = import("./c.js").then(c => c.default + b);

export default message;
1 change: 1 addition & 0 deletions src/fixtures/webpack/b.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = 42;
1 change: 1 addition & 0 deletions src/fixtures/webpack/c.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default "Hello world";
3 changes: 3 additions & 0 deletions src/mocks/chokidar/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = {
watch: () => {}
};
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;
};
1 change: 1 addition & 0 deletions src/suite.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,6 @@ suite.add(require("./source-map-benchmark"));
suite.add(require("./typescript-benchmark"));
suite.add(require("./uglify-js-benchmark"));
suite.add(require("./uglify-es-benchmark"));
suite.add(require("./webpack-benchmark"));

module.exports = suite;
13 changes: 13 additions & 0 deletions src/vfs.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,5 +79,18 @@ fs.writeFileSync(
"third_party/vue.runtime.esm-nobuble-2.4.4.js",
require("raw-loader!../third_party/vue.runtime.esm-nobuble-2.4.4.js")
);
fs.mkdirpSync("/src/fixtures/webpack");
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Webpack requires absolute file paths.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How does that work when you run it via node? I.e.

$ node src/cli

fs.writeFileSync(
"/src/fixtures/webpack/a.js",
require("raw-loader!./fixtures/webpack/a.js")
);
fs.writeFileSync(
"/src/fixtures/webpack/b.js",
require("raw-loader!./fixtures/webpack/b.js")
);
fs.writeFileSync(
"/src/fixtures/webpack/c.js",
require("raw-loader!./fixtures/webpack/c.js")
);

module.exports = fs;
49 changes: 49 additions & 0 deletions src/webpack-benchmark.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// 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 webpack = require("webpack");
const eventLoop = require("./mocks/event-loop");

const payloads = [
{
entry: "/src/fixtures/webpack/a.js"
}
].map(({ entry }, i) => ({
entry,
output: {
path: "/dist/webpack/",
filename: `bundle.${i}.js`
},
// Using bail option in order to receive noisy errors in the benchmark if something went wrong
bail: true,
// We need to define that because Firefox has a Object.prototype.watch function
watch: false
}));

module.exports = {
name: "webpack",
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");
Copy link
Contributor Author

@jhnns jhnns Oct 25, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just to make sure 😁

}
});
}
};
41 changes: 41 additions & 0 deletions src/webpack-benchmark.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// 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");
const webpack = require("webpack");
const webpackConfig = require("../webpack.config.js");

const outputFile = path.resolve(
__dirname,
"..",
"build",
"webpack-benchmark.test.js"
);
let webpackBenchmark;

beforeAll(
() =>
new Promise((resolve, reject) => {
const baseConfig = webpackConfig[0];
const config = Object.assign({}, baseConfig, {
entry: require.resolve("./webpack-benchmark.js")
});
config.output = Object.assign({}, baseConfig.output, {
libraryTarget: "commonjs2",
path: path.dirname(outputFile),
filename: path.basename(outputFile)
});
const compiler = webpack(config);
compiler.run(err => {
if (err) {
reject(err);
return;
}
webpackBenchmark = require(outputFile);
resolve();
});
})
);

it("webpack runs to completion", () => void webpackBenchmark.fn());
41 changes: 39 additions & 2 deletions webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,29 @@ module.exports = [
resolve: {
alias: {
fs: require.resolve("./src/vfs"),
module: require.resolve("./src/mocks/dummy")
"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"),
// These modules are used by virtualfs to fake async fs calls
"core-js/library/fn/set-immediate": require.resolve(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How does that work with running the benchmark suite via node?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See below

"./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 @@ -46,10 +65,28 @@ module.exports = [
alias: {
define: require.resolve("./src/mocks/dummy"),
fs: require.resolve("./src/vfs"),
module: require.resolve("./src/mocks/dummy")
"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"),
"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