mirror of
https://github.com/netfun2000/ip2region.git
synced 2026-02-27 09:44:31 +08:00
[Feature] Add 'memorySearch' and 'memorySearchSync' for Nodejs
1) Add features. 2) Add unit tests. 3) Add benchmark.
This commit is contained in:
94
binding/nodejs/tests/benchmarkTests/main.js
Normal file
94
binding/nodejs/tests/benchmarkTests/main.js
Normal file
@@ -0,0 +1,94 @@
|
||||
const Benchmark = require('benchmark');
|
||||
|
||||
const suite = new Benchmark.Suite();
|
||||
const searcher = require('../../ip2region').create('../../data/ip2region.db');
|
||||
const testDatas = require('../utils/testData');
|
||||
const asyncFor = require('../utils/asyncFor');
|
||||
|
||||
suite.add("MemorySearchSync", () => {
|
||||
for (let i = 0; i < testDatas.length; ++i) {
|
||||
searcher.memorySearchSync(testDatas[i]);
|
||||
}
|
||||
})
|
||||
.add("BinarySearchSync", () => {
|
||||
for (let i = 0; i < testDatas.length; ++i) {
|
||||
searcher.binarySearchSync(testDatas[i]);
|
||||
}
|
||||
})
|
||||
.add("BtreeSearchSync", () => {
|
||||
for (let i = 0; i < testDatas.length; ++i) {
|
||||
searcher.btreeSearchSync(testDatas[i]);
|
||||
}
|
||||
})
|
||||
.add("MemorySearch", {
|
||||
defer: true,
|
||||
fn: function (completeCallBack) {
|
||||
asyncFor(testDatas,
|
||||
(v, c) => {
|
||||
searcher.memorySearch(v, () => {
|
||||
c();
|
||||
});
|
||||
},
|
||||
() => {
|
||||
completeCallBack.resolve();
|
||||
});
|
||||
}
|
||||
})
|
||||
.add("BinarySearch", {
|
||||
defer: true,
|
||||
fn: function (completeCallBack) {
|
||||
asyncFor(testDatas,
|
||||
(v, c) => {
|
||||
searcher.binarySearch(v, () => {
|
||||
c();
|
||||
});
|
||||
},
|
||||
() => {
|
||||
completeCallBack.resolve();
|
||||
});
|
||||
}
|
||||
})
|
||||
.add("BtreeSearch", {
|
||||
defer: true,
|
||||
fn: function (completeCallBack) {
|
||||
asyncFor(testDatas,
|
||||
(v, c) => {
|
||||
searcher.btreeSearch(v, () => {
|
||||
c();
|
||||
});
|
||||
},
|
||||
() => {
|
||||
completeCallBack.resolve();
|
||||
});
|
||||
}
|
||||
})
|
||||
.on('cycle', function (event) {
|
||||
console.log(String(event.target));
|
||||
})
|
||||
.on('complete', function () {
|
||||
|
||||
let results = new Array();
|
||||
|
||||
for (let prop in this) {
|
||||
if (!isNaN(prop)) {
|
||||
const eachResult = {
|
||||
name: this[prop].name,
|
||||
mean: this[prop].stats.mean * 1000, //second => millisecond
|
||||
moe: this[prop].stats.moe,
|
||||
rme: this[prop].stats.rme,
|
||||
sem: this[prop].stats.sem
|
||||
}
|
||||
results.push(eachResult);
|
||||
}
|
||||
}
|
||||
|
||||
results = results.sort((a, b) => { return a.mean - b.mean });
|
||||
|
||||
console.log(`Rand\t${'Name'.padEnd(20)}Time (in milliseconds)`);
|
||||
let id = 1;
|
||||
|
||||
for (let r of results) {
|
||||
console.log(`${id++}\t${r.name.padEnd(20)}${r.mean.toFixed(3)}`);
|
||||
}
|
||||
})
|
||||
.run({ async: true });
|
||||
@@ -1,7 +1,7 @@
|
||||
// This test is used for tesing of a static function `create` of IP2Region
|
||||
const IP2Region = require('../ip2region');
|
||||
const testIps = require('./utils/testData');
|
||||
const asyncFor = require('./utils/asyncFor');
|
||||
const IP2Region = require('../../ip2region');
|
||||
const testIps = require('../utils/testData');
|
||||
const asyncFor = require('../utils/asyncFor');
|
||||
|
||||
describe('Constructor Test', () => {
|
||||
let instance;
|
||||
@@ -26,6 +26,11 @@ describe('Constructor Test', () => {
|
||||
}
|
||||
});
|
||||
|
||||
test('memorySearchSync query', () => {
|
||||
for (const ip of testIps) {
|
||||
expect(instance.memorySearchSync(ip)).toMatchSnapshot();
|
||||
}
|
||||
});
|
||||
|
||||
//#region callBack
|
||||
test('binarySearch query', (done) => {
|
||||
@@ -52,10 +57,22 @@ describe('Constructor Test', () => {
|
||||
() => { done() });
|
||||
});
|
||||
|
||||
test('memorySearch query', (done) => {
|
||||
asyncFor(testIps,
|
||||
(value, continueCallBack) => {
|
||||
instance.memorySearch(value, (err, result) => {
|
||||
expect(err).toBe(null);
|
||||
expect(result).toMatchSnapshot();
|
||||
continueCallBack();
|
||||
});
|
||||
},
|
||||
() => { done() });
|
||||
});
|
||||
|
||||
//#endregion
|
||||
|
||||
//#region Async Promisify test
|
||||
const node_ver = require('./utils/fetchMainVersion');
|
||||
const node_ver = require('../utils/fetchMainVersion');
|
||||
|
||||
// If we have Nodejs >= 8, we now support `async` and `await`
|
||||
if (node_ver >= 8) {
|
||||
@@ -90,6 +107,19 @@ describe('Constructor Test', () => {
|
||||
|
||||
};
|
||||
|
||||
const asyncMemorySearch = async (ip) => {
|
||||
return new Promise((succ, fail) => {
|
||||
instance.memorySearch(ip, (err, result) => {
|
||||
if (err) {
|
||||
fail(err);
|
||||
}
|
||||
else {
|
||||
succ(result);
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
test('async binarySearch query', async () => {
|
||||
for (let i = 0; i < testIps.length; ++i) {
|
||||
const result = await asyncBinarySearch(testIps[i]);
|
||||
@@ -103,6 +133,13 @@ describe('Constructor Test', () => {
|
||||
expect(result).toMatchSnapshot();
|
||||
}
|
||||
});
|
||||
|
||||
test('async memorySearch query', async () => {
|
||||
for (let i = 0; i < testIps.length; ++i) {
|
||||
const result = await asyncMemorySearch(testIps[i]);
|
||||
expect(result).toMatchSnapshot();
|
||||
}
|
||||
});
|
||||
}
|
||||
//#endregion
|
||||
});
|
||||
@@ -1,7 +1,7 @@
|
||||
// This test is used for tesing of a static function `create` of IP2Region
|
||||
const IP2Region = require('../ip2region');
|
||||
const testIps = require('./utils/testData');
|
||||
const asyncFor = require('./utils/asyncFor');
|
||||
const IP2Region = require('../../ip2region');
|
||||
const testIps = require('../utils/testData');
|
||||
const asyncFor = require('../utils/asyncFor');
|
||||
|
||||
describe('Create Test', () => {
|
||||
let instance;
|
||||
@@ -26,6 +26,11 @@ describe('Create Test', () => {
|
||||
}
|
||||
});
|
||||
|
||||
test('memorySearchSync query', () => {
|
||||
for (const ip of testIps) {
|
||||
expect(instance.memorySearchSync(ip)).toMatchSnapshot();
|
||||
}
|
||||
});
|
||||
|
||||
//#region callBack
|
||||
test('binarySearch query', (done) => {
|
||||
@@ -52,10 +57,23 @@ describe('Create Test', () => {
|
||||
() => { done() });
|
||||
});
|
||||
|
||||
test('memorySearch query', (done) => {
|
||||
asyncFor(testIps,
|
||||
(value, continueCallBack) => {
|
||||
instance.memorySearch(value, (err, result) => {
|
||||
expect(err).toBe(null);
|
||||
expect(result).toMatchSnapshot();
|
||||
continueCallBack();
|
||||
});
|
||||
},
|
||||
() => { done() });
|
||||
});
|
||||
|
||||
|
||||
//#endregion
|
||||
|
||||
//#region Async Promisify test
|
||||
const node_ver = require('./utils/fetchMainVersion');
|
||||
const node_ver = require('../utils/fetchMainVersion');
|
||||
|
||||
// If we have Nodejs >= 8, we now support `async` and `await`
|
||||
if (node_ver >= 8) {
|
||||
@@ -90,6 +108,19 @@ describe('Create Test', () => {
|
||||
|
||||
};
|
||||
|
||||
const asyncMemorySearch = async (ip) => {
|
||||
return new Promise((succ, fail) => {
|
||||
instance.memorySearch(ip, (err, result) => {
|
||||
if (err) {
|
||||
fail(err);
|
||||
}
|
||||
else {
|
||||
succ(result);
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
test('async binarySearch query', async () => {
|
||||
for (let i = 0; i < testIps.length; ++i) {
|
||||
const result = await asyncBinarySearch(testIps[i]);
|
||||
@@ -103,6 +134,13 @@ describe('Create Test', () => {
|
||||
expect(result).toMatchSnapshot();
|
||||
}
|
||||
});
|
||||
|
||||
test('async memorySearch query', async () => {
|
||||
for (let i = 0; i < testIps.length; ++i) {
|
||||
const result = await asyncMemorySearch(testIps[i]);
|
||||
expect(result).toMatchSnapshot();
|
||||
}
|
||||
});
|
||||
}
|
||||
//#endregion
|
||||
});
|
||||
@@ -1,6 +1,6 @@
|
||||
// This test is used for tesing of exceptions
|
||||
|
||||
const IP2Region = require('../ip2region');
|
||||
const IP2Region = require('../../ip2region');
|
||||
|
||||
describe('Constructor Test', () => {
|
||||
let instance;
|
||||
Reference in New Issue
Block a user