GeezerVison is showing github.com — benchmark: add zstdCompress benchmark · nodejs/node@d62f112
File tree
Expand file treeCollapse file tree
Open diff view settings
Expand file treeCollapse file tree
Open diff view settings
Collapse file
Copy file name to clipboard +41Lines changed: 41 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
|
| 1 | +'use strict'; |
| 2 | +const common = require('../common.js'); |
| 3 | +const fs = require('fs'); |
| 4 | +const zlib = require('zlib'); |
| 5 | + |
| 6 | +const bench = common.createBenchmark(main, { |
| 7 | + method: ['zstdCompress', 'zstdCompressSync'], |
| 8 | + level: [3, 12], |
| 9 | + inputLen: [16 * 1024, 256 * 1024], |
| 10 | + n: [100], |
| 11 | +}); |
| 12 | + |
| 13 | +function main({ n, method, level, inputLen }) { |
| 14 | + const input = Buffer.alloc(inputLen, fs.readFileSync(__filename)); |
| 15 | + const opts = { params: { [zlib.constants.ZSTD_c_compressionLevel]: level } }; |
| 16 | + |
| 17 | + switch (method) { |
| 18 | + // Performs `n` single zstdCompress operations |
| 19 | + case 'zstdCompress': { |
| 20 | + let i = 0; |
| 21 | + bench.start(); |
| 22 | + (function next(err) { |
| 23 | + if (err) throw err; |
| 24 | + if (i++ === n) |
| 25 | + return bench.end(n); |
| 26 | + zlib.zstdCompress(input, opts, next); |
| 27 | + })(); |
| 28 | + break; |
| 29 | + } |
| 30 | + // Performs `n` single zstdCompressSync operations |
| 31 | + case 'zstdCompressSync': { |
| 32 | + bench.start(); |
| 33 | + for (let i = 0; i < n; ++i) |
| 34 | + zlib.zstdCompressSync(input, opts); |
| 35 | + bench.end(n); |
| 36 | + break; |
| 37 | + } |
| 38 | + default: |
| 39 | + throw new Error('Unsupported zstd method'); |
| 40 | + } |
| 41 | +} |