UNPKG

2.51 kBJavaScriptView Raw
1var PouchDB = require('pouchdb'),
2 pouch = new PouchDB('mydb', { adapter: 'memory' }),
3 arraySize = 10000, // how large of a dataset to generate
4 totalIterations = 60000, // how many times we search it
5 results = [],
6 gAsyncCount = 0,
7 startTime,
8 endTime,
9 isIndexed = false;;
10
11// not really using right now, if we need to time each op independently i might use this outside timing logic
12function genRandomVal()
13{
14 var text = "";
15 var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
16
17 for( var i=0; i < 20; i++ )
18 text += possible.charAt(Math.floor(Math.random() * possible.length));
19
20 return text;
21}
22
23function initializeDB() {
24 gAsyncCount = 0;
25
26 startTime = process.hrtime();
27
28 // nedb uses async callbacks so we will not time each operation but
29 // use globals to count when the last async op has finished
30 for (var idx=0; idx < arraySize; idx++) {
31 var v1 = '12345'; //genRandomVal();
32 var v2 = '23456'; //genRandomVal();
33
34 pouch.put({
35 _id: idx,
36 val: v1,
37 val2: v2,
38 val3: "more data 1234567890"
39 }, function (err, newDoc) { // Callback is optional
40 if (++gAsyncCount == arraySize) {
41 endTime = process.hrtime(startTime);
42 var totalMS = endTime[0] * 1e3 + endTime[1] / 1e6;
43 var rate = arraySize * 1000 / totalMS;
44 rate = rate.toFixed(2);
45 console.log("load (insert) : " + totalMS + "ms (" + rate + ") ops/s");
46
47 testperfFind();
48 }
49 });
50 }
51}
52
53// benchmark find() performance
54// called by initializedb once its last async insert is complete
55// pouchdb let's you provide your own key to use as id so we will just use
56// the (best case?) pouch.get() method with our random customIdx column as the _id column
57 function testperfFind() {
58 var loopIterations = totalIterations;
59
60 gAsyncCount = 0;
61
62 startTime = process.hrtime();
63
64 for (var idx=0; idx < loopIterations; idx++) {
65 var customidx = Math.floor(Math.random() * arraySize) + 1;
66
67 pouch.get(customidx, function (err, docs) {
68 ++gAsyncCount;
69
70 if (gAsyncCount == totalIterations) {
71 endTime = process.hrtime(startTime);
72 var totalMS = endTime[0] * 1e3 + endTime[1] / 1e6;
73 var rate = (isIndexed?totalIterations*200:totalIterations) * 1000 / totalMS;
74 rate = rate.toFixed(2);
75 console.log("find (indexed : " + isIndexed + ") : " + totalMS + "ms (" + rate + ") ops/s");
76 }
77 });
78 }
79}
80
81initializeDB();
82