-
Notifications
You must be signed in to change notification settings - Fork 2
/
test.js
99 lines (93 loc) · 2.3 KB
/
test.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
var JsonDB = require('./index').JsonDB;
var async = require('async');
var db;
var limit = 5;
async.series([
function(callback) {
db = new JsonDB('./test.db', function(err) {
console.log('db opened');
callback(err);
});
},
function(callback) {
db.initialize('obj', [{a: 'int', c: 'text'}], function(err) {
if (err) {
console.error('Error initializing db', err);
}
else {
console.log('done initializing');
}
callback(err);
});
},
function(callback) {
console.log('saving ...');
var val = 1;
var sum = 0;
async.until(
function() { return val > limit; },
function(ucb) {
var start = Date.now();
db.save({ a: val, b: val, c: 'aaa' + val.toString(), lat: 37.11111111111, lon: 44.44444444444 }, 'obj', function(err) {
ucb(err);
sum += Date.now() - start;
val++;
});
},
function(err) {
console.log('avg save time', sum/(val - 1));
callback(err);
}
);
},
function(callback) {
console.log('finding without index ...');
var count = 0;
var sum = 0;
async.until(
function() { return count > limit; },
function(ucb) {
var start = Date.now();
db.find('obj', { b: 5 }, function(err, items) {
if (items[0].b !== 5) throw new Error('item not found');
sum += Date.now() - start;
ucb(err);
});
count++;
},
function(err) {
console.log('avg find time', sum/count);
callback(err);
}
);
},
function(callback) {
console.log('finding with index ...');
var count = 0;
var sum = 0;
async.until(
function() { return count > limit; },
function(ucb) {
var start = Date.now();
db.find('obj', { 'a': 5, 'c>=': 'a5' }, function(err, items) {
if (items[0].a !== 5) throw new Error('item not found');
sum += Date.now() - start;
ucb(err);
});
count++;
},
function(err) {
console.log('avg find time', sum/count);
callback(err);
}
);
},
function(callback) {
db.close(function(err) {
console.log('db closed', err);
callback(err);
});
},
], function(err) {
console.log('error', err);
});