From 008bbbe4d7ebe24e61368afa416593ce9b7c270b Mon Sep 17 00:00:00 2001 From: lion Date: Tue, 14 Oct 2025 21:44:07 +0800 Subject: [PATCH] bench script is ready and bench test passed --- binding/javascript/ReadMe.md | 17 +++++++++++++++-- binding/javascript/tests/bench.app.js | 20 +++++++++++--------- 2 files changed, 26 insertions(+), 11 deletions(-) diff --git a/binding/javascript/ReadMe.md b/binding/javascript/ReadMe.md index 53f2763..3eca0c3 100644 --- a/binding/javascript/ReadMe.md +++ b/binding/javascript/ReadMe.md @@ -169,17 +169,30 @@ try { # bench 测试 -可以通过 `` 命令来进行 bench 测试,一方面确保 `xdb` 文件没有错误,一方面可以评估查询性能: +可以通过 `node tests/bench.app.js` 命令来进行 bench 测试,一方面确保 `xdb` 文件没有错误,一方面可以评估查询性能: ```bash +➜ javascript git:(fr_javascript_ipv6) ✗ node tests/bench.app.js +usage: Usage node tests/bench.app.js [command options] + +ip2region bench script + +optional arguments: + -h, --help show this help message and exit + --db DB ip2region binary xdb file path + --src SRC source ip text file path + --cache-policy CACHE_POLICY + cache policy: file/vectorIndex/content, default: vectorIndex ``` 例如:通过默认的 data/ip2region_v4.xdb 和 data/ipv4_source.txt 文件进行 IPv4 的 bench 测试: ```bash +node tests/bench.app.js --db=../../data/ip2region_v4.xdb --src=../../data/ipv4_source.txt ``` 例如:通过默认的 data/ip2region_v6.xdb 和 data/ipv6_source.txt 文件进行 IPv6 的 bench 测试: ```bash +node tests/bench.app.js --db=../../data/ip2region_v6.xdb --src=../../data/ipv6_source.txt ``` 可以通过分别设置 `cache-policy` 为 file/vectorIndex/content 来测试三种不同缓存实现的效果。 -@Note: 注意 bench 使用的 src 文件要是生成对应 xdb 文件相同的源文件。 +@Note: 注意 bench 使用的 src 文件要是生成对应 xdb 文件相同的源文件。 \ No newline at end of file diff --git a/binding/javascript/tests/bench.app.js b/binding/javascript/tests/bench.app.js index 2bbdcb3..28955bc 100644 --- a/binding/javascript/tests/bench.app.js +++ b/binding/javascript/tests/bench.app.js @@ -82,9 +82,9 @@ const _split = (line) => { return ps; } -const getMs = () => { - const hrTime = process.hrtime(); - return hrTime[0] * 1000000 + hrTime[1] / 1000; +const getMillSecs = () => { + const t = process.hrtime(); + return t[0] * 1_000 + t[1] / 1_000_000; } // console.log(`dbPath=${dbPath}, src=${src}, cachePolicy=${cachePolicy}`); @@ -97,7 +97,7 @@ const main = () => { const searcher = createSearcher(); console.log(`Searcher: ${searcher.toString()}`); - let totalNs = 0, count = 0; + let totalMicroSecs = 0, count = 0; // read the source line and do the search bench const rl = readline.createInterface({ @@ -106,7 +106,7 @@ const main = () => { }); rl.on('line', async (l) => { const ps = _split(l); - const sTime = process.hrtime(); + const st = process.hrtime(); const sip = xdb.parseIP(ps[0]); const eip = xdb.parseIP(ps[1]); if (xdb.ipCompare(sip, eip) > 0) { @@ -121,16 +121,18 @@ const main = () => { } count++; } - totalNs += process.hrtime(sTime); + + const diff = process.hrtime(st); + totalMicroSecs += (diff[0] * 1_000_1000 + (diff[1] / 1e6)); }).on('error', (err) => { console.log(err); process.exit(1); }); process.on('exit', (code) => { - const tookSec = totalNs / 1e9; - const _eachUs = count == 0 ? 0 : totalNs / 1e3 / count; - console.log(`Bench finished, {cachePolicy: ${cachePolicy}, total: ${count}, took: ${tookSec}s, cost: ${_eachUs} μs/op}`); + const tookSec = totalMicroSecs / 1_000_000; + const _eachUs = count == 0 ? 0 : totalMicroSecs / count; + console.log(`Bench finished, {cachePolicy: ${cachePolicy}, total: ${count}, took: ${tookSec} s, cost: ${_eachUs} µs/op}`); }); }