-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest-secondary.js
161 lines (143 loc) · 3.71 KB
/
test-secondary.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
const PouchDB = require('pouchdb');
var dbName = 'test-secondary',
nRun = 25,
nDocs = 50,
deleteDB = true,
status = [
'Lilla Torget', 'Västlänken', 'Kruthusgatan'
],
label = [
'Lilla Torget', 'Västlänken', 'Kruthusgatan',
'Lilla Torget', 'Västlänken', 'Kruthusgatan',
'Lilla Torget', 'Västlänken', 'Kruthusgatan',
'Lilla Torget', 'Västlänken', 'Kruthusgatan',
'Lilla Torget', 'Västlänken', 'Kruthusgatan',
],
machine = [
'213'
],
targetStatusCount = 300,
targetStatus = 'active',
targetLabelCount = 3000,
targetLabel = 'active',
targetMachineCount = 30000,
targetMachine = '123',
db = null;
function setUp() {
db = new PouchDB({name:dbName});
var ddoc = {
_id: '_design/bill',
views: {
by_status: {
map: function(doc) { emit(doc.status); }.toString()
},
by_label: {
map: function(doc) { emit(doc.label); }.toString()
},
by_machine: {
map: function(doc) { emit(doc.machine); }.toString()
},
}
}
db.put(ddoc);
}
function tearDown() {
db.info().then(info => console.log(info))
if(deleteDB) {
db.destroy(function(err, info) {
if (err) {
console.log(err);
}
});
}
}
function time(fn, times) {
var i,
start = Date.now(),
result;
function logStats() {
var t = Date.now() - start;
console.log(fn.name + ': ' + t + ' ms, ' + (t / times) + ' ms/run.');
}
result = new Promise(function(resolve, reject) {
function next(i) {
if (i < times) {
fn(i).
then(function() { next(i + 1); }).
catch(reject);
} else {
logStats();
resolve();
}
}
next(0);
});
return result;
}
function createDocuments(i) {
var activities = [
'Planering', 'Möte', 'Sprängning', 'Grävarbete',
'Transport'
],
descriptions = [
'', '', '', 'Kunden galen', 'Grus i maskineriet', 'OBS! Fakturera först nästa månad'
];
targetStatusCount -= 1;
targetLabelCount -= 1;
targetMachineCount -= 1;
return db.post({
type: 'entry',
activity: activities[i % activities.length],
status: targetStatusCount > 0 ? targetStatus : status[i % status.length],
label: targetLabelCount > 0 ? targetLabel : status[i % label.length],
machine: targetMachineCount > 0 ? targetMachine : status[i % machine.length],
description: descriptions[i % descriptions.length],
})
}
function query(index, key, i) {
return new Promise((resolve) => {
db.query(index, {
key: key,
}).
then((result) => {
resolve()
});
})
}
function queryStatus(i) {
return query('bill/by_status', targetStatus, i)
}
function queryLabel(i) {
return query('bill/by_label', targetLabel, i)
}
function queryMachine(i) {
return query('bill/by_machine', targetMachine, i)
}
function prepare(i) {
return new Promise((resolve) => {
time(createDocuments, nDocs).
then(() => { return time(queryStatus, 1) }).
then(() => { return time(queryLabel, 1) }).
then(() => { return time(queryMachine, 1) }).
then(() => { resolve(); });
});
}
function run(i) {
return new Promise((resolve) => {
createDocuments(1).
then(() => { return time(queryStatus, 1) }).
then(() => { return time(queryLabel, 1) }).
then(() => { return time(queryMachine, 1) }).
then(() => { resolve(); });
});
}
console.log('Pouch-Couch Benchmark');
console.log('=====================');
console.log(dbName);
console.log(`nRun: ${nRun}, nDocs: ${nDocs}`);
setUp();
Promise.resolve().
// then(function() { return time(createWithIndex, nDocs); }).
then(function() { return time(run, nRun); }).
then(tearDown).
catch(function(e) { console.log(e); tearDown(); throw e });