Skip to content

Instantly share code, notes, and snippets.

@billywhizz
Created June 8, 2025 13:27
Show Gist options
  • Save billywhizz/e57c5b41b90079b83bb216e545564cca to your computer and use it in GitHub Desktop.
Save billywhizz/e57c5b41b90079b83bb216e545564cca to your computer and use it in GitHub Desktop.
import { Stats } from './lib/bench.js'
import { SQL } from "bun";
const pool_size = 4
/*
CREATE TABLE Test (
id integer NOT NULL,
PRIMARY KEY (id)
);
*/
const sql = new SQL({
url: "postgres://user:pass@localhost:5432/dbname",
hostname: "tfb-database",
port: 5432,
database: "hello_world",
username: "benchmarkdbuser",
password: "benchmarkdbpass",
max: pool_size, // Maximum connections in pool
idleTimeout: 30, // Close idle connections after 30s
maxLifetime: 0, // Connection lifetime in seconds (0 = forever)
connectionTimeout: 30, // Timeout when establishing new connections
tls: false,
});
const stats = new Stats()
async function connection () {
const foo = await sql.reserve()
stats.conn++
let values = await foo`SELECT id FROM Test`.raw()
stats.rps++
while (values.length === 12) {
values = await foo`SELECT id FROM Test`.raw()
stats.rps++
}
}
const connections = []
for (let i = 0; i < pool_size; i++) {
connections.push(connection())
}
setInterval(() => stats.log(), 1000)
await Promise.all(connections)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment