Skip to content

Commit cad3949

Browse files
author
Luis Pardo
committed
Fix incorrect usage of sync test runner, remove comments, close databases
1 parent ac593b9 commit cad3949

File tree

9 files changed

+45
-130
lines changed

9 files changed

+45
-130
lines changed

experimental/javascript-wc-indexeddb/dist/src/speedometer-utils/benchmark.mjs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/* eslint-disable no-case-declarations */
2-
import { TestRunner } from "./test-runner.mjs";
2+
import { TestRunner, AsyncTestRunner } from "./test-runner.mjs";
33
import { Params } from "./params.mjs";
44

55
/**
@@ -15,7 +15,8 @@ export class BenchmarkStep {
1515
}
1616

1717
async runAndRecord(params, suite, test, callback) {
18-
const testRunner = new TestRunner(null, null, params, suite, test, callback);
18+
const TestRunnerClass = params.useAsyncSteps ? AsyncTestRunner : TestRunner;
19+
const testRunner = new TestRunnerClass(null, null, params, suite, test, callback);
1920
const result = await testRunner.runTest();
2021
return result;
2122
}
@@ -107,7 +108,6 @@ export class BenchmarkConnector {
107108
if (!suite)
108109
console.error(`Suite with the name of "${event.data.name}" not found!`);
109110
const { result } = await suite.runAndRecord(params, (test) => this.sendMessage({ type: "step-complete", status: "success", appId: this.appId, name: this.name, test }));
110-
console.log(result, result.tests);
111111
this.sendMessage({ type: "suite-complete", status: "success", appId: this.appId, result });
112112
this.disconnect();
113113
break;

experimental/javascript-wc-indexeddb/dist/src/storage/base-storage-manager.js

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// Base class for storage managers that provides common functionality
2+
// for tracking pending operations and dispatching events.
13
class BaseStorageManager {
24
constructor() {
35
this.dbName = "todoDB";
@@ -6,17 +8,21 @@ class BaseStorageManager {
68
this.pendingAdditions = 0;
79
this.pendingToggles = 0;
810
this.pendingDeletions = 0;
9-
}
10-
11-
async initDB() {
12-
throw new Error("initDB method must be implemented by subclass");
11+
this.initDB().then(() => {
12+
this._dispatchReadyEvent();
13+
});
1314
}
1415

1516
_ensureDbConnection() {
1617
if (!this.db)
1718
throw new Error("Database connection is not established");
1819
}
1920

21+
// When runner in Speedometer, additions, completions and removals are
22+
// triggered synchonously in a tight loop, increasing the pending counters.
23+
// The completion events are dispatched only when all pending operations
24+
// of that type are complete.
25+
2026
_handleAddComplete() {
2127
if (--this.pendingAdditions === 0)
2228
window.dispatchEvent(new CustomEvent("db-add-completed", {}));
@@ -28,8 +34,10 @@ class BaseStorageManager {
2834
}
2935

3036
_handleRemoveComplete() {
31-
if (--this.pendingDeletions === 0)
37+
if (--this.pendingDeletions === 0) {
38+
this.db.close();
3239
window.dispatchEvent(new CustomEvent("db-remove-completed", {}));
40+
}
3341
}
3442

3543
_dispatchReadyEvent() {
@@ -48,6 +56,12 @@ class BaseStorageManager {
4856
this.pendingDeletions++;
4957
}
5058

59+
// Abstract methods that must be implemented by subclasses
60+
61+
async initDB() {
62+
throw new Error("initDB method must be implemented by subclass");
63+
}
64+
5165
addTodo(todo) {
5266
throw new Error("addTodo method must be implemented by subclass");
5367
}

experimental/javascript-wc-indexeddb/dist/src/storage/dexieDB-manager.js

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,18 @@ import BaseStorageManager from "./base-storage-manager.js";
44
class DexieDBManager extends BaseStorageManager {
55
constructor() {
66
super();
7-
this.initDB().then(() => {
8-
this._dispatchReadyEvent();
9-
});
107
}
118

129
async initDB() {
1310
// Delete the existing database first for clean state
1411
await Dexie.delete(this.dbName);
1512

16-
// Create new Dexie database
1713
this.db = new Dexie(this.dbName);
1814

19-
// Define schema
2015
this.db.version(1).stores({
2116
todos: "itemNumber, id, title, completed, priority",
2217
});
2318

24-
// Open the database
2519
await this.db.open();
2620

2721
return this.db;
@@ -31,7 +25,6 @@ class DexieDBManager extends BaseStorageManager {
3125
this._ensureDbConnection();
3226

3327
this._incrementPendingAdditions();
34-
// Add todo item to Dexie
3528
this.db.todos
3629
.add(todo)
3730
.then(() => {
@@ -60,17 +53,13 @@ class DexieDBManager extends BaseStorageManager {
6053

6154
this._incrementPendingToggles();
6255

63-
// Get the todo item and update it
6456
this.db.todos
6557
.get(itemNumber)
6658
.then((todoItem) => {
6759
if (!todoItem)
6860
throw new Error(`Todo item with itemNumber '${itemNumber}' not found`);
6961

70-
// Update the completed status
7162
todoItem.completed = completed;
72-
73-
// Save the updated item back to the database
7463
return this.db.todos.put(todoItem);
7564
})
7665
.then(() => {
@@ -85,7 +74,6 @@ class DexieDBManager extends BaseStorageManager {
8574
this._ensureDbConnection();
8675

8776
this._incrementPendingDeletions();
88-
// Delete the todo item
8977
this.db.todos
9078
.delete(itemNumber)
9179
.then(() => {

experimental/javascript-wc-indexeddb/dist/src/storage/indexedDB-manager.js

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,6 @@
11
import BaseStorageManager from "./base-storage-manager.js";
22

33
class IndexedDBManager extends BaseStorageManager {
4-
constructor() {
5-
super();
6-
this.dbVersion = 1;
7-
this.initDB().then(() => {
8-
this._dispatchReadyEvent();
9-
});
10-
}
11-
124
initDB() {
135
return new Promise((resolve, reject) => {
146
// Delete the existing database first for clean state
@@ -29,7 +21,7 @@ class IndexedDBManager extends BaseStorageManager {
2921
}
3022

3123
openDatabase(resolve, reject) {
32-
const request = indexedDB.open(this.dbName, this.dbVersion);
24+
const request = indexedDB.open(this.dbName, 1);
3325

3426
request.onerror = (event) => {
3527
reject(event.target.error);
@@ -43,7 +35,6 @@ class IndexedDBManager extends BaseStorageManager {
4335
request.onupgradeneeded = (event) => {
4436
const db = event.target.result;
4537

46-
// Create object store (since we're always creating a fresh DB now)
4738
const store = db.createObjectStore(this.storeName, { keyPath: "itemNumber" });
4839
store.createIndex("id", "id", { unique: true });
4940
store.createIndex("title", "title", { unique: false });
@@ -55,7 +46,6 @@ class IndexedDBManager extends BaseStorageManager {
5546
addTodo(todo) {
5647
this._ensureDbConnection();
5748

58-
// Add todo item to IndexedDB
5949
const transaction = this.db.transaction(this.storeName, "readwrite");
6050
const store = transaction.objectStore(this.storeName);
6151

@@ -116,11 +106,9 @@ class IndexedDBManager extends BaseStorageManager {
116106
toggleTodo(itemNumber, completed) {
117107
this._ensureDbConnection();
118108

119-
// Access the todo item directly by its itemNumber (keyPath)
120109
const transaction = this.db.transaction(this.storeName, "readwrite");
121110
const store = transaction.objectStore(this.storeName);
122111

123-
// Get the todo item directly using its primary key (itemNumber)
124112
const getRequest = store.get(itemNumber);
125113

126114
this._incrementPendingToggles();
@@ -131,9 +119,7 @@ class IndexedDBManager extends BaseStorageManager {
131119
if (!todoItem)
132120
throw new Error(`Todo item with itemNumber '${itemNumber}' not found`);
133121

134-
// Update the completed status
135122
todoItem.completed = completed;
136-
// Save the updated item back to the database
137123
const updateRequest = store.put(todoItem);
138124

139125
updateRequest.onerror = (event) => {

experimental/javascript-wc-indexeddb/src/index.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { BenchmarkConnector } from "/node_modules/speedometer-utils/benchmark.mjs";
1+
import { BenchmarkConnector } from "/src/speedometer-utils/benchmark.mjs";
22
import suites, { appName, appVersion } from "/src/workload-test.mjs";
33

44
window.workloadPromises = {};

experimental/javascript-wc-indexeddb/src/speedometer-utils/benchmark.mjs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/* eslint-disable no-case-declarations */
2-
import { TestRunner } from "./test-runner.mjs";
2+
import { TestRunner, AsyncTestRunner } from "./test-runner.mjs";
33
import { Params } from "./params.mjs";
44

55
/**
@@ -15,7 +15,8 @@ export class BenchmarkStep {
1515
}
1616

1717
async runAndRecord(params, suite, test, callback) {
18-
const testRunner = new TestRunner(null, null, params, suite, test, callback);
18+
const TestRunnerClass = params.useAsyncSteps ? AsyncTestRunner : TestRunner;
19+
const testRunner = new TestRunnerClass(null, null, params, suite, test, callback);
1920
const result = await testRunner.runTest();
2021
return result;
2122
}
@@ -107,7 +108,6 @@ export class BenchmarkConnector {
107108
if (!suite)
108109
console.error(`Suite with the name of "${event.data.name}" not found!`);
109110
const { result } = await suite.runAndRecord(params, (test) => this.sendMessage({ type: "step-complete", status: "success", appId: this.appId, name: this.name, test }));
110-
console.log(result, result.tests);
111111
this.sendMessage({ type: "suite-complete", status: "success", appId: this.appId, result });
112112
this.disconnect();
113113
break;

experimental/javascript-wc-indexeddb/src/storage/base-storage-manager.js

Lines changed: 17 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
1-
/**
2-
* Base class for storage managers that provides common functionality
3-
* for tracking pending operations and dispatching events.
4-
*/
1+
// Base class for storage managers that provides common functionality
2+
// for tracking pending operations and dispatching events.
53
class BaseStorageManager {
64
constructor() {
75
this.dbName = "todoDB";
@@ -10,117 +8,72 @@ class BaseStorageManager {
108
this.pendingAdditions = 0;
119
this.pendingToggles = 0;
1210
this.pendingDeletions = 0;
11+
this.initDB().then(() => {
12+
this._dispatchReadyEvent();
13+
});
1314
}
1415

15-
/**
16-
* Initialize the database. This method should be implemented by subclasses.
17-
* @returns {Promise} Promise that resolves when the database is initialized
18-
*/
19-
async initDB() {
20-
throw new Error("initDB method must be implemented by subclass");
21-
}
22-
23-
/**
24-
* Check if the database connection is established
25-
* @throws {Error} If database connection is not established
26-
*/
2716
_ensureDbConnection() {
2817
if (!this.db)
2918
throw new Error("Database connection is not established");
3019
}
3120

32-
/**
33-
* Handle completion of add operations
34-
* @protected
35-
*/
21+
// When runner in Speedometer, additions, completions and removals are
22+
// triggered synchonously in a tight loop, increasing the pending counters.
23+
// The completion events are dispatched only when all pending operations
24+
// of that type are complete.
25+
3626
_handleAddComplete() {
3727
if (--this.pendingAdditions === 0)
3828
window.dispatchEvent(new CustomEvent("db-add-completed", {}));
3929
}
4030

41-
/**
42-
* Handle completion of toggle operations
43-
* @protected
44-
*/
4531
_handleToggleComplete() {
4632
if (--this.pendingToggles === 0)
4733
window.dispatchEvent(new CustomEvent("db-toggle-completed", {}));
4834
}
4935

50-
/**
51-
* Handle completion of remove operations
52-
* @protected
53-
*/
5436
_handleRemoveComplete() {
55-
if (--this.pendingDeletions === 0)
37+
if (--this.pendingDeletions === 0) {
38+
this.db.close();
5639
window.dispatchEvent(new CustomEvent("db-remove-completed", {}));
40+
}
5741
}
5842

59-
/**
60-
* Dispatch the db-ready event when initialization is complete
61-
* @protected
62-
*/
6343
_dispatchReadyEvent() {
6444
window.dispatchEvent(new CustomEvent("db-ready", {}));
6545
}
6646

67-
/**
68-
* Increment the pending additions counter
69-
* @protected
70-
*/
7147
_incrementPendingAdditions() {
7248
this.pendingAdditions++;
7349
}
7450

75-
/**
76-
* Increment the pending toggles counter
77-
* @protected
78-
*/
7951
_incrementPendingToggles() {
8052
this.pendingToggles++;
8153
}
8254

83-
/**
84-
* Increment the pending deletions counter
85-
* @protected
86-
*/
8755
_incrementPendingDeletions() {
8856
this.pendingDeletions++;
8957
}
9058

9159
// Abstract methods that must be implemented by subclasses
9260

93-
/**
94-
* Add a todo item to the database
95-
* @param {Object} todo - The todo item to add
96-
*/
61+
async initDB() {
62+
throw new Error("initDB method must be implemented by subclass");
63+
}
64+
9765
addTodo(todo) {
9866
throw new Error("addTodo method must be implemented by subclass");
9967
}
10068

101-
/**
102-
* Get todos from the database
103-
* @param {number} upperItemNumber - Upper bound for item numbers (exclusive)
104-
* @param {number} count - Maximum number of items to retrieve
105-
* @returns {Promise<Array>} Promise that resolves to an array of todo items
106-
*/
10769
async getTodos(upperItemNumber, count) {
10870
throw new Error("getTodos method must be implemented by subclass");
10971
}
11072

111-
/**
112-
* Toggle the completed status of a todo item
113-
* @param {number} itemNumber - The item number to toggle
114-
* @param {boolean} completed - The new completed status
115-
*/
11673
toggleTodo(itemNumber, completed) {
11774
throw new Error("toggleTodo method must be implemented by subclass");
11875
}
11976

120-
/**
121-
* Remove a todo item from the database
122-
* @param {number} itemNumber - The item number to remove
123-
*/
12477
removeTodo(itemNumber) {
12578
throw new Error("removeTodo method must be implemented by subclass");
12679
}

0 commit comments

Comments
 (0)