-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbench.ts
132 lines (116 loc) · 3.04 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
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
import pglue from "./mod.ts";
import postgres_js from "https://deno.land/x/postgresjs/mod.js";
import * as deno_postgres from "https://deno.land/x/postgres/mod.ts";
const c_pglue = await pglue.connect(`postgres://test:test@localhost:5432/test`);
const c_pgjs = await postgres_js(
`postgres://test:test@localhost:5432/test`
).reserve();
const c_denopg = new deno_postgres.Client({
user: "test",
database: "test",
hostname: "localhost",
password: "test",
port: 5432,
});
await c_denopg.connect();
async function bench_select(
b: Deno.BenchContext,
n: number,
q: () => PromiseLike<unknown>
) {
await q();
b.start();
const tasks = [];
for (let i = 0; i < n; i++) tasks.push(q());
await Promise.all(tasks);
b.end();
}
async function bench_insert(
b: Deno.BenchContext,
n: number,
q: (a: string, b: boolean, c: number) => PromiseLike<unknown>
) {
await q("prepare", false, 0);
b.start();
const tasks = [];
for (let i = 0; i < n; i++)
tasks.push(q(i.toString(16).repeat(5), i % 3 === 0, i));
await Promise.all(tasks);
b.end();
}
for (const n of [1, 5, 10]) {
Deno.bench({
name: `pglue`,
group: `select n=${n}`,
baseline: true,
async fn(b) {
await bench_select(b, n, () => c_pglue.query`select * from pg_type`);
},
});
Deno.bench({
name: `postgres.js`,
group: `select n=${n}`,
async fn(b) {
await bench_select(b, n, () => c_pgjs`select * from pg_type`);
},
});
Deno.bench({
name: `deno-postgres`,
group: `select n=${n}`,
async fn(b) {
await bench_select(
b,
n,
() => c_denopg.queryArray`select * from pg_type`
);
},
});
}
for (const n of [1, 10, 100, 200]) {
Deno.bench({
name: `pglue`,
group: `insert n=${n}`,
baseline: true,
async fn(b) {
await using _tx = await c_pglue.begin();
await c_pglue.query`create table my_table (a text not null, b boolean not null, c integer not null)`;
await bench_insert(b, n, (a, b, c) =>
c_pglue.query`insert into my_table (a, b, c) values (${a}, ${b}, ${c})`.execute()
);
},
});
Deno.bench({
name: `postgres.js`,
group: `insert n=${n}`,
async fn(b) {
await c_pgjs`begin`;
try {
await c_pgjs`create table my_table (a text not null, b boolean not null, c integer not null)`;
await bench_insert(b, n, (a, b, c) =>
c_pgjs`insert into my_table (a, b, c) values (${a}, ${b}, ${c})`.execute()
);
} finally {
await c_pgjs`rollback`;
}
},
});
Deno.bench({
name: `deno-postgres`,
group: `insert n=${n}`,
async fn(b) {
const tx = c_denopg.createTransaction(`my_tx`);
await tx.begin();
try {
await tx.queryArray`create table my_table (a text not null, b boolean not null, c integer not null)`;
await bench_insert(
b,
n,
(a, b, c) =>
tx.queryArray`insert into my_table (a, b, c) values (${a}, ${b}, ${c})`
);
} finally {
await tx.rollback();
}
},
});
}