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