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 = "testCreateKeysInStores_" + numKeys + "_" + numStores + "_" + |
10 payloadLength; | 10 payloadLength; |
11 assert(numKeys >= 0); | 11 assert(numKeys >= 0); |
12 assert(numStores >= 1); | 12 assert(numStores >= 1); |
13 var objectStoreNames = []; | 13 var objectStoreNames = []; |
14 for (var i=0; i < numStores; ++i) { | 14 for (var i=0; i < numStores; ++i) { |
15 objectStoreNames.push("store " + i); | 15 objectStoreNames.push("store " + i); |
16 } | 16 } |
17 var value = stringOfLength(payloadLength); | 17 var value = stringOfLength(payloadLength); |
18 var start; | |
19 | 18 |
20 function onCreated(db) { | 19 function onCreated(db) { |
21 automation.setStatus("Constructing transaction."); | 20 automation.setStatus("Constructing transaction."); |
22 start = Date.now(); // Ignore the setup time for this test. | 21 var completionFunc = |
| 22 getCompletionFunc(testName, Date.now(), onTestComplete); |
23 var transaction = getTransaction(db, objectStoreNames, "readwrite", | 23 var transaction = getTransaction(db, objectStoreNames, "readwrite", |
24 function() { onTransactionComplete(db); }); | 24 function() { completionFunc(); }); |
25 for (var i in objectStoreNames) { | 25 for (var i in objectStoreNames) { |
26 var os = transaction.objectStore(objectStoreNames[i]); | 26 var os = transaction.objectStore(objectStoreNames[i]); |
27 assert(os); | 27 assert(os); |
28 for (var j = 0; j < numKeys; ++j) { | 28 for (var j = 0; j < numKeys; ++j) { |
29 os.put("key " + j, value); | 29 os.put(value, "key " + j); |
30 } | 30 } |
31 } | 31 } |
32 } | 32 } |
33 function onTransactionComplete(db) { | 33 automation.setStatus("Creating database."); |
34 var duration = Date.now() - start; | |
35 // Ignore the cleanup time for this test. | |
36 automation.addResult(testName, duration); | |
37 automation.setStatus("Deleting."); | |
38 deleteDatabase(db, onDeleted); | |
39 } | |
40 function onDeleted() { | |
41 automation.setStatus("Deleted."); | |
42 onTestComplete(); | |
43 } | |
44 automation.setStatus("Creating."); | |
45 createDatabase(testName, objectStoreNames, onCreated, onError); | 34 createDatabase(testName, objectStoreNames, onCreated, onError); |
46 } | 35 } |
47 | 36 |
| 37 function testRandomReads(numKeys, numReadsPerTransaction, numTransactions, |
| 38 useIndex, onTestComplete) { |
| 39 var indexText = "_bare"; |
| 40 var indexName; |
| 41 if (useIndex) { |
| 42 indexText = "_index"; |
| 43 indexName = "index"; |
| 44 } |
| 45 var testName = "testRandomReads_" + numKeys + "_" + numReadsPerTransaction + |
| 46 "_" + numTransactions + indexText; |
| 47 var numTransactionsLeft = numTransactions; |
| 48 var storeName = "store"; |
| 49 var objectStoreNames = [storeName]; |
| 50 var numTransactionsRunning; |
| 51 |
| 52 function getKey(i) { |
| 53 return "key " + i; |
| 54 } |
| 55 |
| 56 function getValue(i) { |
| 57 return "value " + i; |
| 58 } |
| 59 |
| 60 function onCreated(db) { |
| 61 automation.setStatus("Setting up test database."); |
| 62 var transaction = getTransaction(db, objectStoreNames, "readwrite", |
| 63 function() { onSetupComplete(db); }); |
| 64 var os = transaction.objectStore(storeName); |
| 65 assert(os); |
| 66 for (var j = 0; j < numKeys; ++j) { |
| 67 os.put(getValue(i), getKey(j)); |
| 68 } |
| 69 } |
| 70 var completionFunc; |
| 71 function onSetupComplete(db) { |
| 72 automation.setStatus("Setup complete."); |
| 73 runOneBatch(db); |
| 74 completionFunc = getCompletionFunc(testName, Date.now(), onTestComplete); |
| 75 } |
| 76 |
| 77 function runOneBatch(db) { |
| 78 if (numTransactionsLeft <= 0) { |
| 79 return; |
| 80 } |
| 81 --numTransactionsLeft; |
| 82 ++numTransactionsRunning; |
| 83 var valuesToRead = numReadsPerTransaction; |
| 84 var transaction = getTransaction(db, objectStoreNames, "readonly", |
| 85 function() { |
| 86 assert(!--numTransactionsRunning); |
| 87 assert(!valuesToRead); |
| 88 if (numTransactionsLeft <= 0) { |
| 89 completionFunc(); |
| 90 } else { |
| 91 runOneBatch(db); |
| 92 } |
| 93 }); |
| 94 |
| 95 var queryObject = transaction.objectStore(storeName); |
| 96 if (useIndex) { |
| 97 queryObject = queryObject.index(indexName); |
| 98 } |
| 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 } |
| 109 |
| 110 automation.setStatus("Creating database."); |
| 111 var options = {}; |
| 112 if (useIndex) { |
| 113 options.indexName = indexName; |
| 114 options.indexKeyPath = ""; |
| 115 options.indexIsUnique = true; |
| 116 options.indexIsMultiEntry = false; |
| 117 } |
| 118 createDatabase(testName, objectStoreNames, onCreated, onError, options); |
| 119 } |
| 120 |
| 121 var kUseIndex = true; |
| 122 var kDontUseIndex = false; |
| 123 |
48 var tests = [ | 124 var tests = [ |
49 [testCreateKeysInStores, 1, 1, 1], | 125 [testCreateKeysInStores, 1, 1, 1], |
50 [testCreateKeysInStores, 100, 1, 1], | 126 [testCreateKeysInStores, 100, 1, 1], |
51 [testCreateKeysInStores, 1, 100, 1], | 127 [testCreateKeysInStores, 1, 100, 1], |
52 [testCreateKeysInStores, 100, 1, 100000] | 128 [testCreateKeysInStores, 100, 1, 10000], |
| 129 [testRandomReads, 1000, 5, 50, kDontUseIndex], |
| 130 [testRandomReads, 1000, 50, 5, kDontUseIndex], |
| 131 [testRandomReads, 5000, 50, 5, kDontUseIndex], |
| 132 [testRandomReads, 1000, 5, 50, kUseIndex], |
| 133 [testRandomReads, 1000, 50, 5, kUseIndex], |
| 134 [testRandomReads, 5000, 50, 5, kUseIndex] |
53 ]; | 135 ]; |
54 | 136 |
55 var currentTest = 0; | 137 var currentTest = 0; |
56 | 138 |
57 function runNextTest() { | 139 function runNextTest() { |
58 if (currentTest < tests.length) { | 140 if (currentTest < tests.length) { |
59 var test = tests[currentTest++].slice(); | 141 var test = tests[currentTest++].slice(); |
60 var f = test.shift(); | 142 var f = test.shift(); |
61 test.push(runNextTest); | 143 test.push(runNextTest); |
62 f.apply(null, test); | 144 f.apply(null, test); |
63 } else { | 145 } else { |
64 onAllTestsComplete(); | 146 onAllTestsComplete(); |
65 } | 147 } |
66 } | 148 } |
67 | 149 |
68 function onAllTestsComplete() { | 150 function onAllTestsComplete() { |
69 var overallDuration = Date.now() - overallTestStartTime; | 151 var overallDuration = Date.now() - overallTestStartTime; |
70 automation.addResult("OverallTestDuration", overallDuration); | 152 automation.addResult("OverallTestDuration", overallDuration); |
71 automation.setDone(); | 153 automation.setDone(); |
72 } | 154 } |
73 | 155 |
74 function test() { | 156 function test() { |
75 runNextTest(); | 157 runNextTest(); |
76 } | 158 } |
OLD | NEW |