summaryrefslogtreecommitdiffstats
path: root/tools/page_cycler/indexed_db/basic_insert/index.html
blob: 3edc04660ff7b2fda548c0572bf0ebdd69973aee (plain)
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
<!DOCTYPE HTML>
<html>
<title>Basic Insert</title>
<script src="../head.js"></script>
<script src="../common.js"></script>
<script>
  var RECORDS = 3000;

  var failed = false;
  function error(event) {
    // TODO(jorlow): Remove the old non-event.target part.
    log('Error ' + (event.code || event.target.errorCode) + ': '
        + (event.message || event.target.webkitErrorMessage));

    if (!failed)
      testComplete(TEST_FAILED);
    failed = true;
  }

  function start() {
    if (!setup())
      testComplete(SETUP_FAILED);

    var request = indexedDB.open('idb_basic_insert');
    request.onsuccess = onOpen;
    request.onerror = error;
  }

  function onOpen() {
    // TODO(jorlow): Remove the old non-event.target part.
    window.db = event.result || event.target.result;

    var request = db.setVersion('1');
    request.onerror = error;
    request.onsuccess = onSetVersion;
  }

  function onSetVersion() {
    // TODO(jorlow): Remove the old non-event.target part.
    var transaction = event.result || event.target.result;
    transaction.onerror = error;
    transaction.oncomplete = storeCreated;

    while (db.objectStoreNames.length)
      db.deleteObjectStore(db.objectStoreNames[0]);

    db.createObjectStore('store');
  }

  function storeCreated() {
    var transaction = db.transaction(['store'], IDBTransaction.READ_WRITE);
    transaction.oncomplete = insertsDone;
    transaction.onabort = error;

    var objectStore = transaction.objectStore('store');
    log('Inserting ' + RECORDS + ' records with explicit key.');
    startTime = new Date();
    for (var i = 0; i < RECORDS; i++) {
      var x = Math.floor(Math.random() * 1000000);
      objectStore.put(x, x).onerror = error;
    }
  }

  function insertsDone() {
    var now = Date.now();
    var diff = now - startTime;
    log('Inserted ' + RECORDS + ' records in ' + diff + ' ms (' + diff /
        RECORDS + ' ms per record)');

    if (!failed)
      testComplete(now - startTime);
  }
</script>

<body onLoad="start()">
</body>
</html>