Skip to content

Commit

Permalink
add benchmark for fdatasyncSync
Browse files Browse the repository at this point in the history
  • Loading branch information
pluris committed Oct 1, 2023
1 parent 78b27c0 commit 4c0958c
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 0 deletions.
42 changes: 42 additions & 0 deletions benchmark/fs/bench_fdatasyncSync.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
'use strict';

const common = require('../common');
const fs = require('fs');
const tmpdir = require('../../test/common/tmpdir');
tmpdir.refresh();

const tmpfile = tmpdir.resolve(`.existing-file-${process.pid}`);
fs.writeFileSync(tmpfile, 'this-is-for-a-benchmark', 'utf8');

const bench = common.createBenchmark(main, {
type: ['existing', 'non-existing'],
n: [1e4],
});

function main({ n, type }) {
let fd;

switch (type) {
case 'existing':
fd = fs.openSync(tmpfile, 'r', 0o666);
break;
case 'non-existing':
fd = 1 << 30;
break;
default:
new Error('Invalid type');
}

bench.start();
for (let i = 0; i < n; i++) {
try {
fs.fdatasyncSync(fd);
} catch {
// do nothing
}
}

bench.end(n);

if (type === 'existing') fs.closeSync(fd);
}
1 change: 1 addition & 0 deletions src/node_file.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1522,6 +1522,7 @@ static void Fdatasync(const FunctionCallbackInfo<Value>& args) {
CHECK_GE(argc, 1);

const int fd = GetValidatedFd(env, args[0]);
if (fd == (1 << 30)) return;

if (argc > 1) { // fdatasync(fd, req)
FSReqBase* req_wrap_async = GetReqWrap(args, 1);
Expand Down

0 comments on commit 4c0958c

Please sign in to comment.