mirror of
https://github.com/nodejs/node.git
synced 2024-11-21 10:59:27 +00:00
0376b5b7ba
Many benchmarks use test/common/tmpdir. This changes 3 benchmarks that use NODE_TMPDIR to also use test/common/tmpdir. This is necessary in preparation for the next commit that changes tmpdir to delete tmpdir.path when the Node.js process exits. Thus, if multiple benchmarks are run sequentially, the ones that use tmpdir will remove the directory and the ones changed here would fail because it does not exist. This happens when running test/benchmark. Note: to explicitly select a directory for tmpdir, use NODE_TEST_DIR. PR-URL: https://github.com/nodejs/node/pull/28858 Reviewed-By: Rich Trott <rtrott@gmail.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
73 lines
1.6 KiB
JavaScript
73 lines
1.6 KiB
JavaScript
// Test the throughput of the fs.WriteStream class.
|
|
'use strict';
|
|
|
|
const path = require('path');
|
|
const common = require('../common.js');
|
|
const fs = require('fs');
|
|
|
|
const tmpdir = require('../../test/common/tmpdir');
|
|
tmpdir.refresh();
|
|
const filename = path.resolve(tmpdir.path,
|
|
`.removeme-benchmark-garbage-${process.pid}`);
|
|
|
|
const bench = common.createBenchmark(main, {
|
|
dur: [5],
|
|
encodingType: ['buf', 'asc', 'utf'],
|
|
size: [2, 1024, 65535, 1024 * 1024]
|
|
});
|
|
|
|
function main({ dur, encodingType, size }) {
|
|
var encoding;
|
|
|
|
var chunk;
|
|
switch (encodingType) {
|
|
case 'buf':
|
|
chunk = Buffer.alloc(size, 'b');
|
|
break;
|
|
case 'asc':
|
|
chunk = 'a'.repeat(size);
|
|
encoding = 'ascii';
|
|
break;
|
|
case 'utf':
|
|
chunk = 'ü'.repeat(Math.ceil(size / 2));
|
|
encoding = 'utf8';
|
|
break;
|
|
default:
|
|
throw new Error(`invalid encodingType: ${encodingType}`);
|
|
}
|
|
|
|
try { fs.unlinkSync(filename); } catch {}
|
|
|
|
var started = false;
|
|
var ended = false;
|
|
|
|
const f = fs.createWriteStream(filename);
|
|
f.on('drain', write);
|
|
f.on('open', write);
|
|
f.on('close', done);
|
|
f.on('finish', () => {
|
|
ended = true;
|
|
const written = fs.statSync(filename).size / 1024;
|
|
try { fs.unlinkSync(filename); } catch {}
|
|
bench.end(written / 1024);
|
|
});
|
|
|
|
|
|
function write() {
|
|
if (!started) {
|
|
started = true;
|
|
setTimeout(() => {
|
|
f.end();
|
|
}, dur * 1000);
|
|
bench.start();
|
|
}
|
|
|
|
while (false !== f.write(chunk, encoding));
|
|
}
|
|
|
|
function done() {
|
|
if (!ended)
|
|
f.emit('finish');
|
|
}
|
|
}
|