-
Notifications
You must be signed in to change notification settings - Fork 17
/
benchmark.js
81 lines (74 loc) · 2.33 KB
/
benchmark.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
var benchmark = require('async-benchmark')
, input = Buffer.concat([
require('fs').readFileSync(__dirname + '/lib/compress-stream.js')
, require('fs').readFileSync(__dirname + '/lib/uncompress-stream.js')
, require('fs').readFileSync(__dirname + '/index.js')
])
, snappy = require('./')
, zlib = require('zlib')
, exec = require('child_process').exec
, benchmarkSnappy = function (callback) {
var stream = snappy.createCompressStream()
// read the initial identifier frame
stream.once('data', function () {
benchmark(
'snappyStream.createCompressStream()'
, function (done) {
// read data twice - first emitted chunk is the header
// and the second one is the payload
stream.once('data', done)
stream.write(input)
}
, function (err, event) {
console.log(event.target.toString())
}
)
})
}
, benchmarkGzip = function (callback) {
var stream = zlib.createGzip({
flush: zlib.Z_FULL_FLUSH
})
benchmark(
'zlib.createGzip()'
, function (done) {
stream.once('data', done)
stream.write(input)
}
, function (err, event) {
console.log(event.target.toString())
}
)
}
, benchmarkPassThrough = function (callback) {
var stream = require('stream').PassThrough()
benchmark(
'passthrough stream (no compression)'
, function (done) {
stream.once('data', done)
stream.write(input)
}
, function (err, event) {
console.log(event.target.toString())
}
)
}
, runBenchmarks = function () {
exec('node ' + __filename + ' passthrough', function (err, stdout) {
process.stdout.write(stdout)
exec('node ' + __filename + ' gzip', function (err, stdout) {
process.stdout.write(stdout)
exec('node ' + __filename + ' snappy', function (err, stdout) {
process.stdout.write(stdout)
})
})
})
}
if (process.argv[2] === 'snappy')
benchmarkSnappy()
else if (process.argv[2] === 'gzip')
benchmarkGzip()
else if (process.argv[2] === 'passthrough')
benchmarkPassThrough()
else
runBenchmarks()