OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 var overallTestStartTime = Date.now(); | 5 var overallTestStartTime = Date.now(); |
6 | 6 |
7 function testCreateKeysInStores( | 7 function testCreateKeysInStores( |
8 numKeys, numStores, payloadLength, onTestComplete) { | 8 numKeys, numStores, payloadLength, onTestComplete) { |
9 var testName = "testCreateKeysInStores_" + numKeys + "_" + numStores + "_" + | 9 var testName = getDisplayName(arguments); |
10 payloadLength; | |
11 assert(numKeys >= 0); | 10 assert(numKeys >= 0); |
12 assert(numStores >= 1); | 11 assert(numStores >= 1); |
13 var objectStoreNames = []; | 12 var objectStoreNames = []; |
14 for (var i=0; i < numStores; ++i) { | 13 for (var i=0; i < numStores; ++i) { |
15 objectStoreNames.push("store " + i); | 14 objectStoreNames.push("store " + i); |
16 } | 15 } |
17 var value = stringOfLength(payloadLength); | 16 var value = stringOfLength(payloadLength); |
| 17 function getValue() { |
| 18 return value; |
| 19 } |
18 | 20 |
19 function onCreated(db) { | 21 function onCreated(db) { |
20 automation.setStatus("Constructing transaction."); | 22 automation.setStatus("Constructing transaction."); |
21 var cleanUpFunc = getCleanUpFunc(testName, Date.now(), onTestComplete); | 23 var cleanUpFunc = getCleanUpFunc(testName, Date.now(), onTestComplete); |
22 var transaction = getTransaction(db, objectStoreNames, "readwrite", | 24 var transaction = getTransaction(db, objectStoreNames, "readwrite", |
23 function() { cleanUpFunc(db); }); | 25 function() { cleanUpFunc(db); }); |
24 for (var i in objectStoreNames) { | 26 putLinearValues(transaction, objectStoreNames, numKeys, null, getValue); |
25 var os = transaction.objectStore(objectStoreNames[i]); | |
26 assert(os); | |
27 for (var j = 0; j < numKeys; ++j) { | |
28 os.put(value, "key " + j); | |
29 } | |
30 } | |
31 } | 27 } |
32 automation.setStatus("Creating database."); | 28 automation.setStatus("Creating database."); |
33 createDatabase(testName, objectStoreNames, onCreated, onError); | 29 createDatabase(testName, objectStoreNames, onCreated, onError); |
34 } | 30 } |
35 | 31 |
36 function testRandomReads(numKeys, numReadsPerTransaction, numTransactions, | 32 function testRandomReadsAndWrites( |
37 useIndex, onTestComplete) { | 33 numKeys, numReadsPerTransaction, numWritesPerTransaction, numTransactions, |
38 var indexText = "_bare"; | 34 useIndexForReads, onTestComplete) { |
39 var indexName; | 35 var indexName; |
40 if (useIndex) { | 36 if (useIndexForReads) |
41 indexText = "_index"; | |
42 indexName = "index"; | 37 indexName = "index"; |
43 } | 38 var testName = getDisplayName(arguments); |
44 var testName = "testRandomReads_" + numKeys + "_" + numReadsPerTransaction + | |
45 "_" + numTransactions + indexText; | |
46 var numTransactionsLeft = numTransactions; | 39 var numTransactionsLeft = numTransactions; |
47 var storeName = "store"; | 40 var objectStoreNames = ["store"]; |
48 var objectStoreNames = [storeName]; | |
49 var value = "test value"; | |
50 var numTransactionsRunning; | 41 var numTransactionsRunning; |
51 | 42 |
52 function getKey(i) { | |
53 return "key " + i; | |
54 } | |
55 | |
56 function onCreated(db) { | 43 function onCreated(db) { |
57 automation.setStatus("Setting up test database."); | 44 automation.setStatus("Setting up test database."); |
58 var transaction = getTransaction(db, objectStoreNames, "readwrite", | 45 var transaction = getTransaction(db, objectStoreNames, "readwrite", |
59 function() { onSetupComplete(db); }); | 46 function() { onSetupComplete(db); }); |
60 var os = transaction.objectStore(storeName); | 47 putLinearValues(transaction, objectStoreNames, numKeys, |
61 assert(os); | 48 function () { return "test value"; }); |
62 for (var j = 0; j < numKeys; ++j) { | |
63 os.put(value, getKey(j)); | |
64 } | |
65 } | 49 } |
66 var cleanUpFunc; | 50 var cleanUpFunc; |
67 function onSetupComplete(db) { | 51 function onSetupComplete(db) { |
68 automation.setStatus("Setup complete."); | 52 automation.setStatus("Setup complete."); |
69 runOneBatch(db); | 53 runOneBatch(db); |
70 cleanUpFunc = getCleanUpFunc(testName, Date.now(), onTestComplete); | 54 cleanUpFunc = getCleanUpFunc(testName, Date.now(), onTestComplete); |
71 } | 55 } |
72 | 56 |
73 function runOneBatch(db) { | 57 function runOneBatch(db) { |
74 if (numTransactionsLeft <= 0) { | 58 if (numTransactionsLeft <= 0) { |
75 return; | 59 return; |
76 } | 60 } |
77 --numTransactionsLeft; | 61 --numTransactionsLeft; |
78 ++numTransactionsRunning; | 62 ++numTransactionsRunning; |
79 var valuesToRead = numReadsPerTransaction; | 63 var mode = "readonly"; |
80 var transaction = getTransaction(db, objectStoreNames, "readonly", | 64 if (numWritesPerTransaction) |
| 65 mode = "readwrite"; |
| 66 var transaction = getTransaction(db, objectStoreNames, mode, |
81 function() { | 67 function() { |
82 assert(!--numTransactionsRunning); | 68 assert(!--numTransactionsRunning); |
83 assert(!valuesToRead); | |
84 if (numTransactionsLeft <= 0) { | 69 if (numTransactionsLeft <= 0) { |
85 cleanUpFunc(db); | 70 cleanUpFunc(db); |
86 } else { | 71 } else { |
87 runOneBatch(db); | 72 runOneBatch(db); |
88 } | 73 } |
89 }); | 74 }); |
90 | 75 |
91 var queryObject = transaction.objectStore(storeName); | 76 getRandomValues(transaction, objectStoreNames, numReadsPerTransaction, |
92 if (useIndex) { | 77 numKeys, indexName); |
93 queryObject = queryObject.index(indexName); | 78 putRandomValues(transaction, objectStoreNames, numWritesPerTransaction, |
94 } | 79 numKeys, null, function () { return "new value"; }); |
95 assert(queryObject); | |
96 for (var i = 0; i < numReadsPerTransaction; ++i) { | |
97 var rand = Math.floor(Math.random() * numKeys); | |
98 var request = queryObject.get(getKey(rand)); | |
99 request.onerror = onError; | |
100 request.onsuccess = function () { | |
101 assert(valuesToRead--); | |
102 } | |
103 } | |
104 } | 80 } |
105 | 81 |
106 automation.setStatus("Creating database."); | 82 automation.setStatus("Creating database."); |
107 var options = {}; | 83 var options = {}; |
108 if (useIndex) { | 84 if (useIndexForReads) { |
109 options.indexName = indexName; | 85 options.indexName = indexName; |
110 options.indexKeyPath = ""; | 86 options.indexKeyPath = ""; |
111 options.indexIsUnique = true; | 87 options.indexIsUnique = true; |
112 options.indexIsMultiEntry = false; | 88 options.indexIsMultiEntry = false; |
113 } | 89 } |
114 createDatabase(testName, objectStoreNames, onCreated, onError, options); | 90 createDatabase(testName, objectStoreNames, onCreated, onError, options); |
115 } | 91 } |
116 | 92 |
| 93 function testCreateAndDeleteIndex(numKeys, onTestComplete) { |
| 94 var testName = getDisplayName(arguments); |
| 95 var objectStoreNames = ["store"]; |
| 96 function getValue(i) { |
| 97 return { firstName: i + " first name", lastName: i + " last name" }; |
| 98 } |
| 99 |
| 100 var startTime; |
| 101 function onCreated(db) { |
| 102 automation.setStatus("Initializing data."); |
| 103 var transaction = getTransaction(db, objectStoreNames, "readwrite", |
| 104 function() { onPopulated(db); }); |
| 105 putLinearValues(transaction, objectStoreNames, numKeys, null, getValue); |
| 106 } |
| 107 |
| 108 function onPopulated(db) { |
| 109 db.close(); |
| 110 automation.setStatus("Building index."); |
| 111 startTime = Date.now(); |
| 112 var f = |
| 113 function(objectStore) { |
| 114 objectStore.createIndex("index", "firstName", {unique: true}); |
| 115 } |
| 116 alterObjectStores(testName, objectStoreNames, f, onIndexCreated, onError); |
| 117 } |
| 118 |
| 119 var cleanUpFunc; |
| 120 function onIndexCreated(db) { |
| 121 db.close(); |
| 122 var indexCreationCompleteTime = Date.now(); |
| 123 automation.addResult("testCreateIndex", |
| 124 indexCreationCompleteTime - startTime); |
| 125 cleanUpFunc = getCleanUpFunc("testDeleteIndex", |
| 126 indexCreationCompleteTime, onTestComplete); |
| 127 var f = |
| 128 function(objectStore) { |
| 129 objectStore.deleteIndex("index"); |
| 130 } |
| 131 automation.setStatus("Deleting index."); |
| 132 alterObjectStores(testName, objectStoreNames, f, cleanUpFunc, onError); |
| 133 } |
| 134 |
| 135 function onIndexDeleted(db) { |
| 136 cleanUpFunc(db); |
| 137 } |
| 138 |
| 139 automation.setStatus("Creating database."); |
| 140 createDatabase(testName, objectStoreNames, onCreated, onError); |
| 141 } |
| 142 |
117 var tests = [ | 143 var tests = [ |
118 [testCreateKeysInStores, 1, 1, 1], | 144 [testCreateKeysInStores, 1, 1, 1], |
119 [testCreateKeysInStores, 100, 1, 1], | 145 [testCreateKeysInStores, 100, 1, 1], |
120 [testCreateKeysInStores, 1, 100, 1], | 146 [testCreateKeysInStores, 1, 100, 1], |
121 [testCreateKeysInStores, 100, 1, 10000], | 147 [testCreateKeysInStores, 100, 1, 10000], |
122 [testRandomReads, 1000, 5, 50, false], | 148 [testRandomReadsAndWrites, 1000, 5, 0, 50, false], |
123 [testRandomReads, 1000, 50, 5, false], | 149 [testRandomReadsAndWrites, 1000, 50, 0, 5, false], |
124 [testRandomReads, 5000, 50, 5, false], | 150 [testRandomReadsAndWrites, 5000, 50, 0, 5, false], |
125 [testRandomReads, 1000, 5, 50, true], | 151 [testRandomReadsAndWrites, 1000, 5, 0, 50, true], |
126 [testRandomReads, 1000, 50, 5, true], | 152 [testRandomReadsAndWrites, 1000, 50, 0, 5, true], |
127 [testRandomReads, 5000, 50, 5, true] | 153 [testRandomReadsAndWrites, 5000, 50, 0, 5, true], |
| 154 [testRandomReadsAndWrites, 1000, 5, 5, 50, false], |
| 155 [testRandomReadsAndWrites, 1000, 5, 5, 50, true], |
| 156 [testCreateAndDeleteIndex, 1000] |
128 ]; | 157 ]; |
129 | 158 |
130 var currentTest = 0; | 159 var currentTest = 0; |
131 | 160 |
132 function runNextTest() { | 161 function runNextTest() { |
133 if (currentTest < tests.length) { | 162 if (currentTest < tests.length) { |
134 var test = tests[currentTest++].slice(); | 163 var test = tests[currentTest++].slice(); |
135 var f = test.shift(); | 164 var f = test.shift(); |
136 test.push(runNextTest); | 165 test.push(runNextTest); |
137 f.apply(null, test); | 166 f.apply(null, test); |
138 } else { | 167 } else { |
139 onAllTestsComplete(); | 168 onAllTestsComplete(); |
140 } | 169 } |
141 } | 170 } |
142 | 171 |
143 function onAllTestsComplete() { | 172 function onAllTestsComplete() { |
144 var overallDuration = Date.now() - overallTestStartTime; | 173 var overallDuration = Date.now() - overallTestStartTime; |
145 automation.addResult("OverallTestDuration", overallDuration); | 174 automation.addResult("OverallTestDuration", overallDuration); |
146 automation.setDone(); | 175 automation.setDone(); |
147 } | 176 } |
148 | 177 |
149 function test() { | 178 function test() { |
150 runNextTest(); | 179 runNextTest(); |
151 } | 180 } |
OLD | NEW |