fix the duration stats

This commit is contained in:
lion 2025-10-15 13:39:18 +08:00
parent 8c7c83cf7f
commit a2b76d38a9
3 changed files with 21 additions and 31 deletions

View File

@ -1,6 +1,6 @@
{ {
"name": "ip2region.js", "name": "ip2region.js",
"version": "3.1.0", "version": "3.1.1",
"description": "official javascript binding for ip2region with both IPv4 and IPv6 supported ", "description": "official javascript binding for ip2region with both IPv4 and IPv6 supported ",
"type": "module", "type": "module",
"main": "index.js", "main": "index.js",
@ -40,6 +40,7 @@
"@types/jest": "^30.0.0", "@types/jest": "^30.0.0",
"argparse": "^2.0.1", "argparse": "^2.0.1",
"jest": "^30.2.0", "jest": "^30.2.0",
"n-readlines": "^1.0.1",
"ts-jest": "^29.4.5", "ts-jest": "^29.4.5",
"typescript": "^5.9.3" "typescript": "^5.9.3"
} }

View File

@ -7,7 +7,7 @@
import * as xdb from '../index.js'; import * as xdb from '../index.js';
import {ArgumentParser} from 'argparse'; import {ArgumentParser} from 'argparse';
import readline from 'node:readline'; import LineByLine from 'n-readlines';
import fs from 'fs'; import fs from 'fs';
@ -82,7 +82,7 @@ const _split = (line) => {
return ps; return ps;
} }
const main = () => { const main = async () => {
if (dbPath.length < 1 || srcPath.length < 1) { if (dbPath.length < 1 || srcPath.length < 1) {
parser.print_help(); parser.print_help();
return; return;
@ -91,16 +91,12 @@ const main = () => {
const searcher = createSearcher(); const searcher = createSearcher();
console.log(`Searcher: ${searcher.toString()}`); console.log(`Searcher: ${searcher.toString()}`);
let totalMicroSecs = 0, count = 0;
// read the source line and do the search bench // read the source line and do the search bench
const rl = readline.createInterface({ let totalMicroSecs = 0, count = 0, line = null;
input: fs.createReadStream(srcPath), const rl = new LineByLine(srcPath);
crlfDelay: Infinity while (line = rl.next()) {
}); const ps = _split(line.toString('utf-8'));
rl.on('line', async (l) => { const sTime = process.hrtime();
const ps = _split(l);
const st = process.hrtime();
const sip = xdb.parseIP(ps[0]); const sip = xdb.parseIP(ps[0]);
const eip = xdb.parseIP(ps[1]); const eip = xdb.parseIP(ps[1]);
if (xdb.ipCompare(sip, eip) > 0) { if (xdb.ipCompare(sip, eip) > 0) {
@ -115,19 +111,14 @@ const main = () => {
} }
count++; count++;
} }
const diff = process.hrtime(sTime);
const took = diff[0] * 1_000_000 + diff[1] / 1e3;
totalMicroSecs += took;
}
const diff = process.hrtime(st); const tookSec = totalMicroSecs / 1e6;
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; const _eachUs = count == 0 ? 0 : totalMicroSecs / count;
console.log(`Bench finished, {cachePolicy: ${cachePolicy}, total: ${count}, took: ${tookSec} s, cost: ${_eachUs} µs/op}`); console.log(`Bench finished, {cachePolicy: ${cachePolicy}, total: ${count}, took: ${tookSec} s, cost: ${_eachUs} μs/op}`);
});
} }
main(); main();

View File

@ -8,8 +8,6 @@
import * as xdb from '../index.js'; import * as xdb from '../index.js';
import {ArgumentParser} from 'argparse'; import {ArgumentParser} from 'argparse';
import fs from 'fs'; import fs from 'fs';
import { parseIP } from '../util.js';
const parser = new ArgumentParser({ const parser = new ArgumentParser({
add_help: true, add_help: true,
@ -62,10 +60,10 @@ const createSearcher = () => {
const readlineSync = () => { const readlineSync = () => {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
process.stdin.resume() process.stdin.resume();
process.stdin.on('data', (buff) => { process.stdin.on('data', (buff) => {
process.stdin.pause(); 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 // parse the ip address
const sTime = process.hrtime();
let ipBytes = null; let ipBytes = null;
try { try {
ipBytes = xdb.parseIP(ipString); ipBytes = xdb.parseIP(ipString);
@ -105,6 +102,7 @@ type 'quit' to exit`);
} }
// do the search // do the search
const sTime = process.hrtime();
let region = null; let region = null;
try { try {
region = await searcher.search(ipBytes); region = await searcher.search(ipBytes);
@ -114,7 +112,7 @@ type 'quit' to exit`);
} }
const diff = process.hrtime(sTime); 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}`); console.log(`{region: ${region}, ioCount: ${searcher.getIOCount()}, took: ${took} μs}`);
} }
} }