-
-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathbench_deno_ffi.js
79 lines (68 loc) · 1.78 KB
/
bench_deno_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
import ffi from "../src/ffi.ts";
import { toCString, unwrap } from "../src/util.ts";
import {
SQLITE3_OPEN_CREATE,
SQLITE3_OPEN_MEMORY,
SQLITE3_OPEN_PRIVATECACHE,
SQLITE3_OPEN_READWRITE,
} from "../src/constants.ts";
const {
sqlite3_open_v2,
sqlite3_exec,
sqlite3_prepare_v2,
sqlite3_reset,
sqlite3_step,
sqlite3_column_int,
} = ffi;
const pHandle = new Uint32Array(2);
unwrap(
sqlite3_open_v2(
toCString(":memory:"),
pHandle,
SQLITE3_OPEN_READWRITE | SQLITE3_OPEN_PRIVATECACHE |
SQLITE3_OPEN_CREATE | SQLITE3_OPEN_MEMORY,
null,
),
);
const db = Deno.UnsafePointer.create(pHandle[0] + 2 ** 32 * pHandle[1]);
function exec(sql) {
const _pErr = new Uint32Array(2);
unwrap(sqlite3_exec(db, toCString(sql), null, null, _pErr));
}
exec("PRAGMA auto_vacuum = none");
exec("PRAGMA temp_store = memory");
exec("PRAGMA locking_mode = exclusive");
exec("PRAGMA user_version = 100");
const sql = "pragma user_version";
let total = parseInt(Deno.args[0], 10);
const runs = parseInt(Deno.args[1], 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) bench(query);
}
function prepareStatement() {
const pHandle = new Uint32Array(2);
unwrap(
sqlite3_prepare_v2(
db,
toCString(sql),
sql.length,
pHandle,
null,
),
);
return Deno.UnsafePointer.create(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);