Simular Carga de Clientes con pgbench
Para observar el funcionamiento del agrupamiento de conexiones (connection pooling), utilizará pgbench, una herramienta de benchmarking estándar incluida con PostgreSQL. pgbench puede simular múltiples clientes accediendo a la base de datos de forma concurrente.
Primero, necesita inicializar el entorno de pgbench. Esto crea algunas tablas y las puebla con datos de ejemplo. Ejecute el siguiente comando, asegurándose de conectarse a través del puerto de PgBouncer (6432).
pgbench -i -h 127.0.0.1 -p 6432 -U postgres postgres
Se le pedirá la contraseña (labex_password). La opción -i inicializa la base de datos para el benchmarking. Debería ver una salida que indique que las tablas se han creado y poblado.
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).
Ahora, ejecute el benchmark para simular una carga. El siguiente comando simula 10 clientes concurrentes (-c 10), con cada cliente ejecutando 300 transacciones (-t 300).
pgbench -c 10 -t 300 -h 127.0.0.1 -p 6432 -U postgres postgres
Nuevamente, introduzca la contraseña cuando se le solicite. El benchmark se ejecutará durante unos segundos y luego mostrará un resumen de los resultados, incluyendo el número de transacciones por segundo (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)
Esta prueba generó un tráfico significativo a través de PgBouncer, que inspeccionaremos en el siguiente paso.