From a2b76d38a9432e575035907dd26f8f6fac074a1e Mon Sep 17 00:00:00 2001 From: lion Date: Wed, 15 Oct 2025 13:39:18 +0800 Subject: [PATCH] fix the duration stats --- binding/javascript/package.json | 5 ++-- binding/javascript/tests/bench.app.js | 37 ++++++++++---------------- binding/javascript/tests/search.app.js | 10 +++---- 3 files changed, 21 insertions(+), 31 deletions(-) diff --git a/binding/javascript/package.json b/binding/javascript/package.json index c314fd0..120a197 100644 --- a/binding/javascript/package.json +++ b/binding/javascript/package.json @@ -1,6 +1,6 @@ { "name": "ip2region.js", - "version": "3.1.0", + "version": "3.1.1", "description": "official javascript binding for ip2region with both IPv4 and IPv6 supported ", "type": "module", "main": "index.js", @@ -40,7 +40,8 @@ "@types/jest": "^30.0.0", "argparse": "^2.0.1", "jest": "^30.2.0", + "n-readlines": "^1.0.1", "ts-jest": "^29.4.5", "typescript": "^5.9.3" } -} \ No newline at end of file +} diff --git a/binding/javascript/tests/bench.app.js b/binding/javascript/tests/bench.app.js index 913aa91..f9ee536 100644 --- a/binding/javascript/tests/bench.app.js +++ b/binding/javascript/tests/bench.app.js @@ -7,7 +7,7 @@ import * as xdb from '../index.js'; import {ArgumentParser} from 'argparse'; -import readline from 'node:readline'; +import LineByLine from 'n-readlines'; import fs from 'fs'; @@ -82,7 +82,7 @@ const _split = (line) => { return ps; } -const main = () => { +const main = async () => { if (dbPath.length < 1 || srcPath.length < 1) { parser.print_help(); return; @@ -91,16 +91,12 @@ const main = () => { const searcher = createSearcher(); console.log(`Searcher: ${searcher.toString()}`); - let totalMicroSecs = 0, count = 0; - // read the source line and do the search bench - const rl = readline.createInterface({ - input: fs.createReadStream(srcPath), - crlfDelay: Infinity - }); - rl.on('line', async (l) => { - const ps = _split(l); - const st = process.hrtime(); + let totalMicroSecs = 0, count = 0, line = null; + const rl = new LineByLine(srcPath); + while (line = rl.next()) { + const ps = _split(line.toString('utf-8')); + const sTime = process.hrtime(); const sip = xdb.parseIP(ps[0]); const eip = xdb.parseIP(ps[1]); if (xdb.ipCompare(sip, eip) > 0) { @@ -115,19 +111,14 @@ const main = () => { } count++; } + const diff = process.hrtime(sTime); + const took = diff[0] * 1_000_000 + diff[1] / 1e3; + totalMicroSecs += took; + } - 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 = 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}`); - }); + const tookSec = totalMicroSecs / 1e6; + const _eachUs = count == 0 ? 0 : totalMicroSecs / count; + console.log(`Bench finished, {cachePolicy: ${cachePolicy}, total: ${count}, took: ${tookSec} s, cost: ${_eachUs} μs/op}`); } main(); \ No newline at end of file diff --git a/binding/javascript/tests/search.app.js b/binding/javascript/tests/search.app.js index 9a9ebda..653e0c5 100644 --- a/binding/javascript/tests/search.app.js +++ b/binding/javascript/tests/search.app.js @@ -8,8 +8,6 @@ import * as xdb from '../index.js'; import {ArgumentParser} from 'argparse'; import fs from 'fs'; -import { parseIP } from '../util.js'; - const parser = new ArgumentParser({ add_help: true, @@ -62,10 +60,10 @@ const createSearcher = () => { const readlineSync = () => { return new Promise((resolve, reject) => { - process.stdin.resume() + process.stdin.resume(); process.stdin.on('data', (buff) => { process.stdin.pause(); - resolve(buff.toString('utf-8')) + resolve(buff.toString('utf-8')); }); }); } @@ -95,7 +93,6 @@ type 'quit' to exit`); } // parse the ip address - const sTime = process.hrtime(); let ipBytes = null; try { ipBytes = xdb.parseIP(ipString); @@ -105,6 +102,7 @@ type 'quit' to exit`); } // do the search + const sTime = process.hrtime(); let region = null; try { region = await searcher.search(ipBytes); @@ -114,7 +112,7 @@ type 'quit' to exit`); } const diff = process.hrtime(sTime); - const took = diff[0] * 1_000_000 + diff[1] / 1e6; + const took = diff[0] * 1_000_000 + diff[1] / 1e3; console.log(`{region: ${region}, ioCount: ${searcher.getIOCount()}, took: ${took} μs}`); } }