我试图用benchmark.js为一些流行的node.js解压缩库(主要是adm-zip, yauzl & unzip )编写一个基准测试。但是,我不确定我是否正确地编写了基准代码,因为我一直为异步解压缩的库获取Error: UNKNOWN, open 'file-<unzip-lib>.zip'错误。
我创建了一个示例zip文件file-<unzip-lib>.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 });是因为溪流没有被正确关闭而失败吗?我不完全确定情况是否如此,因为我确实看到,例如,每次运行[YAUZL] Closing zip测试时都会显示yauzl消息。
,这里有一个运行的示例
$ 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'不完全确定这是怎么回事。
发布于 2018-04-19 00:00:49
我在打字稿中遇到了类似的问题,并意识到在我打电话给filestream之前,我给它写信的那个yauzl并没有被关闭。
因此,在调用close之前,我一直在等待filestream上的yauzl事件。
您可能需要在C#上尝试close事件的等效filestream。
https://stackoverflow.com/questions/30113413
复制相似问题