Simulate Client Load with pgbench
To observe connection pooling in action, you will use pgbench, a standard benchmarking tool included with PostgreSQL. pgbench can simulate multiple clients concurrently accessing the database.
First, you need to initialize the pgbench environment. This creates a few tables and populates them with sample data. Run the following command, making sure to connect through the PgBouncer port (6432).
pgbench -i -h 127.0.0.1 -p 6432 -U postgres postgres
You will be prompted for the password (labex_password). The -i flag initializes the database for benchmarking. You should see output indicating that the tables have been created and populated.
dropping old tables...
NOTICE: table "pgbench_accounts" does not exist, skipping
NOTICE: table "pgbench_branches" does not exist, skipping
NOTICE: table "pgbench_history" does not exist, skipping
NOTICE: table "pgbench_tellers" does not exist, skipping
creating tables...
generating data (client-side)...
100000 of 100000 tuples (100%) done (elapsed 0.01 s, remaining 0.00 s)
vacuuming...
creating primary keys...
done in 0.13 s (drop tables 0.00 s, create tables 0.00 s, client-side generate 0.06 s, vacuum 0.04 s, primary keys 0.02 s).
Now, run the benchmark to simulate a load. The following command simulates 10 concurrent clients (-c 10), with each client running 300 transactions (-t 300).
pgbench -c 10 -t 300 -h 127.0.0.1 -p 6432 -U postgres postgres
Again, enter the password when prompted. The benchmark will run for a few seconds and then display a summary of the results, including the number of transactions per second (tps).
pgbench (14.18 (Ubuntu 14.18-0ubuntu0.22.04.1))
starting vacuum...end.
transaction type: <builtin: TPC-B (sort of)>
scaling factor: 1
query mode: simple
number of clients: 10
number of threads: 1
number of transactions per client: 300
number of transactions actually processed: 3000/3000
latency average = 5.935 ms
initial connection time = 1.342 ms
tps = 1685.027854 (without initial connection time)
This test generated significant traffic through PgBouncer, which we will inspect in the next step.