-
Notifications
You must be signed in to change notification settings - Fork 63
Expand file tree
/
Copy pathbenchmark.js
More file actions
241 lines (212 loc) · 6.46 KB
/
benchmark.js
File metadata and controls
241 lines (212 loc) · 6.46 KB
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
const farmhash = require('farmhash');
const randomIpv6 = require('random-ipv6');
function random_ip() {
return Math.round(Math.random()*255) + '.' +
Math.round(Math.random()*255) + '.' +
Math.round(Math.random()*255) + '.' +
Math.round(Math.random()*255);
}
// Hash IP address using "Int31" algorithm from sticky-sessions.
function hash_int31(ip, seed) {
var hash = ip.reduce(function(r, num) {
r += parseInt(num, 10);
r %= 2147483648;
r += (r << 10)
r %= 2147483648;
r ^= r >> 6;
return r;
}, seed);
hash += hash << 3;
hash %= 2147483648;
hash ^= hash >> 11;
hash += hash << 15;
hash %= 2147483648;
return hash >>> 0;
}
// Convert IP address to true decimal representation.
function hash_numeric_real(dot) {
var d = dot.split('.');
return ((((((+d[0])*256)+(+d[1]))*256)+(+d[2]))*256)+(+d[3]);
}
// Simple numeric representation, using a regex.
function hash_simple_regex(ip) {
return Number(ip.replace(/\./g, ''));
};
// Simple numeric representation, using a loop.
function hash_simple_loop(ip) {
var s = '';
for (var i = 0, _len = ip.length; i < _len; i++) {
if (!isNaN(ip[i])) {
s += ip[i];
}
}
return Number(s);
};
// IPv4
//
// Generate a lot of random IPs.
var num_ips = 1000000,
ipsv4 = [];
ipsv6 = [];
console.log('Generating ' + num_ips + ' IPs (IPv4)...');
for (var i = 0; i < num_ips; i++) {
ipsv4[i] = random_ip();
}
console.log('Generating ' + num_ips + ' IPs (IPv6)...');
for (var i = 0; i < num_ips; i++) {
ipsv6[i] = randomIpv6();
}
console.log('');
// How many slots are available to us.
var len = parseInt(process.argv[2]);
// Random seed for "int31" hashing. Done once so not taken in account.
var seed = ~~(Math.random() * 1e9);
// Scattering results and timing.
var scatter_int31 = {},
scatter_numeric_real = {},
scatter_numeric_simple_regex = {},
scatter_numeric_simple_loop = {},
scatter_numeric_farmhash = {};
var t1, t2;
console.log('IPv4');
console.log('----------')
console.log('benchmarking int31...');
t1 = new Date();
for (var i = 0; i < ipsv4.length; i++) {
var index = hash_int31((ipsv4[i] || '').split(/\./g), seed) % len;
if (!scatter_int31.hasOwnProperty(index)) {
scatter_int31[index] = 1;
} else {
scatter_int31[index]++;
}
}
t2 = new Date();
console.log(' time (ms):', t2 - t1);
console.log(' scatter:', scatter_int31);
console.log('benchmarking numeric_real...');
t1 = new Date();
for (var i = 0; i < ipsv4.length; i++) {
var index = hash_numeric_real(ipsv4[i]) % len;
if (!scatter_numeric_real.hasOwnProperty(index)) {
scatter_numeric_real[index] = 1;
} else {
scatter_numeric_real[index]++;
}
}
t2 = new Date();
console.log(' time (ms):', t2 - t1);
console.log(' scatter:', scatter_numeric_real);
console.log('benchmarking simple_regex...');
t1 = new Date();
for (var i = 0; i < ipsv4.length; i++) {
var index = hash_simple_regex(ipsv4[i]) % len;
if (!scatter_numeric_simple_regex.hasOwnProperty(index)) {
scatter_numeric_simple_regex[index] = 1;
} else {
scatter_numeric_simple_regex[index]++;
}
}
t2 = new Date();
console.log(' time (ms):', t2 - t1);
console.log(' scatter:', scatter_numeric_simple_regex);
console.log('benchmarking simple_loop...');
t1 = new Date();
for (var i = 0; i < ipsv4.length; i++) {
var index = hash_simple_loop(ipsv4[i]) % len;
if (!scatter_numeric_simple_loop.hasOwnProperty(index)) {
scatter_numeric_simple_loop[index] = 1;
} else {
scatter_numeric_simple_loop[index]++;
}
}
t2 = new Date();
console.log(' time (ms):', t2 - t1);
console.log(' scatter:', scatter_numeric_simple_loop);
console.log('benchmarking farmhash...');
t1 = new Date();
for (var i = 0; i < ipsv4.length; i++) {
var index = farmhash.fingerprint32(ipsv4[i]) % len;
if (!scatter_numeric_farmhash.hasOwnProperty(index)) {
scatter_numeric_farmhash[index] = 1;
} else {
scatter_numeric_farmhash[index]++;
}
}
t2 = new Date();
console.log(' time (ms):', t2 - t1);
console.log(' scatter:', scatter_numeric_farmhash);
console.log('\n')
// IPv6
//
scatter_int31 = {},
scatter_numeric_real = {},
scatter_numeric_simple_regex = {},
scatter_numeric_simple_loop = {},
scatter_numeric_farmhash = {};
console.log('IPv6');
console.log('----------');
console.log('benchmarking int31...');
t1 = new Date();
for (var i = 0; i < ipsv6.length; i++) {
var index = hash_int31((ipsv6[i] || '').split(/\./g), seed) % len;
if (!scatter_int31.hasOwnProperty(index)) {
scatter_int31[index] = 1;
} else {
scatter_int31[index]++;
}
}
t2 = new Date();
console.log(' time (ms):', t2 - t1);
console.log(' scatter:', scatter_int31);
console.log('benchmarking numeric_real...');
t1 = new Date();
for (var i = 0; i < ipsv6.length; i++) {
var index = hash_numeric_real(ipsv6[i]) % len;
if (!scatter_numeric_real.hasOwnProperty(index)) {
scatter_numeric_real[index] = 1;
} else {
scatter_numeric_real[index]++;
}
}
t2 = new Date();
console.log(' time (ms):', t2 - t1);
console.log(' scatter:', scatter_numeric_real);
console.log('benchmarking simple_regex...');
t1 = new Date();
for (var i = 0; i < ipsv6.length; i++) {
var index = hash_simple_regex(ipsv6[i]) % len;
if (!scatter_numeric_simple_regex.hasOwnProperty(index)) {
scatter_numeric_simple_regex[index] = 1;
} else {
scatter_numeric_simple_regex[index]++;
}
}
t2 = new Date();
console.log(' time (ms):', t2 - t1);
console.log(' scatter:', scatter_numeric_simple_regex);
console.log('benchmarking simple_loop...');
t1 = new Date();
for (var i = 0; i < ipsv6.length; i++) {
var index = hash_simple_loop(ipsv6[i]) % len;
if (!scatter_numeric_simple_loop.hasOwnProperty(index)) {
scatter_numeric_simple_loop[index] = 1;
} else {
scatter_numeric_simple_loop[index]++;
}
}
t2 = new Date();
console.log(' time (ms):', t2 - t1);
console.log(' scatter:', scatter_numeric_simple_loop);
console.log('benchmarking farmhash...');
t1 = new Date();
for (var i = 0; i < ipsv6.length; i++) {
var index = farmhash.fingerprint32(ipsv6[i]) % len;
if (!scatter_numeric_farmhash.hasOwnProperty(index)) {
scatter_numeric_farmhash[index] = 1;
} else {
scatter_numeric_farmhash[index]++;
}
}
t2 = new Date();
console.log(' time (ms):', t2 - t1);
console.log(' scatter:', scatter_numeric_farmhash);