bench script is ready and bench test passed

This commit is contained in:
lion 2025-10-14 21:44:07 +08:00
parent d35a7b9120
commit 008bbbe4d7
2 changed files with 26 additions and 11 deletions

View File

@ -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 文件相同的源文件。

View File

@ -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}`);
});
}