-
Notifications
You must be signed in to change notification settings - Fork 39
/
bench.ts
88 lines (79 loc) · 1.87 KB
/
bench.ts
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
import {
bench,
runBenchmarks,
} from "https://deno.land/[email protected]/testing/bench.ts";
import { DB } from "./mod.ts";
if (Deno.args[0]) {
try {
await Deno.remove(Deno.args[0]);
} catch (_) {
// ignore
}
}
const dbFile = Deno.args[0] || ":memory:";
const db = new DB(dbFile);
db.query(
"CREATE TABLE users (id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT, balance INTEGER)",
);
/** Performance of insert statements (1 insert). */
const names = "Deno Land Peter Parker Clark Kent Robert Parr".split(" ");
bench({
name: "insert 10 000 (named)",
runs: 100,
func: (b): void => {
b.start();
const query = db.prepareQuery(
"INSERT INTO users (name, balance) VALUES (:name, :balance)",
);
db.query("begin");
for (let i = 0; i < 10_000; i++) {
query.execute({ name: names[i % names.length], balance: i });
}
db.query("commit");
b.stop();
},
});
bench({
name: "insert 10 000 (positional)",
runs: 100,
func: (b): void => {
b.start();
const query = db.prepareQuery(
"INSERT INTO users (name, balance) VALUES (?, ?)",
);
db.query("begin");
for (let i = 0; i < 10_000; i++) {
query.execute([names[i % names.length], i]);
}
db.query("commit");
b.stop();
},
});
/** Performance of select statements (select all; 10_000). */
bench({
name: "select 10 000 (select all)",
runs: 100,
func: (b): void => {
b.start();
db.query(
"SELECT name, balance FROM users LIMIT 10000",
);
b.stop();
},
});
/** Performance of select statements (select individually; 10_000). */
bench({
name: "select 10 000 (select first)",
runs: 100,
func: (b): void => {
b.start();
const query = db.prepareQuery(
"SELECT name, balance FROM users WHERE id = ?",
);
for (let id = 1; id <= 10_000; id++) {
query.first([id]);
}
b.stop();
},
});
runBenchmarks();