I'm trying to write a benchmark, using benchmark.js
, for some popular node.js unzip libs, mainly adm-zip, yauzl & unzip
. However, I'm not sure if I've written the benchmark code correctly, because I keep getting Error: UNKNOWN, open 'file-<unzip-lib>.zip'
errors for the libs that unzip asynchronously.
I created 3 copies of a sample zip file file-<unzip-lib>.zip
, one for each library to work on.
Here's the code:
"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 });
Is it failing because the streams haven't been closed properly? I'm not entirely sure this is the case, because I do see, for example, the [YAUZL] Closing zip
message being displayed each time the yauzl
test is run.
Here's a sample run:
$ 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'
Not entirely sure what's going on here.