Skip to content

Instantly share code, notes, and snippets.

@aq1018
Created March 14, 2011 23:38
Show Gist options
  • Save aq1018/870085 to your computer and use it in GitHub Desktop.
Save aq1018/870085 to your computer and use it in GitHub Desktop.
Dynamic zip file archiving and streaming
#!/usr/bin/env node
var fs = require('fs'),
util = require('util'),
express = require('express'),
spawn = require('child_process').spawn,
app = express.createServer(
express.profiler(),
express.logger(),
express.bodyParser()
);
app.get('/', function(req, res){
var files = req.param('files'),
args = ['-r', '-'].concat(files),
zip = spawn('zip', args);
if(!files) {
res.send('Bad Request', { 'Content-Type': 'text/plain' }, 400);
} else {
zip.stdout.pipe(res);
zip.stderr.on('data', function(data) {
console.log("stderr" + data);
});
zip.on('exit', function (code) {
console.log("zip process exited with " + code);
// I guess I don't need end()
// since we are piping from zip process, it will end() anyway
//res.end();
});
}
});
app.listen(3000);
@aq1018
Copy link
Author

aq1018 commented Mar 15, 2011

Running apache bench on a MacBook Pro. ( 2.53 GHz i5, 4 GB memory )

Server Software:        
Server Hostname:        127.0.0.1
Server Port:            3000

Document Path:          /?files[]=/Users/aqian/test.jpg&files[]=/Users/aqian/test2.jpg
Document Length:        870 bytes

Concurrency Level:      300
Time taken for tests:   6.307 seconds
Complete requests:      1000
Failed requests:        231
   (Connect: 9, Receive: 0, Length: 222, Exceptions: 0)
Write errors:           0
Non-2xx responses:      778
Total transferred:      165285082 bytes
HTML transferred:       165189072 bytes
Requests per second:    158.55 [#/sec] (mean)
Time per request:       1892.094 [ms] (mean)
Time per request:       6.307 [ms] (mean, across all concurrent requests)
Transfer rate:          25592.47 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0   33 167.6      0     941
Processing:    18 1367 2187.9    210    6294
Waiting:       18  382 350.4    208    1027
Total:         18 1400 2179.6    218    6304

Percentage of the requests served within a certain time (ms)
  50%    218
  66%    270
  75%   1055
  80%   4700
  90%   4913
  95%   6265
  98%   6290
  99%   6298
 100%   6304 (longest request)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment