Skip to content

Instantly share code, notes, and snippets.

@alexgleason
Created June 20, 2024 03:16
Show Gist options
  • Save alexgleason/64ed1a1e44e40284b3c379b4331eb340 to your computer and use it in GitHub Desktop.
Save alexgleason/64ed1a1e44e40284b3c379b4331eb340 to your computer and use it in GitHub Desktop.
Benchmark for node-postgres on Deno
import { Buffer } from 'node:buffer';
// @deno-types="npm:@types/pg@^8.11.6"
import pg from 'npm:pg@^8.12.0';
interface TestQuery {
text: string
values?: unknown[]
}
const params: TestQuery = {
text: 'select typname, typnamespace, typowner, typlen, typbyval, typcategory, typispreferred, typisdefined, typdelim, typrelid, typelem, typarray from pg_type where typtypmod = $1 and typisdefined = $2',
values: [-1, true],
}
const insert: TestQuery = {
text: 'INSERT INTO foobar(name, age) VALUES ($1, $2)',
values: ['brian', 100],
}
const seq: TestQuery = {
text: 'SELECT * FROM generate_series(1, 1000)',
}
const exec = async (client: pg.Client, q: TestQuery): Promise<void> => {
await client.query({
text: q.text,
values: q.values,
rowMode: 'array',
})
}
const bench = async (client: pg.Client, q: TestQuery, time: number): Promise<number> => {
const start = Date.now()
let count = 0
while (true) {
await exec(client, q)
count++
if (Date.now() - start > time) {
return count
}
}
}
const run = async (): Promise<void> => {
const client = new pg.Client()
await client.connect()
console.log('start')
await client.query('CREATE TEMP TABLE foobar(name TEXT, age NUMERIC)')
await client.query('CREATE TEMP TABLE buf(name TEXT, data BYTEA)')
await bench(client, params, 1000)
console.log('warmup done')
const seconds = 5
for (let i = 0; i < 4; i++) {
let queries = await bench(client, params, seconds * 1000)
console.log('')
console.log('little queries:', queries)
console.log('qps', queries / seconds)
console.log('on my laptop best so far seen 733 qps')
console.log('')
queries = await bench(client, seq, seconds * 1000)
console.log('sequence queries:', queries)
console.log('qps', queries / seconds)
console.log('on my laptop best so far seen 1309 qps')
console.log('')
queries = await bench(client, insert, seconds * 1000)
console.log('insert queries:', queries)
console.log('qps', queries / seconds)
console.log('on my laptop best so far seen 6445 qps')
console.log('')
console.log('Warming up bytea test')
await client.query({
text: 'INSERT INTO buf(name, data) VALUES ($1, $2)',
values: ['test', Buffer.allocUnsafe(104857600)],
})
console.log('bytea warmup done')
const start = Date.now()
const results = await client.query('SELECT * FROM buf')
const time = Date.now() - start
console.log('bytea time:', time, 'ms')
console.log('bytea length:', results.rows[0].data.byteLength, 'bytes')
console.log('on my laptop best so far seen 1107ms and 104857600 bytes')
await new Promise((resolve) => setTimeout(resolve, 250))
}
await client.end()
await client.end()
}
run().catch((e) => console.error(e))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment