Skip to content

Commit 421b996

Browse files
committed
feat: remove @databases dependency
The internals of the application use @databases/sqlite-sync, which has a dependency on `better-sqlite3`. The convienience of @databases is great for larger projects, applications, and is a fantastic abstraction. We can can reduce our dependencies by removing @databases and using `better-sqlite3` directly. It's a little more verbose, but manageable for inside this library.
1 parent 2b0297e commit 421b996

File tree

7 files changed

+172
-181
lines changed

7 files changed

+172
-181
lines changed

.gitignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,3 +128,8 @@ dist
128128
.yarn/build-state.yml
129129
.yarn/install-state.gz
130130
.pnp.*
131+
132+
# sqlite files
133+
*.sqlite
134+
*.sqlite-wal
135+
*.sqlite-shm

bench-turbo.js

Lines changed: 44 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,77 @@
1+
import { styleText } from 'node:util'
12
import Hold from './index.js'
23
import crypto from 'node:crypto'
34

4-
export default function benchTurbo (holder) {
5-
holder ||= Hold({ turbo: true })
5+
export default function benchTurbo (holder, {
6+
targetTime = 500,
7+
threshold = 1000,
8+
maxRuns = 10,
9+
growth = 1.01,
10+
retraction = 0.99,
11+
lower = 1,
12+
upper = 1000000
13+
} = {}) {
614
const records = []
15+
let runCount = 0
16+
17+
function log ({ a, b, c, time, col = null }) {
18+
const pad = (str) => `${str}`.padStart(('' + upper).length, ' ')
19+
const aStr = col === 'a' ? styleText('green', pad(a)) : pad(a)
20+
const bStr = col === 'b' ? styleText('green', pad(b)) : pad(b)
21+
const cStr = col === 'c' ? styleText('green', pad(c)) : pad(c)
22+
const timeStr = time < targetTime ? time.toFixed(3) : styleText('red', time.toFixed(3))
23+
24+
console.log(`Run: ${('' + runCount).padStart(('' + maxRuns).length, ' ')} |`, aStr, cStr, bStr, '|', timeStr + 'ms')
25+
}
26+
27+
function record ({ a, c, b, time, col }) {
28+
log(...arguments)
729

8-
function record (rec, time) {
930
records.push({
10-
'Time (ms)': time,
11-
'Record Count': rec,
12-
'Recods per ms': Number((rec / time).toFixed(3))
31+
Run: runCount,
32+
'Time (ms)': Number(time.toFixed(3)),
33+
'Record Count': arguments[0][col],
34+
'Recods per ms': Number((arguments[0][col] / time).toFixed(3))
1335
})
1436
}
1537

1638
function run (count) {
39+
const db = Hold({ turbo: true })
40+
1741
const tableName = crypto.createHash('sha1').update(('b' + new Date().valueOf() + count)).digest('hex')
18-
holder.init(tableName, 'keyX', 'valueX')
42+
db.init(tableName, 'keyX', 'valueX')
1943

20-
const entries = Array.from(Array(count)).map((_, i) => holder.prepare(tableName, `key${i}`, `value${i}`))
44+
const entries = Array.from(Array(count)).map((_, i) => db.prepare(tableName, `key${i}`, `value${i}`))
2145

2246
const start = performance.now()
23-
holder.setBulk(tableName, 'keyX', entries)
47+
db.setBulk(tableName, 'keyX', entries)
2448

2549
return performance.now() - start
2650
}
2751

2852
function bisect (a, b) {
29-
if ((b - a) <= 1000) return Math.round((a + b) / 2)
53+
runCount++
54+
if ((b - a) <= threshold || runCount > maxRuns) return Math.round((a + b) / 2)
3055

3156
const c = Math.round((a + b) / 2)
32-
3357
const cTime = run(c)
34-
if (cTime < 1000) {
35-
record(c, cTime)
36-
return bisect(c, Math.round(b * 1.02), 'c')
58+
59+
if (cTime < targetTime) {
60+
record({ a, c, b, time: cTime, col: 'c' })
61+
return bisect(c, Math.round(b * growth), 'c')
3762
}
3863

3964
const aTime = run(a)
40-
if (aTime < 1000) {
41-
record(a, aTime)
65+
if (aTime < targetTime) {
66+
record({ a, b, c, time: aTime, col: 'a' })
4267
return bisect(a, c, 'a')
4368
}
4469

45-
return bisect(Math.round(a * 0.98), c, null)
70+
log({ a, c, b, time: cTime })
71+
return bisect(Math.round(a * retraction), c, null)
4672
}
4773

48-
bisect(1, 150000, null)
74+
bisect(lower, upper, null)
4975

5076
return records.sort((a, b) => b['Recods per ms'] - a['Recods per ms']).slice(0, 3)
5177
}

bench.js

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,33 +19,31 @@ bench
1919
diskIdx++
2020
}, {
2121
beforeAll () {
22-
diskHolder = Hold({ location: './bench.db', enableWAL: false, exposeConnection: true })
22+
diskHolder = Hold({ location: './bench.sqlite', enableWAL: false, exposeConnection: true })
2323

2424
for (let i = 0; i < warmupIterations; i++) {
2525
diskHolder.set('disk', `key${i}`, 'value')
2626
}
2727
},
2828
afterAll () {
29-
diskHolder.connection.dispose()
3029
diskHolder = null
31-
unlinkSync('./bench.db')
30+
unlinkSync('./bench.sqlite')
3231
}
3332
})
3433
.add('diskWAL', () => {
3534
diskWALHolder.set('diskWAL', `key${diskWALIdx}`, 'value')
3635
diskWALIdx++
3736
}, {
3837
beforeAll () {
39-
diskWALHolder = Hold({ location: './bench-wal.db', exposeConnection: true })
38+
diskWALHolder = Hold({ location: './bench-wal.sqlite', exposeConnection: true })
4039

4140
for (let i = 0; i < warmupIterations; i++) {
4241
diskWALHolder.set('diskWAL', `key${i}`, 'value')
4342
}
4443
},
4544
afterAll () {
46-
diskWALHolder.connection.dispose()
4745
diskWALHolder = null
48-
unlinkSync('./bench-wal.db')
46+
unlinkSync('./bench-wal.sqlite')
4947
}
5048
})
5149
.add('memory', () => {
@@ -60,7 +58,6 @@ bench
6058
}
6159
},
6260
afterAll () {
63-
memoryHolder.connection.dispose()
6461
memoryHolder = null
6562
}
6663
})
@@ -76,7 +73,6 @@ bench
7673
}
7774
},
7875
afterAll () {
79-
memorySerialJSONHolder.connection.dispose()
8076
memorySerialJSONHolder = null
8177
}
8278
})
@@ -92,7 +88,6 @@ bench
9288
}
9389
},
9490
afterAll () {
95-
memorySerialJSONFastHolder.connection.dispose()
9691
memorySerialJSONFastHolder = null
9792
}
9893
})
@@ -108,7 +103,6 @@ bench
108103
}
109104
},
110105
afterAll () {
111-
memoryComplexKeyHolder.connection.dispose()
112106
memoryComplexKeyHolder = null
113107
}
114108
})

0 commit comments

Comments
 (0)