summaryrefslogtreecommitdiffstats
path: root/testing/raptor/browsertime/indexeddb_getkeyrange.js
blob: 100ac20e2a7df60bab91b7c498da9928767d3065 (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
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

/* eslint-env node */

module.exports = async function (context, commands) {
  context.log.info("Starting a indexedDB getkeyrange");

  const test_url = context.options.browsertime.url;
  let page_cycles = context.options.browsertime.page_cycles;
  let page_cycle_delay = context.options.browsertime.page_cycle_delay;
  let post_startup_delay = context.options.browsertime.post_startup_delay;

  context.log.info(
    "Waiting for %d ms (post_startup_delay)",
    post_startup_delay
  );
  await commands.wait.byTime(post_startup_delay);

  await commands.navigate(test_url);

  for (let count = 0; count < page_cycles; count++) {
    context.log.info("Cycle %d, waiting for %d ms", count, page_cycle_delay);
    await commands.wait.byTime(page_cycle_delay);

    context.log.info("Cycle %d, starting the measure", count);
    await await commands.measure.start();
    await commands.js.run(`
        const notifyDone = arguments[arguments.length - 1];
        async function resPromise() {
          return new Promise((resolve, reject) => {
            const results = {};
            const request = indexedDB.open('get-keyrange', 1);

            request.onsuccess = () => {
              const db = request.result;
              const start = Date.now();
              const keyRange = IDBKeyRange.bound(0, 99);
              const transaction = db.transaction('entries', 'readonly');
              const store = transaction.objectStore('entries');
              const index = store.index('index');
              const getAllRequest = index.getAll(keyRange);
              getAllRequest.onsuccess = () => {
                const items = getAllRequest.result;
                items.forEach((item) => {
                  results[item.key] = item;
                });
                const end = Date.now();
                db.close();
                resolve(end - start);
              };
              getAllRequest.onerror = () => {
                reject(getAllRequest.error);
              };
            };

          });
        }
        resPromise().then(() => {
          notifyDone();
        });
      `);
    await commands.measure.stop();
  }

  context.log.info("IndexedDB getkeyrange ended.");
  return true;
};