-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdata-compression-efficiency.js
87 lines (81 loc) · 2.55 KB
/
data-compression-efficiency.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
82
83
84
85
86
87
import { argv, hrtime } from 'process';
import { createGzip, createBrotliCompress, createDeflate } from 'zlib'
import { PassThrough, pipeline } from 'stream'
import { createReadStream, createWriteStream } from 'fs';
const compressTypes = { 'gzip': createGzip, 'brotli': createBrotliCompress, 'deflate': createDeflate };
const inputPath = argv[2];
let results = {
size: {
original: 0,
gzip: 0,
brotli: 0,
deflate: 0
},
timestamps: {
gzip: {
chunkStartCompression: null,
compressionTime: null
},
brotli: {
chunkStartCompression: null,
compressionTime: null
},
deflate: {
chunkStartCompression: null,
compressionTime: null
}
}
}
const sizeMonit = (key) => {
const stream = new PassThrough()
stream.on('data', (chunk) => {
results.size[key] += chunk.length;
})
return stream;
}
const chunkStarCompressiontTime = (key) => {
const stream = new PassThrough();
stream.on('data', () => {
results.timestamps[key].chunkStartCompression = hrtime.bigint()
})
return stream;
}
const chunkEndCompressionTime = (key) => {
const stream = new PassThrough();
stream.on('data', () => {
const now = hrtime.bigint();
if (!results.timestamps[key].compressionTime)
results.timestamps[key].compressionTime = now - now // bigint initial value for compressionTime
results.timestamps[key].compressionTime += now - results.timestamps[key].chunkStartCompression;
})
return stream;
}
let streams = Object.keys(compressTypes).length
const done = (err) => {
if (err) {
console.error(err)
process.exit(1)
}
if (--streams === 0) {
console.log('Compression Time:');
Object.keys(compressTypes).forEach((key) => {
console.log(`${(key + ':').padEnd(10, ' ')}: ${results.timestamps[key].compressionTime}ns`)
})
console.log('\n');
console.log('Compression Effeciency:');
Object.keys(compressTypes).forEach((key) => {
console.log(`${(key + ':').padEnd(10, ' ')}: ${((1 - results.size[key] / results.size.original) * 100).toFixed(2)}%`)
})
}
}
Object.keys(compressTypes).forEach((key) => {
pipeline(
createReadStream(inputPath).pipe(sizeMonit('original')),
chunkStarCompressiontTime(key),
compressTypes[key](),
chunkEndCompressionTime(key),
sizeMonit(key),
createWriteStream(inputPath+`.${key}`),
done
)
})