mirror of
https://github.com/lionsoul2014/ip2region.git
synced 2025-12-08 19:25:22 +00:00
add test app
This commit is contained in:
parent
159ec582f6
commit
607fbcdde7
2
.gitignore
vendored
2
.gitignore
vendored
@ -74,7 +74,7 @@ target
|
||||
/binding/nodejs/coverage
|
||||
/binding/nodejs/node_modules
|
||||
/binding/nodejs/.nyc_output
|
||||
/bingind/nodejs/package-lock.json
|
||||
/binging/nodejs/package-lock.json
|
||||
|
||||
# maker
|
||||
## golang
|
||||
|
||||
@ -69,21 +69,29 @@ try {
|
||||
可以通过 `java -jar ip2region-{version}.jar search` 命令来测试查询:
|
||||
|
||||
```shell
|
||||
➜ java git:(v2.0_xdb) ✗ java -jar target/ip2region-2.6.0.jar search
|
||||
java -jar ip2region-{version}.jar search [command options]
|
||||
options:
|
||||
--db string ip2region binary xdb file path
|
||||
--cache-policy string cache policy: file/vectorIndex/content
|
||||
➜ nodejs git:(v2.0-for-nodejs) ✗ node ./tests/test.app.js --help
|
||||
usage: Usage node test.app.js <agrs>
|
||||
|
||||
ip2region test app
|
||||
|
||||
optional arguments:
|
||||
-h, --help show this help message and exit
|
||||
-d DB, --db DB ip2region binary xdb file path, default: ../../data/ip2region.xdb
|
||||
-c CACHE_POLICY, --cache-policy CACHE_POLICY
|
||||
cache policy: file/vectorIndex/content, default: content
|
||||
```
|
||||
|
||||
例如:使用默认的 data/ip2region.xdb 文件进行查询测试:
|
||||
|
||||
```shell
|
||||
➜ java git:(v2.0_xdb) ✗ java -jar target/ip2region-2.6.0.jar search --db=../../data/ip2region.xdb
|
||||
ip2region xdb searcher test program, cachePolicy: vectorIndex
|
||||
➜ nodejs git:(v2.0-for-nodejs) ✗ node ./tests/test.app.js
|
||||
parameters:
|
||||
dbPath: ../../data/ip2region.xdb
|
||||
cache-policy: content
|
||||
|
||||
type 'quit' to exit
|
||||
ip2region>> 1.2.3.4
|
||||
{region: 美国|0|华盛顿|0|谷歌, ioCount: 7, took: 82 μs}
|
||||
{ region: '美国|0|华盛顿|0|谷歌', ioCount: 0, took: 0.606261 }
|
||||
ip2region>>
|
||||
```
|
||||
|
||||
|
||||
3098
binding/nodejs/package-lock.json
generated
Normal file
3098
binding/nodejs/package-lock.json
generated
Normal file
File diff suppressed because it is too large
Load Diff
@ -14,6 +14,7 @@
|
||||
"devDependencies": {
|
||||
"@types/chai": "^4.3.1",
|
||||
"@types/node": "^18.0.6",
|
||||
"argparse": "^2.0.1",
|
||||
"benchmark": "^2.1.4",
|
||||
"chai": "^4.3.6",
|
||||
"eslint": "^8.20.0",
|
||||
@ -27,4 +28,4 @@
|
||||
"engines": {
|
||||
"node": ">=8.0.0"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -0,0 +1,79 @@
|
||||
const Searcher = require('../')
|
||||
const path = require('path')
|
||||
const { ArgumentParser } = require('argparse')
|
||||
|
||||
// 处理输入参数
|
||||
const parser = new ArgumentParser({
|
||||
add_help: true,
|
||||
description: 'ip2region test app',
|
||||
prog: 'node test.app.js',
|
||||
usage: 'Usage %(prog)s <agrs>'
|
||||
})
|
||||
|
||||
parser.add_argument('-d', '--db', { help: `ip2region binary xdb file path, default: ${path.join(__dirname, '..', '..', '..', 'data', 'ip2region.xdb')}` })
|
||||
parser.add_argument('-c', '--cache-policy', { help: 'cache policy: file/vectorIndex/content, default: content' })
|
||||
|
||||
const args = parser.parse_args()
|
||||
const db = args.db
|
||||
const policy = args.cache_policy
|
||||
|
||||
// 创建searcher对象
|
||||
const createSearcher = () => {
|
||||
const dbPath = db || '../../data/ip2region.xdb'
|
||||
const cachePolicy = policy || 'content'
|
||||
|
||||
let searcher = null
|
||||
let vectorIndex = null
|
||||
let buffer = null
|
||||
|
||||
switch (cachePolicy) {
|
||||
case 'file':
|
||||
searcher = Searcher.newWithFileOnly(dbPath)
|
||||
break
|
||||
case 'vectorIndex':
|
||||
vectorIndex = Searcher.loadVectorIndexFromFile(dbPath)
|
||||
searcher = Searcher.newWithVectorIndex(dbPath, vectorIndex)
|
||||
break
|
||||
default:
|
||||
buffer = Searcher.loadContentFromFile(dbPath)
|
||||
searcher = Searcher.newWithBuffer(buffer)
|
||||
}
|
||||
console.log('parameters: ')
|
||||
console.log(` dbPath: ${dbPath}`)
|
||||
console.log(` cache-policy: ${cachePolicy}`)
|
||||
console.log('')
|
||||
return searcher
|
||||
}
|
||||
|
||||
// 从控制台读取用户一行输入
|
||||
const readlineSync = () => {
|
||||
return new Promise((resolve, reject) => {
|
||||
process.stdin.resume()
|
||||
process.stdin.on('data', data => {
|
||||
process.stdin.pause()
|
||||
resolve(data.toString('utf-8'))
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
const searcher = createSearcher()
|
||||
|
||||
async function main () {
|
||||
console.log('type \'quit\' to exit')
|
||||
while (true) {
|
||||
process.stdout.write('ip2region>> ')
|
||||
const ip = (await readlineSync()).trim()
|
||||
if (ip === 'quit') {
|
||||
process.exit(0)
|
||||
} else {
|
||||
try {
|
||||
const response = await searcher.search(ip)
|
||||
console.log(response)
|
||||
} catch (err) {
|
||||
console.log(err)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
main()
|
||||
Loading…
x
Reference in New Issue
Block a user