mirror of
https://github.com/nodejs/node.git
synced 2024-11-24 12:10:08 +01:00
ed49722a8a
PR-URL: https://github.com/nodejs/node/pull/50032 Refs: https://github.com/nodejs/performance/issues/106 Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com>
41 lines
769 B
JavaScript
41 lines
769 B
JavaScript
'use strict';
|
|
|
|
const common = require('../common');
|
|
const fs = require('fs');
|
|
const tmpdir = require('../../test/common/tmpdir');
|
|
tmpdir.refresh();
|
|
|
|
const path = tmpdir.resolve(`new-file-${process.pid}`);
|
|
fs.appendFileSync(path, 'Some content.');
|
|
|
|
const bench = common.createBenchmark(main, {
|
|
type: ['invalid', 'valid'],
|
|
n: [1e4],
|
|
});
|
|
|
|
function main({ n, type }) {
|
|
let fd;
|
|
|
|
switch (type) {
|
|
case 'invalid':
|
|
fd = 1 << 30;
|
|
break;
|
|
case 'valid':
|
|
fd = fs.openSync(path, 'r+');
|
|
break;
|
|
default:
|
|
throw new Error('Invalid type');
|
|
}
|
|
|
|
bench.start();
|
|
for (let i = 0; i < n; i++) {
|
|
try {
|
|
fs.ftruncateSync(fd, 4);
|
|
} catch {
|
|
// do nothing
|
|
}
|
|
}
|
|
bench.end(n);
|
|
if (type === 'valid') fs.closeSync(fd);
|
|
}
|