Skip to content

Commit

Permalink
chore(bench): use Deno.bench (#35)
Browse files Browse the repository at this point in the history
* stuff

* bump std version
  • Loading branch information
DjDeveloperr authored Jun 30, 2022
1 parent 8790f83 commit 30e0ef4
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 76 deletions.
78 changes: 8 additions & 70 deletions bench/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,11 @@ import native from "./native.ts";
import wasm from "./wasm.ts";
import { Backend } from "./backend.ts";

let level = 0;

function log(type: string, msg: string): void {
console.log(`${" ".repeat(level)}%c${type} %c${msg}`, "color: #0DBC79", "");
}

const ROWS = 10;
const ITERS = 10;

const backends: Backend[] = [native, wasm];

for (const backend of backends) {
log("Bench", backend.name);
level++;

backend.execute("pragma journal_mode = WAL", []);
backend.execute("pragma synchronous = normal", []);
backend.execute("pragma temp_store = memory", []);
Expand All @@ -26,71 +16,19 @@ for (const backend of backends) {
[],
);

log("Insert", "Bench start ->");
level++;

let total = 0;
let min!: number, max!: number;
for (let iter = 0; iter < ITERS; iter++) {
const now = performance.now();
Deno.bench(`${backend.name}: insert`, () => {
const prep = backend.prepare("insert into test (value) values (?)");
for (let i = 0; i < ROWS; i++) {
prep.execute([`iter ${iter} loop ${i}`]);
prep.execute([`loop ${i}`]);
}
prep.finalize();
const took = performance.now() - now;

if (min === undefined || max === undefined) {
min = max = took;
} else {
min = Math.min(min, took);
max = Math.max(max, took);
}

total += took;
}

log(
"Result",
`${ITERS} iter ${ROWS} rows took ${total.toFixed(2)}ms ${
(ITERS / (total / 1000)).toFixed(2)
} iter/sec min ${min.toFixed(2)}ms max ${max.toFixed(2)}ms ±${
Math.abs(max - min).toFixed(2)
}ms`,
);
level--;

log("Query", "Bench start ->");
level++;
});

total = 0, min = undefined as any, max = undefined as any;
for (let iter = 0; iter < ITERS; iter++) {
const now = performance.now();
Deno.bench(`${backend.name}: query`, () => {
backend.query("select * from test");
const took = performance.now() - now;

if (min === undefined || max === undefined) {
min = max = took;
} else {
min = Math.min(min, took);
max = Math.max(max, took);
}

total += took;
}

log(
"Result",
`${ITERS} iter ${ROWS} rows took ${total.toFixed(2)}ms ${
(ITERS / (total / 1000)).toFixed(2)
} iter/sec min ${min.toFixed(2)}ms max ${max.toFixed(2)}ms ±${
Math.abs(max - min).toFixed(2)
}ms`,
);

level--;

level--;
});

backend.close();
globalThis.addEventListener("unload", () => {
backend.close();
});
}
2 changes: 1 addition & 1 deletion deno.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"tasks": {
"test": "deno test --unstable -A test/test.ts",
"bench": "deno run --unstable -A bench/main.ts"
"bench": "deno bench --unstable -A bench/main.ts"
},
"lint": {
"rules": {
Expand Down
2 changes: 1 addition & 1 deletion deps.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export { fromFileUrl } from "https://deno.land/std@0.139.0/path/mod.ts";
export { fromFileUrl } from "https://deno.land/std@0.145.0/path/mod.ts";
25 changes: 22 additions & 3 deletions src/ffi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ const symbols = {
parameters: [
"pointer", /* sqlite3 *db */
"pointer", /* const char *sql */
"pointer", /* sqlite3_callback callback */
"function", /* sqlite3_callback callback */
"pointer", /* void *pArg */
"pointer", /* char **errmsg */
],
Expand Down Expand Up @@ -686,18 +686,37 @@ export function sqlite3_free(ptr: bigint): void {
lib.sqlite3_free(ptr);
}

export type SqliteCallback = (
funcArg: bigint,
columns: number,
p1: bigint,
p2: bigint,
) => number;

export function createSqliteCallback(cb: SqliteCallback): Deno.UnsafeCallback {
return new Deno.UnsafeCallback(
{
parameters: ["pointer", "i32", "pointer", "pointer"],
result: "i32",
} as const,
cb,
);
}

export function sqlite3_exec(
db: sqlite3,
sql: string,
func?: bigint,
funcArg?: bigint,
): void {
const sqlPtr = toCString(sql);
const outPtr = new BigUint64Array(8);

const result = lib.sqlite3_exec(
db,
sqlPtr,
null,
null,
func ?? 0n,
funcArg ?? 0n,
outPtr,
);

Expand Down
2 changes: 1 addition & 1 deletion src/util.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
export const encoder = new TextEncoder();

export function toCString(str: string): Uint8Array {
return new Uint8Array([...encoder.encode(str), 0]);
return encoder.encode(str + "\0");
}

export function isObject(value: unknown): boolean {
Expand Down

0 comments on commit 30e0ef4

Please sign in to comment.