/
benchmark_cached_key_map_random.js
117 lines (96 loc) · 3.53 KB
/
benchmark_cached_key_map_random.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
module.exports.run = function(benchmark, next) {
var assert = require('assert');
var sys = require('sys') || require('util');
var fs = require('fs');
var cached_key_map = require(__dirname + '/../lib/alfred/cached_key_map.js');
var random = require('../tools/random_generator');
var OBJECT_COUNT = 100000;
var file_path = __dirname + '/../tmp/cached_key_map_random_benchmark.alf';
try {
fs.unlinkSync(file_path);
} catch(excp) {
// do nothing
}
benchmark.start('open empty indexed key map');
cached_key_map.open(file_path, function(err, key_map) {
if (err) {
throw err;
}
benchmark.end();
var keys = [];
var key_count = 0;
benchmark.start('clear empty indexed key map');
key_map.clear(function(err) {
if (err) {
throw err;
}
benchmark.end();
benchmark.start('populate indexed key map', OBJECT_COUNT);
for (var i = 0; i < OBJECT_COUNT; i ++) {
(function(i) {
var value = random.createRandomObject();
var key = random.createRandomString(16);
keys.push(key);
key_map.put(key, value, function(err) {
if (err) {
throw err;
}
key_count ++;
if (key_count == OBJECT_COUNT) {
benchmark.end();
setTimeout(function() {
// test if we can retrieve all keys
key_map.end(function(err) {
if (err) {
throw err;
}
benchmark.start('reopen indexed key map with ' + OBJECT_COUNT + ' records');
cached_key_map.open(file_path, function(err, key_map) {
if (err) {
throw err;
}
benchmark.end();
var timeout = setTimeout(function() {
assert.ok(false, "timeout");
}, 30000);
var tested_keys = 0;
var going_for = OBJECT_COUNT * 3;
benchmark.start('cached_key_map.get', going_for);
var gets = 0;
for(var j = 0; j < going_for; j++) {
(function(i) {
var key = keys[Math.floor(Math.random() * keys.length)];
key_map.get(key, function(err, value) {
if (err) {
throw err;
}
gets ++;
if (gets == going_for) {
benchmark.end();
benchmark.start('ending key_map');
key_map.end(function(err) {
if (err) {
throw err;
}
benchmark.end();
clearTimeout(timeout);
benchmark.start('wait 10 seconds for gc to kick in');
setTimeout(function() {
benchmark.end();
next();
}, 10000);
});
}
});
})(i);
}
});
});
}, 2000);
}
});
})(i);
}
});
});
};