Skip to content

Commit a2ebde5

Browse files
committed
Update benchmarks and fix concurrency issues.
1 parent dfc77f4 commit a2ebde5

File tree

7 files changed

+56
-44
lines changed

7 files changed

+56
-44
lines changed

benchmarks/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
"name": "benchmarks",
33
"type": "module",
44
"scripts": {
5-
"build": "tsc -b"
5+
"build": "tsc -b",
6+
"start": "NODE_OPTIONS=\"--experimental-sqlite --disable-warning=ExperimentalWarning\" node lib/index.js"
67
},
78
"dependencies": {
89
"better-sqlite3": "^11.0.0",

benchmarks/src/implementations/sjp-json.ts

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -33,20 +33,23 @@ export class JSPJsonImpl extends Benchmark {
3333
const db = this.driver(dbPath);
3434
this.db = db;
3535

36-
await using c = await db.reserveConnection();
36+
{
37+
await using c = await db.reserveConnection();
3738

38-
await c.run(
39-
'CREATE TABLE t1(id INTEGER PRIMARY KEY, a INTEGER, b INTEGER, c TEXT)'
40-
);
41-
await c.run(
42-
'CREATE TABLE t2(id INTEGER PRIMARY KEY, a INTEGER, b INTEGER, c TEXT)'
43-
);
44-
await c.run(
45-
'CREATE TABLE t3(id INTEGER PRIMARY KEY, a INTEGER, b INTEGER, c TEXT)'
46-
);
47-
await c.run('CREATE INDEX i3a ON t3(a)');
48-
await c.run('CREATE INDEX i3b ON t3(b)');
39+
await c.run(
40+
'CREATE TABLE t1(id INTEGER PRIMARY KEY, a INTEGER, b INTEGER, c TEXT)'
41+
);
42+
await c.run(
43+
'CREATE TABLE t2(id INTEGER PRIMARY KEY, a INTEGER, b INTEGER, c TEXT)'
44+
);
45+
await c.run(
46+
'CREATE TABLE t3(id INTEGER PRIMARY KEY, a INTEGER, b INTEGER, c TEXT)'
47+
);
48+
await c.run('CREATE INDEX i3a ON t3(a)');
49+
await c.run('CREATE INDEX i3b ON t3(b)');
50+
}
4951

52+
// Pre-create a bunch of read connections
5053
let promises = [];
5154
for (let i = 0; i < 10; i++) {
5255
promises.push(

benchmarks/src/implementations/sjp-optimized.ts

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -33,20 +33,23 @@ export class JSPOptimizedImpl extends Benchmark {
3333
const db = this.driver(dbPath);
3434
this.db = db;
3535

36-
await using c = await db.reserveConnection();
36+
{
37+
await using c = await db.reserveConnection();
3738

38-
await c.run(
39-
'CREATE TABLE t1(id INTEGER PRIMARY KEY, a INTEGER, b INTEGER, c TEXT)'
40-
);
41-
await c.run(
42-
'CREATE TABLE t2(id INTEGER PRIMARY KEY, a INTEGER, b INTEGER, c TEXT)'
43-
);
44-
await c.run(
45-
'CREATE TABLE t3(id INTEGER PRIMARY KEY, a INTEGER, b INTEGER, c TEXT)'
46-
);
47-
await c.run('CREATE INDEX i3a ON t3(a)');
48-
await c.run('CREATE INDEX i3b ON t3(b)');
39+
await c.run(
40+
'CREATE TABLE t1(id INTEGER PRIMARY KEY, a INTEGER, b INTEGER, c TEXT)'
41+
);
42+
await c.run(
43+
'CREATE TABLE t2(id INTEGER PRIMARY KEY, a INTEGER, b INTEGER, c TEXT)'
44+
);
45+
await c.run(
46+
'CREATE TABLE t3(id INTEGER PRIMARY KEY, a INTEGER, b INTEGER, c TEXT)'
47+
);
48+
await c.run('CREATE INDEX i3a ON t3(a)');
49+
await c.run('CREATE INDEX i3b ON t3(b)');
50+
}
4951

52+
// Pre-create a bunch of read connections
5053
let promises = [];
5154
for (let i = 0; i < 10; i++) {
5255
promises.push(

benchmarks/src/index.ts

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
1-
import {
2-
nodeSqliteAsyncPool,
3-
nodeSqlitePool
4-
} from '@powersync/sqlite-js-driver/node';
1+
import { NodeSqliteDriver } from '@powersync/sqlite-js-driver/node';
52
import { ConnectionPoolImpl } from '@powersync/sqlite-js-api';
6-
import { betterSqliteAsyncPool } from '@powersync/sqlite-js-better-sqlite3';
7-
import { betterSqlitePool } from '@powersync/sqlite-js-better-sqlite3/sync-driver';
3+
import { BetterSqliteDriver } from '@powersync/sqlite-js-better-sqlite3';
84
import { Benchmark } from './Benchmark.js';
95
import { BenchmarkResults } from './BenchmarkResults.js';
106
import { BetterSqlite3Impl } from './implementations/better-sqlite3.js';
@@ -20,61 +16,67 @@ async function main() {
2016
const results: BenchmarkResults[] = [
2117
await test(
2218
new JSPImpl('sjp-sync', dir, (path) => {
23-
const db = new ConnectionPoolImpl(betterSqlitePool(path));
19+
const db = new ConnectionPoolImpl(
20+
BetterSqliteDriver.openInProcess(path)
21+
);
2422
return db;
2523
})
2624
),
2725
await test(
2826
new JSPImpl('sjp-async', dir, (path) => {
29-
const db = new ConnectionPoolImpl(betterSqliteAsyncPool(path));
27+
const db = new ConnectionPoolImpl(BetterSqliteDriver.open(path));
3028
return db;
3129
})
3230
),
3331
await test(
3432
new JSPOptimizedImpl('sjp-sync-optimized', dir, (path) => {
35-
const db = new ConnectionPoolImpl(betterSqlitePool(path));
33+
const db = new ConnectionPoolImpl(
34+
BetterSqliteDriver.openInProcess(path)
35+
);
3636
return db;
3737
})
3838
),
3939
await test(
4040
new JSPOptimizedImpl('sjp-async-optimized', dir, (path) => {
41-
const db = new ConnectionPoolImpl(betterSqliteAsyncPool(path));
41+
const db = new ConnectionPoolImpl(BetterSqliteDriver.open(path));
4242
return db;
4343
})
4444
),
4545
await test(
4646
new JSPJsonImpl('sjp-sync-json', dir, (path) => {
47-
const db = new ConnectionPoolImpl(betterSqlitePool(path));
47+
const db = new ConnectionPoolImpl(
48+
BetterSqliteDriver.openInProcess(path)
49+
);
4850
return db;
4951
})
5052
),
5153
await test(
5254
new JSPJsonImpl('sjp-async-json', dir, (path) => {
53-
const db = new ConnectionPoolImpl(betterSqliteAsyncPool(path));
55+
const db = new ConnectionPoolImpl(BetterSqliteDriver.open(path));
5456
return db;
5557
})
5658
),
5759
await test(
5860
new JSPImpl('node-sjp-sync', dir, (path) => {
59-
const db = new ConnectionPoolImpl(nodeSqlitePool(path));
61+
const db = new ConnectionPoolImpl(NodeSqliteDriver.openInProcess(path));
6062
return db;
6163
})
6264
),
6365
await test(
6466
new JSPImpl('node-sjp-async', dir, (path) => {
65-
const db = new ConnectionPoolImpl(nodeSqliteAsyncPool(path));
67+
const db = new ConnectionPoolImpl(NodeSqliteDriver.open(path));
6668
return db;
6769
})
6870
),
6971
await test(
7072
new JSPOptimizedImpl('node-sjp-sync-optimized', dir, (path) => {
71-
const db = new ConnectionPoolImpl(nodeSqlitePool(path));
73+
const db = new ConnectionPoolImpl(NodeSqliteDriver.openInProcess(path));
7274
return db;
7375
})
7476
),
7577
await test(
7678
new JSPOptimizedImpl('node-sjp-async-optimized', dir, (path) => {
77-
const db = new ConnectionPoolImpl(nodeSqliteAsyncPool(path));
79+
const db = new ConnectionPoolImpl(NodeSqliteDriver.open(path));
7880
return db;
7981
})
8082
),

benchmarks/tsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,5 @@
1212
"typeRoots": ["./src/types", "./node_modules/@types"]
1313
},
1414
"include": ["src"],
15-
"references": []
15+
"references": [{ "path": "../packages/sqlite-js-api" }]
1616
}

packages/sqlite-js-api/src/impl.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ export class ConnectionPoolImpl
8484
): Promise<T> {
8585
const r = await this.reserveConnection(options);
8686
try {
87-
return r.transaction(callback, {
87+
return await r.transaction(callback, {
8888
type: options?.type ?? (options?.readonly ? 'deferred' : 'exclusive')
8989
});
9090
} finally {

packages/sqlite-js-driver/src/util/SingleConnectionPool.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,10 @@ export class SingleConnectionPool implements SqliteDriverConnectionPool {
6464
);
6565
});
6666

67-
return promise;
67+
return promise.then((r) => {
68+
this.inUse = reserved;
69+
return r;
70+
});
6871
}
6972
}
7073

0 commit comments

Comments
 (0)