add test app
This commit is contained in:
parent
159ec582f6
commit
607fbcdde7
|
|
@ -74,7 +74,7 @@ target
|
||||||
/binding/nodejs/coverage
|
/binding/nodejs/coverage
|
||||||
/binding/nodejs/node_modules
|
/binding/nodejs/node_modules
|
||||||
/binding/nodejs/.nyc_output
|
/binding/nodejs/.nyc_output
|
||||||
/bingind/nodejs/package-lock.json
|
/binging/nodejs/package-lock.json
|
||||||
|
|
||||||
# maker
|
# maker
|
||||||
## golang
|
## golang
|
||||||
|
|
|
||||||
|
|
@ -69,21 +69,29 @@ try {
|
||||||
可以通过 `java -jar ip2region-{version}.jar search` 命令来测试查询:
|
可以通过 `java -jar ip2region-{version}.jar search` 命令来测试查询:
|
||||||
|
|
||||||
```shell
|
```shell
|
||||||
➜ java git:(v2.0_xdb) ✗ java -jar target/ip2region-2.6.0.jar search
|
➜ nodejs git:(v2.0-for-nodejs) ✗ node ./tests/test.app.js --help
|
||||||
java -jar ip2region-{version}.jar search [command options]
|
usage: Usage node test.app.js <agrs>
|
||||||
options:
|
|
||||||
--db string ip2region binary xdb file path
|
ip2region test app
|
||||||
--cache-policy string cache policy: file/vectorIndex/content
|
|
||||||
|
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 文件进行查询测试:
|
例如:使用默认的 data/ip2region.xdb 文件进行查询测试:
|
||||||
|
|
||||||
```shell
|
```shell
|
||||||
➜ java git:(v2.0_xdb) ✗ java -jar target/ip2region-2.6.0.jar search --db=../../data/ip2region.xdb
|
➜ nodejs git:(v2.0-for-nodejs) ✗ node ./tests/test.app.js
|
||||||
ip2region xdb searcher test program, cachePolicy: vectorIndex
|
parameters:
|
||||||
|
dbPath: ../../data/ip2region.xdb
|
||||||
|
cache-policy: content
|
||||||
|
|
||||||
type 'quit' to exit
|
type 'quit' to exit
|
||||||
ip2region>> 1.2.3.4
|
ip2region>> 1.2.3.4
|
||||||
{region: 美国|0|华盛顿|0|谷歌, ioCount: 7, took: 82 μs}
|
{ region: '美国|0|华盛顿|0|谷歌', ioCount: 0, took: 0.606261 }
|
||||||
ip2region>>
|
ip2region>>
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
|
||||||
File diff suppressed because it is too large
Load Diff
|
|
@ -14,6 +14,7 @@
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@types/chai": "^4.3.1",
|
"@types/chai": "^4.3.1",
|
||||||
"@types/node": "^18.0.6",
|
"@types/node": "^18.0.6",
|
||||||
|
"argparse": "^2.0.1",
|
||||||
"benchmark": "^2.1.4",
|
"benchmark": "^2.1.4",
|
||||||
"chai": "^4.3.6",
|
"chai": "^4.3.6",
|
||||||
"eslint": "^8.20.0",
|
"eslint": "^8.20.0",
|
||||||
|
|
@ -27,4 +28,4 @@
|
||||||
"engines": {
|
"engines": {
|
||||||
"node": ">=8.0.0"
|
"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…
Reference in New Issue