forked from ether/ueberDB
-
Notifications
You must be signed in to change notification settings - Fork 0
/
benchmark.js
369 lines (319 loc) · 8.91 KB
/
benchmark.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
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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
/**
* 2011 Peter 'Pita' Martischka
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS-IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
var async = require("async");
var ueberDB = require("./CloneAndAtomicLayer");
//settings
var maxValues = 100000;
var writes = 210;
var reads = 100;
var updates = 20;
var deletes = 10;
var initialWrites = writes-deletes;
var valueLength = 1000;
var randomized = true;
var db;
//counts how man keys are in the db
var counter = 0;
var timeSum = null;
var randomStrings = [];
var valueMap = {};
//the default settings for benchmarking
var bench_settings = require("./defaultTestSettings.js");
if(process.argv.length == 3)
{
console.log("test");
var settings = bench_settings[process.argv[2]];
db = new ueberDB.database(process.argv[2], settings);
benchmark(function(err, time)
{
if(err) console.error(err.stack ? err.stack : err);
console.log("finished");
console.log("time: " + time + "s");
process.exit(0);
})
}
//the benchmark function
function benchmark(callback)
{
var startTime = new Date().getTime();
console.log("generating random data, to fill database...");
for(var i=0;i<1000;i++)
{
randomStrings.push(randomString(valueLength));
}
console.log("done");
async.series([
//init db
function(callback)
{
console.log("initalize database...");
db.init(callback);
},
//fill the database with the inital values
function(callback)
{
console.log("done");
console.log("do the first writings to fill the database...");
doOperations(generateOperations(counter, counter + initialWrites, initialWrites, "write"), false, callback);
//increment counter
counter+=initialWrites;
},
function(callback)
{
console.log("done");
console.error("values;read_time;write_time;update_time;delete_time;memory_mb");
var round = 1;
//async while loop
async.whilst(
function () { return counter < maxValues; },
function (callback)
{
if(round%50 == 0)
{
console.log("persistence test");
persistenceTest(callback);
round++;
return;
}
else
{
if(timeSum != null)
{
var readTime = Math.round(timeSum["read"]/reads);
var writeTime = Math.round(timeSum["write"]/writes);
var updateTime = Math.round(timeSum["update"]/updates);
var deleteTime = Math.round(timeSum["delete"]/deletes);
var memoryUsage = Math.round(process.memoryUsage().heapUsed/1024/1024);
var csvLine = counter + ";" + readTime + ";" + writeTime + ";" + updateTime + ";" + deleteTime + ";" + memoryUsage;
console.error(csvLine);
console.log("rows: " + counter);
//increment counter
counter+=writes-deletes;
}
timeSum = {"read":0, "write":0,"update":0,"delete":0};
var writeOps = generateOperations(counter, counter + writes, writes, "write");
var readOps = generateOperations(0, counter, reads, "read");
var updateOps = generateOperations(0, counter, updates, "update");
var deleteOps = generateOperations(0, counter, deletes, "delete");
if(randomized)
{
//put all operations together and shuffle them
var operations = writeOps.concat(readOps,updateOps,deleteOps).sort(function() {return 0.5 - Math.random()});
//execute the operations
doOperations(operations, true, callback);
}
else
{
async.waterfall([
function(callback)
{
console.error("write");
doOperations(writeOps, true, callback);
},
function(callback)
{
console.error("read");
doOperations(readOps, true, callback);
},
function(callback)
{
console.error("update");
doOperations(updateOps, true, callback);
},
function(callback)
{
console.error("delete");
doOperations(deleteOps, true, callback);
},
],callback);
}
}
round++;
},callback);
}
], function(err)
{
db.close(function()
{
var time = Math.round((new Date().getTime()-startTime)/1000);
callback(err, time);
});
});
}
function generateOperations(startNum, endNum, size, type)
{
var operations = [];
var keyNum = startNum;
for(var i=0;i<size;i++)
{
var key;
if(type == "write")
{
key = "key" + keyNum;
keyNum++;
}
else
{
key = "key" + (startNum + uniqueRandomNum(endNum-startNum))
}
var operation = {type:type, key:key, callback:function(){}};
if(type == "write" || type == "update")
{
var StringNum = Math.floor(Math.random() * 1000);
operation.value = randomStrings[StringNum];
valueMap[key] = StringNum;
}
if(type == "delete")
{
valueMap[key] = null;
}
operations.push(operation);
}
return operations;
}
function doOperations(operations, measure, callback)
{
async.forEach(operations, function(item, _callback)
{
//slow down the callback, this ensures the db gets time write the values
var callback = function(err)
{
setTimeout(_callback, 1, err);
};
//if write or update, call set
if(item.type == "write" || item.type == "update")
{
if(measure)
{
measureTime(function(callback)
{
db.set(item.key, item.value, null, callback);
},item.type, callback);
}
else
{
db.set(item.key, item.value, null, callback);
}
}
else if(item.type == "read")
{
if(measure)
{
measureTime(function(callback){
db.get(item.key, callback);
},item.type, callback);
}
else
{
db.get(item.key, callback);
}
}
else if(item.type == "delete")
{
if(measure)
{
measureTime(function(callback){
db.remove(item.key, callback);
},item.type, callback);
}
else
{
db.remove(item.key, callback);
}
}
else
{
callback("Unkown Operation!");
}
},function(err)
{
callback(err);
});
}
function persistenceTest(callback)
{
//get all keys in a array
var keys = [];
for(var i in valueMap)
{
keys.push(i);
}
//test for inconsistence
async.forEach(keys, function(item, callback)
{
db.get(item, function(err, value)
{
if(!err)
{
if(value != randomStrings[valueMap[item]])
{
err = "Inconsistent key : '" + item + "'\n";
err+= "is : '" + value + "'\n";
err+= "should be : '" + randomStrings[valueMap[item]] + "'";
}
}
callback(err);
});
}, callback);
}
function measureTime(func, type, callback){
var startTime = new Date().getTime();
func(function(err){
var time = new Date().getTime()-startTime;
timeSum[type]+=time;
callback(err);
});
}
var lastRands = [];
// This Function ensures that the generated random values for the
// deletes/updates are never the same in one round
function uniqueRandomNum(maxNum)
{
var rand;
var isUnique = false;
while(!isUnique)
{
rand = Math.floor(Math.random() * maxNum);
isUnique = true;
//try to proof that its not unique
for(var i in lastRands)
{
if(lastRands[i] == rand)
{
isUnique = false;
break;
}
}
}
lastRands.push(rand);
//remove the first element if lastRands is too long
if(lastRands.length > (reads+updates+deletes)*2)
{
lastRands.shift();
}
return rand;
}
function randomString(string_length)
{
var chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghiklmnopqrstuvwxyz";
var randomstring = '';
for (var i=0; i<string_length; i++)
{
var rnum = Math.floor(Math.random() * chars.length);
randomstring += chars.substring(rnum,rnum+1);
}
return randomstring;
}