Commit 9b791d5d authored by ericu@chromium.org's avatar ericu@chromium.org

Add a read cache test.

We could just add a parameter to a similar test, but I think most of them have enough parameters already, and this keeps it simpler.

BUG=137764


Review URL: https://chromiumcodereview.appspot.com/10854046

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@151735 0039d316-1c4b-4281-b951-d872f2087c98
parent 78d17234
......@@ -278,6 +278,10 @@ function putLinearValues(
}
}
function verifyResultNonNull(result) {
assert(result != null);
}
function getRandomValues(
transaction, objectStoreNames, numReads, numKeys, indexName, getKey) {
if (!getKey)
......@@ -291,6 +295,7 @@ function getRandomValues(
var rand = Math.floor(Math.random() * numKeys);
var request = source.get(getKey(rand));
request.onerror = onError;
request.onsuccess = verifyResultNonNull;
}
}
}
......@@ -311,6 +316,20 @@ function putRandomValues(
}
}
function getSpecificValues(transaction, objectStoreNames, indexName, keys) {
for (var i in objectStoreNames) {
var os = transaction.objectStore(objectStoreNames[i]);
var source = os;
if (indexName)
source = source.index(indexName);
for (var j = 0; j < keys.length; ++j) {
var request = source.get(keys[j]);
request.onerror = onError;
request.onsuccess = verifyResultNonNull;
}
}
}
// getKey should be deterministic, as we assume that a cursor that starts at
// getKey(X) and runs through getKey(X + K) has exactly K values available.
// This is annoying to guarantee generally when using an index, so we avoid both
......@@ -360,3 +379,30 @@ function getValuesFromCursor(
}
request.onerror = onError;
}
function runTransactionBatch(db, count, batchFunc, objectStoreNames, mode,
onComplete) {
var numTransactionsRunning = 0;
runOneBatch(db);
function runOneBatch(db) {
if (count <= 0) {
return;
}
--count;
++numTransactionsRunning;
var transaction = getTransaction(db, objectStoreNames, mode,
function() {
assert(!--numTransactionsRunning);
if (count <= 0) {
onComplete();
} else {
runOneBatch(db);
}
});
batchFunc(transaction);
}
}
......@@ -77,6 +77,14 @@ var tests = [
// Make large batches of random writes into a store with many indices, triggered
// by periodic setTimeout calls.
[testSporadicWrites, 500, 10],
// Make a small bunch of batches of reads of the same keys from an object store.
[testReadCache, 10, kDontUseIndex],
// Make a bunch of batches of reads of the same keys from an index.
[testReadCache, 50, kUseIndex],
// Make a small bunch of batches of reads of the same keys from an object store.
[testReadCache, 10, kDontUseIndex],
// Make a bunch of batches of reads of the same keys from an index.
[testReadCache, 50, kUseIndex],
// Create and delete an index on a store that already contains data [produces
// a timing result for each of creation and deletion].
[testCreateAndDeleteIndex, 5000]
......@@ -185,9 +193,7 @@ function testRandomReadsAndWrites(
if (useIndexForReads)
indexName = "index";
var testName = getDisplayName(arguments);
var numTransactionsLeft = numTransactions;
var objectStoreNames = ["store"];
var numTransactionsRunning;
automation.setStatus("Creating database.");
var options;
......@@ -206,36 +212,21 @@ function testRandomReadsAndWrites(
var transaction = getTransaction(db, objectStoreNames, "readwrite",
function() { onSetupComplete(db); });
putLinearValues(transaction, objectStoreNames, numKeys, null,
function () { return "test value"; });
function() { return "test value"; });
}
var completionFunc;
function onSetupComplete(db) {
automation.setStatus("Setup complete.");
completionFunc =
var completionFunc =
getCompletionFunc(db, testName, Date.now(), onTestComplete);
runOneBatch(db);
}
function runOneBatch(db) {
if (numTransactionsLeft <= 0) {
return;
}
--numTransactionsLeft;
++numTransactionsRunning;
var mode = "readonly";
if (numWritesPerTransaction)
mode = "readwrite";
var transaction = getTransaction(db, objectStoreNames, mode,
function() {
assert(!--numTransactionsRunning);
if (numTransactionsLeft <= 0) {
completionFunc();
} else {
runOneBatch(db);
}
});
runTransactionBatch(db, numTransactions, batchFunc, objectStoreNames, mode,
completionFunc);
}
function batchFunc(transaction) {
getRandomValues(transaction, objectStoreNames, numReadsPerTransaction,
numKeys, indexName);
putRandomValues(transaction, objectStoreNames, numWritesPerTransaction,
......@@ -243,6 +234,55 @@ function testRandomReadsAndWrites(
}
}
function testReadCache(numTransactions, useIndexForReads, onTestComplete) {
var numKeys = 10000;
var numReadsPerTransaction = 50;
var numTransactionsLeft = numTransactions;
var indexName;
if (useIndexForReads)
indexName = "index";
var testName = getDisplayName(arguments);
var objectStoreNames = ["store"];
var keys = [];
for (var i=0; i < numReadsPerTransaction; ++i) {
keys.push(getSimpleKey(Math.floor(Math.random() * numKeys)));
}
automation.setStatus("Creating database.");
var options;
if (useIndexForReads) {
options = [{
indexName: indexName,
indexKeyPath: "",
indexIsUnique: false,
indexIsMultiEntry: false,
}];
}
createDatabase(testName, objectStoreNames, onCreated, onError, options);
function onCreated(db) {
automation.setStatus("Setting up test database.");
var transaction = getTransaction(db, objectStoreNames, "readwrite",
function() { onSetupComplete(db); });
putLinearValues(transaction, objectStoreNames, numKeys, getSimpleKey,
function () { return "test value"; });
}
var completionFunc;
function onSetupComplete(db) {
automation.setStatus("Setup complete.");
completionFunc =
getCompletionFunc(db, testName, Date.now(), onTestComplete);
runTransactionBatch(db, numTransactions, batchFunc, objectStoreNames,
"readonly", completionFunc);
}
function batchFunc(transaction) {
getSpecificValues(transaction, objectStoreNames, indexName, keys);
}
}
function testCreateAndDeleteIndex(numKeys, onTestComplete) {
var testName = getDisplayName(arguments);
var objectStoreNames = ["store"];
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment