porsager / postgres

Postgres.js - The Fastest full featured PostgreSQL client for Node.js, Deno, Bun and CloudFlare
The Unlicense
7.09k stars 259 forks source link

A weird issue: speed dropped #747

Open lnlife opened 8 months ago

lnlife commented 8 months ago

Here is my code:

import express from "express";
import postgres from "postgres";
const sql = postgres('postgres://test:test@127.0.0.1:5432/test')
const port = 3000;
const app = express();

app.get("/", async (req, res) => {
    for (let i = 1; i <= 20; i++) {
        const xs = await sql`select * from posts where id=1`
    }
    res.send(`ok`);
});
app.listen(port, () => {
    console.log(`Listening on port ${port}...`);
});

I run the following ab test: ab -n 2000 -c 5 http://localhost:3000 ( number:2000, concurrency: 5 requests at a time) The result is stable at around 900/sec, whatever times repeat the command.

Then I run: ab -n 2000 -c 10 http://localhost:3000 (change 5 to 10) The result is still around 900/sec

Then issue came. When I run ab -n 2000 -c 5 http://localhost:3000 (change 10 back to 5) After repeat this command several times, the results significantly dropped to 150/sec, and even keep dropping. If I restart the express server, and run the above command, it become stable at 900/sec again. This issue is that, if I set -c 10 and then set -c 5, the reqeusts/sec will drop significantly.

I have tested both ts-node and bun, both express and bun native serve, same issue occured.

WIGGLES-dev commented 7 months ago

Can confirm experiencing same issue in bun, performance degrades between stress test. Notably the issue is only present when max connections is set great than 1. So it may be a pooling issue.

porsager commented 7 months ago

This is a very nice repro @lnlife ! Thank you! I've looked closer, and there's a really bad regression all the way back from 3.0.1. I'll be looking deeper into this tomorrow.

divmgl commented 4 months ago

Is this still happening? I was about to go all in on postgres for some of the services in our codebase given the sheer amount of performance problems with Prisma.

lnlife commented 4 months ago

Is this still happening? I was about to go all in on postgres for some of the services in our codebase given the sheer amount of performance problems with Prisma.

Yes it is. I have to set {max:1} in the connection option to stop pool. Even so, it is still the fasted client.

jonataslaw commented 3 months ago

any update on this?