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",
"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"
}
}
}

View File

@ -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();

View File

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