/
3_TransactionRead.js
115 lines (112 loc) · 3.32 KB
/
3_TransactionRead.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
(function(s) {
s['Transaction based on Read'] = {
category: 'Transaction',
desc: 'Should read requests be queued in a single transaction, or will it be faster to open multiple transactions and read in batches',
_bTestKey: 'agt1YS1wcm9maWxlcnINCxIEVGVzdBjIjagUDA',
onStart: function(cb, status) {
$.indexedDB('IndexedDBPerf').deleteDatabase().done(function() {
$.indexedDB('IndexedDBPerf', {
'schema': {
'1': function(trans) {
trans.createObjectStore('Table1', {
'autoIncrement': true
});
}
}
}).done(function() {
var objStore = $.indexedDB('IndexedDBPerf').objectStore('Table1');
var added = 0;
for (var i = 0; i < 1000; i++) {
objStore.add(Faker.Helpers.createCard()).always(function() {
if (++added >= 999) {
cb();
}
status();
});
}
});
});
},
setup: function() {
var data = Faker.Helpers.createCard();
},
teardown: function() {
},
tests: {
'All Reads in One Transaction': function(deferred) {
var req = window.indexedDB.open('IndexedDBPerf');
var readCount = 0;
req.onsuccess = function() {
var db = req.result;
var transaction = db.transaction(['Table1'], 'readonly');
var objStore = transaction.objectStore('Table1');
for (var i = 0; i < 1000; i++) {
(function(i) {
var readReq = objStore.get(i);
readReq.onsuccess = readReq.onerror = function() {
if (++readCount >= 999) {
db.close()
deferred.resolve();
}
}
}(i));
}
};
req.onerror = function() {
throw "Could not open database";
}
},
'Group reads in transaction sets with 10 reads each': function(deferred) {
var req = window.indexedDB.open('IndexedDBPerf');
var readCount = 0;
req.onsuccess = function() {
var db = req.result;
//console.log("Database opened");
for (var i = 0; i < 10; i++) {
var transaction = db.transaction(['Table1'], 'readonly');
var objStore = transaction.objectStore('Table1');
for (var j = 0; j < 100; j++) {
(function(i) {
var readReq = objStore.get(i);
readReq.onsuccess = readReq.onerror = function() {
if (++readCount >= 999) {
db.close();
deferred.resolve();
}
}
}(i));
}
}
};
req.onerror = function() {
throw "Could not open database";
}
},
'Each read in its own transaction': function(deferred) {
var req = window.indexedDB.open('IndexedDBPerf');
var readCount = 0;
req.onsuccess = function() {
var db = req.result;
//console.log("Database opened");
for (var i = 0; i < 1000; i++) {
(function(i) {
var transaction = db.transaction(['Table1'], 'readonly');
var objStore = transaction.objectStore('Table1');
//console.log('Multiple transactions - Reading ', i)
var readReq = objStore.get(i);
readReq.onsuccess = readReq.onerror = function() {
if (++readCount >= 999) {
db.close();
deferred.resolve();
}
}
}(i));
}
};
req.onerror = function() {
throw "Could not open database";
}
}
}
};
}(window.IndexedDBPerf.suites));