-
-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathbench_bun_ffi.js
150 lines (129 loc) · 3.03 KB
/
bench_bun_ffi.js
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
import { dlopen, ptr } from "bun:ffi";
import {
SQLITE3_OPEN_CREATE,
SQLITE3_OPEN_MEMORY,
SQLITE3_OPEN_PRIVATECACHE,
SQLITE3_OPEN_READWRITE,
} from "../src/constants.ts";
const {
symbols: {
sqlite3_open_v2,
sqlite3_exec,
sqlite3_prepare_v2,
sqlite3_reset,
sqlite3_step,
sqlite3_column_int,
sqlite3_errstr,
},
} = dlopen("build/libsqlite3.so", {
sqlite3_open_v2: {
args: [
"ptr", // const char *filename
"ptr", // sqlite3 **ppDb
"i32", // int flags
"u64", // const char *zVfs
],
returns: "i32",
},
sqlite3_errstr: {
args: ["i32" /** int errcode */],
returns: "cstring",
},
sqlite3_prepare_v2: {
args: [
"u64", // sqlite3 *db
"ptr", // const char *zSql
"i32", // int nByte
"ptr", // sqlite3_stmt **ppStmt
"u64", // const char **pzTail
],
returns: "i32",
},
sqlite3_exec: {
args: [
"u64", // sqlite3 *db
"ptr", // const char *sql
"u64", // sqlite3_callback callback
"u64", // void *arg
"ptr", // char **errmsg
],
returns: "i32",
},
sqlite3_reset: {
args: [
"u64", // sqlite3_stmt *pStmt
],
returns: "i32",
},
sqlite3_step: {
args: [
"u64", // sqlite3_stmt *pStmt
],
returns: "i32",
},
sqlite3_column_int: {
args: [
"u64", // sqlite3_stmt *pStmt
"i32", // int iCol
],
returns: "i32",
},
});
function unwrap(code) {
if (code !== 0) {
throw new Error(`SQLite3 error: ${sqlite3_errstr(code)}: ${code}`);
}
}
const pHandle = new Uint32Array(2);
const encoder = new TextEncoder();
unwrap(
sqlite3_open_v2(
ptr(encoder.encode(":memory:\0")),
ptr(pHandle),
SQLITE3_OPEN_READWRITE | SQLITE3_OPEN_PRIVATECACHE |
SQLITE3_OPEN_CREATE | SQLITE3_OPEN_MEMORY,
0,
),
);
const db = pHandle[0] + 2 ** 32 * pHandle[1];
function exec(sql) {
const _pErr = new Uint32Array(2);
unwrap(sqlite3_exec(db, ptr(encoder.encode(sql + "\0")), 0, 0, ptr(_pErr)));
}
exec("PRAGMA auto_vacuum = none");
exec("PRAGMA temp_store = memory");
exec("PRAGMA locking_mode = exclusive");
exec("PRAGMA user_version = 100");
let total = parseInt(process.argv[2], 10);
const runs = parseInt(process.argv[3], 10);
function bench(query) {
const start = performance.now();
for (let i = 0; i < runs; i++) query();
const elapsed = Math.floor(performance.now() - start);
const rate = Math.floor(runs / (elapsed / 1000));
console.log(`time ${elapsed} ms rate ${rate}`);
if (--total) process.nextTick(() => bench(query));
}
function prepareStatement() {
const pHandle = new Uint32Array(2);
const s = encoder.encode("PRAGMA user_version");
unwrap(
sqlite3_prepare_v2(
db,
ptr(s),
s.byteLength,
ptr(pHandle),
0,
),
);
return pHandle[0] + 2 ** 32 * pHandle[1];
}
const prepared = prepareStatement();
function run() {
sqlite3_step(prepared);
const int = sqlite3_column_int(prepared, 0);
sqlite3_reset(prepared);
return int;
}
console.log(`user_version: ${run()}`);
bench(run);