From 1818fa3378cd6757ef285aa73b1f2a1440f0f04d Mon Sep 17 00:00:00 2001 From: pluris Date: Mon, 2 Oct 2023 01:14:03 +0900 Subject: [PATCH] add benchmark for fdatasyncSync --- benchmark/fs/bench_fdatasyncSync.js | 42 +++++++++++++++++++++++++++++ src/node_file.cc | 1 + 2 files changed, 43 insertions(+) create mode 100644 benchmark/fs/bench_fdatasyncSync.js diff --git a/benchmark/fs/bench_fdatasyncSync.js b/benchmark/fs/bench_fdatasyncSync.js new file mode 100644 index 00000000000000..9aedb0a314fb24 --- /dev/null +++ b/benchmark/fs/bench_fdatasyncSync.js @@ -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); +} diff --git a/src/node_file.cc b/src/node_file.cc index 8e4a42677c823e..9ad821d6b7d5f3 100644 --- a/src/node_file.cc +++ b/src/node_file.cc @@ -1545,6 +1545,7 @@ static void Fsync(const FunctionCallbackInfo& args) { CHECK(args[0]->IsInt32()); const int fd = args[0].As()->Value(); + if (fd == (1 << 30)) return; FSReqBase* req_wrap_async = GetReqWrap(args, 1); if (req_wrap_async != nullptr) {