我正在尝试进行一些基本的基准测试,但无法确定正确的配置。我正在尝试使用 Benchmarkjs 与 webpack 和 babel 将我的代码转换为 es5。我创建了一个 benchmarks.webpack.js 作为入口点,如下所示:
var context = require.context('./src/js', true, /-benchmark\.js$/);
context.keys().forEach(context);
module.exports = context;
然后我有一个想要运行的基准测试文件 (test-benchmark.js):
import benchmark from 'benchmark';
import benchmarks from 'beautify-benchmark';
let suite = new benchmark.Suite;
suite.add('RegExp#test', function() {
/o/.test('Hello World!');
})
.add('String#indexOf', function() {
'Hello World!'.indexOf('o') > -1;
})
.on('cycle', function(event) {
benchmarks.add(event.target);
})
.on('complete', function() {
benchmarks.log();
})
.run();
我更新了我的 webpack 版本来尝试转换基准:
_.assign(config, {
devtool: 'eval-cheap-module-source-map',
output: {
path: path.join(__dirname, 'build/benchmark'),
filename: 'benchmark.js',
publicPath: '/'
},
entry: [
'./benchmarks.webpack.js'
],
plugins: [
],
module: {
loaders: [
{
test: /\.js$/,
loaders: ['babel?stage=0'],
include: path.join(__dirname, 'src/js')
},
]
},
});
最后,我希望能够从 npm 脚本运行它:
"scripts": {
"bench": "webpack --config webpack.bench.config.js && node build/benchmark/benchmark.js"
},
但是,我收到警告,指出基准测试依赖项的结果是一个表达式,并且没有适合 .json、.txt 等文件的加载器。我尝试破解 Benchmarkjs 以正确导出,但没有成功。
WARNING in ./~/benchmark/benchmark.js
Critical dependencies:
1122:34-49 the request of a dependency is an expression
@ ./~/benchmark/benchmark.js 1122:34-49
WARNING in ./~/benchmark/package.json
Module parse failed: /home/bill/dev/levelstory/react-client-redux/node_modules/benchmark/package.json Line 2: Unexpected token :
You may need an appropriate loader to handle this file type.
| {
| "name": "benchmark",
| "version": "1.0.0",
| "description": "A benchmarking library that works on nearly all JavaScript platforms, supports high-resolution timers, and returns statistically significant results.",
@ ./~/benchmark ^\.\/.*$
WARNING in ./~/benchmark/LICENSE.txt
Module parse failed: /home/bill/dev/levelstory/react-client-redux/node_modules/benchmark/LICENSE.txt Line 1: Unexpected number
You may need an appropriate loader to handle this file type.
| Copyright 2010-2012 Mathias Bynens <http://mathiasbynens.be/>
| Based on JSLitmus.js, copyright Robert Kieffer <http://broofa.com/>
| Modified by John-David Dalton <http://allyoucanleet.com/>
@ ./~/benchmark ^\.\/.*$
最佳答案
看起来基准对require
做了一些特殊的事情。这让 Webpack 变得一团糟。它有以下几行:
var freeRequire = typeof require == 'function' && require;
...
function req(id) {
try {
var result = freeExports && freeRequire(id);
} catch(e) { }
return result || null;
}
如果注释掉函数内容,错误就会消失。鉴于以这种方式修补它并不理想,我会直接向基准测试人员询问这一点。也许我们遗漏了一些东西。
关于node.js - 将 Benchmarkjs 与 Webpack 和 Babel 结合使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32242072/