-
Notifications
You must be signed in to change notification settings - Fork 4
/
bench.js
176 lines (145 loc) · 4.33 KB
/
bench.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
#!/usr/bin/env node
var ben = require('ben')
, Location = require('..')
, assert = require('assert')
, dropin = require('../dropin')
try {
var ip2loc = require('ip2location-nodejs')
} catch(e) {
console.log("missing: ip2location-nodejs");
}
if (process.argv.length < 3) {
console.log('usage: node ' +
require('path').basename(process.argv[1]) +
' IP2LOCATION-DATABASE.BIN access_mode iterations mask\n\n' +
' access_mode: file|cache|mmap|shared|/shared_name');
process.exit(1);
}
function parseMask(arg) {
if (arg) {
if ((/^(?:0x)?\d+$/).test(arg)) {
return parseInt(arg);
} else {
return arg.toUpperCase().split(/[|,+-.]+/).reduce(function(mask, name) {
if (name in Location) {
return mask | Location[name];
} else {
throw new Error("Unknown mask name: " + name);
}
}, 0);
}
}
return Location.ALL;
}
var file = process.argv[2]
, mode = process.argv[3] || 'file'
, iter = (process.argv[4]|0) || 10000
, mask = parseMask(process.argv[5])
, location = new Location(file, mode)
;
var random = Math.random;
function randip() {
return [random()*100&255,random()*256&255,random()*256&255,random()*256&255].join('.');
}
var ip4buff = new Buffer(4);
function randipbin() {
ip4buff[0] = random()*100&255;
ip4buff[1] = random()*256&255;
ip4buff[2] = random()*256&255;
ip4buff[3] = random()*256&255;
return ip4buff;
}
function randipv6() {
return ["2A04",
(random()*0x10000|0+0x10000).toString(16).substr(1),
(random()*0x10000|0+0x10000).toString(16).substr(1),
(random()*0x10000|0+0x10000).toString(16).substr(1),
'',
(random()*0x10000|0+0x10000).toString(16).substr(1)].join(":");
}
var ip6buff = new Buffer(16);
ip6buff.fill(0);
ip6buff[0] = 0x2a;
ip6buff[1] = 0x04;
function randipv6bin() {
ip6buff.writeUInt16BE(random()*0x10000|0, 2);
ip6buff.writeUInt16BE(random()*0x10000|0, 4);
ip6buff.writeUInt16BE(random()*0x10000|0, 6);
ip6buff.writeUInt16BE(random()*0x10000|0, 14);
return ip6buff;
}
var hasipv6 = location.ipv6;
function test(testfun, nobin) {
console.log("ipv4 test x " + iter + " times");
var ms = ben(iter, function() {
testfun(randip());
});
console.log('query/ms: ' + (1 / ms).toFixed(4));
console.log('query in: ' + ms + 'ms');
if (!nobin) {
console.log("\nipv4bin test x " + iter + " times");
var ms = ben(iter, function() {
testfun(randipbin());
});
console.log('query/ms: ' + (1 / ms).toFixed(4));
console.log('query in: ' + ms + 'ms');
}
if (hasipv6) {
console.log("\nipv6 test x " + iter + " times");
ms = ben(iter, function() {
testfun(randipv6());
});
console.log('query/ms: ' + (1 / ms).toFixed(4));
console.log('query in: ' + ms + 'ms');
if (!nobin) {
console.log("\nipv6bin test x " + iter + " times");
ms = ben(iter, function() {
testfun(randipv6bin());
});
console.log('query/ms: ' + (1 / ms).toFixed(4));
console.log('query in: ' + ms + 'ms');
}
}
}
console.log(location);
console.log(location.info());
var ip = randip();
console.log(ip);
console.log(location.query(ip, mask));
ip = randipbin();
console.log(ip);
console.log(location.query(ip, mask));
if (location.ipv6) {
ip = randipv6();
console.log(ip);
console.log(location.query(ip, mask));
ip = randipv6bin();
console.log(ip);
console.log(location.query(ip, mask));
}
console.log('-------------------');
console.log("pid: " + process.pid);
console.log("mask: 0x" + (mask|0x100000).toString(16).substr(1));
console.log('-------------------');
console.log("ip2location native:");
test(function(ip) { location.query(ip, mask); });
location.close();
console.log();
dropin.IP2Location_init(file, mode);
console.log('-------------------');
console.log("ip2location dropin:");
console.log("\nIP2Location_get_all()");
test(dropin.IP2Location_get_all);
console.log("\nIP2Location_get_country_short()");
test(dropin.IP2Location_get_country_short);
dropin.IP2Location_init();
console.log();
if (ip2loc) {
ip2loc.IP2Location_init(file)
console.log('-------------------');
console.log("ip2location-nodejs:");
console.log("\nIP2Location_get_all()");
test(ip2loc.IP2Location_get_all, true);
console.log("\nIP2Location_get_country_short()");
test(ip2loc.IP2Location_get_country_short.bind(ip2loc), true);
}