Javascript 使用benchmark.js对各种node.js解压库进行基准测试
我正在尝试使用Javascript 使用benchmark.js对各种node.js解压库进行基准测试,javascript,node.js,asynchronous,benchmarking,Javascript,Node.js,Asynchronous,Benchmarking,我正在尝试使用benchmark.js为一些流行的node.js解压库编写一个基准,主要是adm-zip、yauzl&unzip。但是,我不确定我是否正确编写了基准代码,因为对于异步解压的libs,我不断收到Error:UNKNOWN,open'file-.zip'错误 我创建了一个示例zip文件file-.zip的3个副本,每个库对应一个副本 以下是代码: "use strict"; var fs = require("fs"); var Benchmark = require("benc
benchmark.js
为一些流行的node.js解压库编写一个基准,主要是adm-zip、yauzl&unzip
。但是,我不确定我是否正确编写了基准代码,因为对于异步解压的libs,我不断收到Error:UNKNOWN,open'file-.zip'
错误
我创建了一个示例zip文件file-.zip
的3个副本,每个库对应一个副本
以下是代码:
"use strict";
var fs = require("fs");
var Benchmark = require("benchmark");
var AdmZip = require("adm-zip"),
yauzl = require("yauzl"),
unzip = require("unzip");
var suite = new Benchmark.Suite;
var entryFile = "file.xml",
targetDir = "./unzipped/";
suite
.add("Adm-Zip#extractEntryTo", function() {
var zip = new AdmZip("./file-adm-zip.zip");
zip.extractEntryTo(entryFile, targetDir + "adm-zip/", /*maintainEntryPath*/false, /*overwrite*/true);
})
.add("YAUZL#open", function() {
yauzl.open("./file-yauzl.zip", function(err, zip) {
if (err) throw err;
zip.on("entry", function(entry) {
if (entryFile === (entry.fileName)) {
zip.openReadStream(entry, function(err, readStream) {
if (err) throw err;
// ensure parent directory exists, and then:
readStream.pipe(fs.createWriteStream(targetDir + "yauzl/" + entry.fileName));
});
}
});
zip.once("end", function() {
console.log("[YAUZL] Closing zip");
zip.close();
});
});
})
.add("UNZIP#Parse", function() {
fs.createReadStream("./file-unzip.zip")
.pipe(unzip.Parse())
.on("entry", function (entry) {
var fileName = entry.path;
if (fileName === entryFile) {
entry.pipe(fs.createWriteStream(targetDir + "unzip/" + fileName));
} else {
entry.autodrain();
}
})
.on("close", function() {
console.log("[UNZIP] Closing zip");
});
})
// add listeners
.on("cycle", function(event) {
console.log(String(event.target));
})
.on("complete", function() {
console.log("Fastest is " + this.filter("fastest").pluck("name"));
})
// run async
.run({ "async": true });
$ node benchmark-unzippers.js
Adm-Zip#extractEntryTo x 2.56 ops/sec ±1.62% (11 runs sampled)
[YAUZL] Closing zip
[YAUZL] Closing zip
[YAUZL] Closing zip
[YAUZL] Closing zip
~/benchmark-unzippers.js:23
if (err) throw err;
^
Error: UNKNOWN, open 'file-yauzl.zip'
它失败是因为溪流没有被正确关闭吗?我不完全确定情况是否如此,因为我确实看到,例如,每次运行YAUZL
测试时都会显示[YAUZL]关闭zip
消息
下面是一个运行示例:
"use strict";
var fs = require("fs");
var Benchmark = require("benchmark");
var AdmZip = require("adm-zip"),
yauzl = require("yauzl"),
unzip = require("unzip");
var suite = new Benchmark.Suite;
var entryFile = "file.xml",
targetDir = "./unzipped/";
suite
.add("Adm-Zip#extractEntryTo", function() {
var zip = new AdmZip("./file-adm-zip.zip");
zip.extractEntryTo(entryFile, targetDir + "adm-zip/", /*maintainEntryPath*/false, /*overwrite*/true);
})
.add("YAUZL#open", function() {
yauzl.open("./file-yauzl.zip", function(err, zip) {
if (err) throw err;
zip.on("entry", function(entry) {
if (entryFile === (entry.fileName)) {
zip.openReadStream(entry, function(err, readStream) {
if (err) throw err;
// ensure parent directory exists, and then:
readStream.pipe(fs.createWriteStream(targetDir + "yauzl/" + entry.fileName));
});
}
});
zip.once("end", function() {
console.log("[YAUZL] Closing zip");
zip.close();
});
});
})
.add("UNZIP#Parse", function() {
fs.createReadStream("./file-unzip.zip")
.pipe(unzip.Parse())
.on("entry", function (entry) {
var fileName = entry.path;
if (fileName === entryFile) {
entry.pipe(fs.createWriteStream(targetDir + "unzip/" + fileName));
} else {
entry.autodrain();
}
})
.on("close", function() {
console.log("[UNZIP] Closing zip");
});
})
// add listeners
.on("cycle", function(event) {
console.log(String(event.target));
})
.on("complete", function() {
console.log("Fastest is " + this.filter("fastest").pluck("name"));
})
// run async
.run({ "async": true });
$ node benchmark-unzippers.js
Adm-Zip#extractEntryTo x 2.56 ops/sec ±1.62% (11 runs sampled)
[YAUZL] Closing zip
[YAUZL] Closing zip
[YAUZL] Closing zip
[YAUZL] Closing zip
~/benchmark-unzippers.js:23
if (err) throw err;
^
Error: UNKNOWN, open 'file-yauzl.zip'
不完全确定这里发生了什么。我在typescript中遇到了类似的问题,并意识到我正在写入的
文件流在我调用yauzl
之前没有关闭
因此,在调用yauzl
之前,我等待filestream
上的close
事件
您可能需要在filestream
上为close
事件尝试C#等效程序