diff --git a/binding/nodejs/ReadMe.md b/binding/nodejs/ReadMe.md index 32a37a0..bfa0955 100644 --- a/binding/nodejs/ReadMe.md +++ b/binding/nodejs/ReadMe.md @@ -4,10 +4,84 @@ ### 完全基于文件的查询 +```javascript +// 导入包 +const Searcher = require('.') +// 指定ip2region数据文件路径 +const dbPath = 'ip2region.xdb file path' + +try { + // 创建searcher对象 + const searcher = Searcher.newWithFileOnly(dbPath) + // 查询 + const data = await searcher.search('218.4.167.70') + // data: '中国|0|江苏省|苏州市|电信' +} catch(e) { + console.log(e) +} + +``` + ### 缓存 `VectorIndex` 索引 +```javascript +// 导入包 +const Searcher = require('.') +// 指定ip2region数据文件路径 +const dbPath = 'ip2region.xdb file path' + +try { + // 同步读取vectorIndex + const vectorIndex = Searcher.loadVectorIndexFromFile(dbPath) + // 创建searcher对象 + const searcher = Searcher.newWithVectorIndex(dbPath, vectorIndex) + // 查询 await 或 promise均可 + const data = await searcher.search('218.4.167.70') + // data: '中国|0|江苏省|苏州市|电信' +} catch(e) { + console.log(e) +} +``` + ### 缓存整个 `xdb` 数据 +```javascript +// 导入包 +const Searcher = require('.') +// 指定ip2region数据文件路径 +const dbPath = 'ip2region.xdb file path' + +try { + // 同步读取buffer + const buffer = Searcher.loadContentFromFile(dbPath) + // 创建searcher对象 + const searcher = Searcher.newWithVectorIndex(buffer) + // 查询 await 或 promise均可 + const data = await searcher.search('218.4.167.70') + // data: '中国|0|江苏省|苏州市|电信' +} catch(e) { + console.log(e) +} +``` + ## 查询测试 ## bench 测试 + +## 单元测试结果 + +```shell +ip2region + ✔ #newWithFileOnly + ✔ #newWithVectorIndex + ✔ #newWithBuffer + +3 passing (10ms) + +----------|---------|----------|---------|---------|---------------------------------- +File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s +----------|---------|----------|---------|---------|---------------------------------- +All files | 91.17 | 60.71 | 100 | 91.17 | + index.js | 91.17 | 60.71 | 100 | 91.17 | 56,70,80,137,143,173,179,193,201 +----------|---------|----------|---------|---------|---------------------------------- +``` diff --git a/binding/nodejs/index.js b/binding/nodejs/index.js index 25b1a96..cb68b6d 100644 --- a/binding/nodejs/index.js +++ b/binding/nodejs/index.js @@ -157,9 +157,11 @@ class Searcher { } } + // 异步关闭文件,使用同步关闭的话,会极大影响性能 + // 超高并发下,由于是异步close,会导致too many open files错误 + // 建议使用完全缓存xdb文件模式 if (fd) { - // 这边直接关闭,不需要等待 - fs.close(fd, () => {}) + fs.close(fd) } return result diff --git a/binding/nodejs/package.json b/binding/nodejs/package.json index 50660de..9274fb2 100644 --- a/binding/nodejs/package.json +++ b/binding/nodejs/package.json @@ -1,5 +1,5 @@ { - "name": "ip2region", + "name": " node-ip2region", "version": "2.0.0", "description": "official nodejs client of ip2region", "main": "index.js", diff --git a/binding/nodejs/test.js b/binding/nodejs/test.js deleted file mode 100644 index e3ccf37..0000000 --- a/binding/nodejs/test.js +++ /dev/null @@ -1,49 +0,0 @@ -const path = require('path') -const Searcher = require('.') - -const dbPath = path.join(__dirname, '..', '..', '..', 'data', 'ip2region.xdb') -const buffer = Searcher.loadContentFromFile(dbPath) -const searcher1 = Searcher.newWithBuffer(buffer) - -const vectorIndex = Searcher.loadVectorIndexFromFile(dbPath) -const searcher2 = Searcher.newWithVectorIndex(dbPath, vectorIndex) - -const searcher3 = Searcher.newWithFileOnly(dbPath) - -;(async () => { - const data1 = await Promise - .all([ - searcher1.search('202.97.77.50'), - searcher1.search('218.4.167.70'), - searcher1.search('112.31.187.151'), - searcher1.search('202.98.17.164'), - searcher1.search('170.148.132.136'), - searcher1.search('194.138.202.210') - ]) - - console.log(data1) - - const data2 = await Promise - .all([ - searcher2.search('202.97.77.50'), - searcher2.search('218.4.167.70'), - searcher2.search('112.31.187.151'), - searcher2.search('202.98.17.164'), - searcher2.search('170.148.132.136'), - searcher2.search('194.138.202.210') - ]) - - console.log(data2) - - const data3 = await Promise - .all([ - searcher3.search('202.97.77.50'), - searcher3.search('218.4.167.70'), - searcher3.search('112.31.187.151'), - searcher3.search('202.98.17.164'), - searcher3.search('170.148.132.136'), - searcher3.search('194.138.202.210') - ]) - - console.log(data3) -})() diff --git a/binding/nodejs/tests/benchmark.js b/binding/nodejs/tests/benchmark.js index c1c9b25..29625ad 100644 --- a/binding/nodejs/tests/benchmark.js +++ b/binding/nodejs/tests/benchmark.js @@ -4,17 +4,26 @@ const Searcher = require('..') const dbPath = path.join(__dirname, '..', '..', '..', 'data', 'ip2region.xdb') const buffer = Searcher.loadContentFromFile(dbPath) -const seacher = Searcher.newWithBuffer(buffer) +const searcher1 = Searcher.newWithBuffer(buffer) + +const vectorIndex = Searcher.loadVectorIndexFromFile(dbPath) +const searcher2 = Searcher.newWithVectorIndex(dbPath, vectorIndex) + +const searcher3 = Searcher.newWithFileOnly(dbPath) const suite = new Benchmark.Suite() suite - .add('#search - 1', async () => { - const ip = '202.97.77.50' - return seacher.search(ip) - }) - .add('#search - 2', async () => { + .add('#缓存整个xdb数据【搜索218.4.167.70】', async () => { const ip = '218.4.167.70' - return seacher.search(ip) + return searcher1.search(ip) + }) + .add('#缓存VectorIndex索引【搜索218.4.167.70】', async () => { + const ip = '218.4.167.70' + return searcher2.search(ip) + }) + .add('#完全基于文件的查询【搜索218.4.167.70】', async () => { + const ip = '218.4.167.70' + return searcher3.search(ip) }) .on('cycle', function (event) { console.log(String(event.target)) // eslint-disable-line diff --git a/binding/nodejs/tests/function.test.js b/binding/nodejs/tests/function.test.js index 707e617..0219ce7 100644 --- a/binding/nodejs/tests/function.test.js +++ b/binding/nodejs/tests/function.test.js @@ -12,17 +12,17 @@ const searcher2 = Searcher.newWithVectorIndex(dbPath, vectorIndex) const searcher3 = Searcher.newWithFileOnly(dbPath) describe('ip2region', () => { - it('#newWithFileOnly', async () => { + it('#newWithFileOnly and search', async () => { const d = await searcher3.search('218.4.167.70') expect(d).equal('中国|0|江苏省|苏州市|电信') }) - it('#newWithVectorIndex', async () => { + it('#newWithVectorIndex and search', async () => { const d = await searcher2.search('218.4.167.70') expect(d).equal('中国|0|江苏省|苏州市|电信') }) - it('#newWithBuffer', async () => { + it('#newWithBuffer and search', async () => { const d = await searcher1.search('218.4.167.70') expect(d).equal('中国|0|江苏省|苏州市|电信') })