Skip to content

Instantly share code, notes, and snippets.

@dgrelaud
Last active December 26, 2015 21:49
Show Gist options
  • Select an option

  • Save dgrelaud/7218818 to your computer and use it in GitHub Desktop.

Select an option

Save dgrelaud/7218818 to your computer and use it in GitHub Desktop.
Rapid performance test for zipfile
var path = require('path');
var fs = require('fs');
var ZIP = require('zip');
var zipfile = require('zipfile');
var filePath1 = path.resolve('./zipfile1.zip');
var filePath2 = path.resolve('./zipfile2.zip');
var nbExecuted = 14;
/***********************************************************************************/
/************************************ ZIP ******************************************/
/***********************************************************************************/
function unzip_with_zip(filePath, callback){
var unzippedFiles = [];
fs.readFile(filePath, function(err, buffer){
var reader = ZIP.Reader(buffer);
var filesObj = reader.toObject();
for(var filename in filesObj){
unzippedFiles.push({
'name':filename,
'buffer':filesObj[filename]
});
}
callback(err, unzippedFiles);
});
}
/**** Test perf ***/
function test_zip(){
var nbLeft = nbExecuted;
var start = new Date();
for (var i = 0; i < nbExecuted; i++) {
var filePath = (i % 3 === 0 ) ? filePath1 : filePath2; //do not unzip always the same file
unzip_with_zip(filePath, function(err, files){
nbLeft--;
if(nbLeft === 0){
theEnd();
}
});
};
function theEnd(){
var end = new Date();
var elapsed = (end.getTime() - start.getTime())/nbExecuted; // time in milliseconds
console.log('Zip - Time Elapsed : '+elapsed + ' ms per file for '+nbExecuted+' unzip tasks');
}
}
/***********************************************************************************/
/******************************** ZIPFILE ******************************************/
/***********************************************************************************/
function unzip_with_zipfile(filePath, callback){
var zippedFile = new zipfile.ZipFile(filePath);
var nbFiles = zippedFile.names.length;
var nbUnzipped = 0;
var unzippedFiles = [];
for (var i = 0; i < nbFiles; i++) {
var filename = zippedFile.names[i];
unzipNextFile(filename);
}
function unzipNextFile(filename){
//var buffer = zippedFile.readFileSync(filename); it works with the sync version of the api
zippedFile.readFile(filename, function(err, buffer){
unzippedFiles.push({
'name' : filename,
'buffer' : buffer
});
nbUnzipped++;
if(nbUnzipped >= nbFiles){
callback(err, unzippedFiles);
}
});
}
}
/**** Test perf ***/
function test_zipfile(){
var nbLeft = nbExecuted;
var start = new Date();
for (var i = 0; i < nbExecuted; i++) {
var filePath = (i % 3 === 0 ) ? filePath1 : filePath2; //do not unzip always the same file
unzip_with_zipfile(filePath, function(err, files){
nbLeft--;
if(nbLeft === 0){
theEnd();
}
});
};
function theEnd(){
var end = new Date();
var elapsed = (end.getTime() - start.getTime())/nbExecuted; // time in milliseconds
console.log('ZIPFILE - Time Elapsed : '+elapsed + ' ms per file for '+nbExecuted+' unzip tasks');
}
}
test_zip();
test_zipfile();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment