-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest-relational.js
133 lines (118 loc) · 2.7 KB
/
test-relational.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
const PouchDB = require('pouchdb');
var dbName = 'test-relational',
nRun = 5
nDocs = 100,
deleteDB = true,
status = [
'Lilla Torget', 'Västlänken', 'Kruthusgatan'
],
db = null;
function setUp() {
db = new PouchDB({name:dbName});
PouchDB.plugin(require('relational-pouch'));
PouchDB.plugin(require('pouchdb-find'));
db.setSchema([
{
singular: 'entry',
plural: 'entries',
relations: {
status: {belongsTo: 'entryStatus'},
}
},
{
singular: 'entryStatus',
plural: 'entryStatuses',
relations: {
entries: {hasMany: {type: 'entry', options: {queryInverse: 'entry'}}}
}
},
]);
status.forEach((s) => {
db.rel.save('entryStatus', {
id: s,
});
})
}
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'
],
slot = [
'Skanska', 'Sweco', 'ÅF', 'Privat'
],
descriptions = [
'', '', '', 'Kunden galen', 'Grus i maskineriet', 'OBS! Fakturera först nästa månad'
];
const status = status[i % status.length];
return db.rel.save('entry', {
activity: activities[i % activities.length],
status: status,
slot: slot[i % slot.length],
time: (((i % 9) + '').replace(/,/g, '.') * 1),
description: descriptions[i % descriptions.length],
date: new Date(i * 86400)
}).then((result) => {
});
}
function query(i) {
return new Promise((resolve) => {
db.rel.find('entryStatus', status[0]).
then((result) => {
console.log(result);
resolve()
});
})
}
function run(i) {
return new Promise((resolve) => {
time(createDocuments, nDocs).
then(() => { return time(query, 1, true) }).
then(() => { return time(query, 1, true) }).
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(run, nRun); }).
then(tearDown).
catch(function(e) { console.log(e); tearDown(); throw e });