使用 pgbench 进行数据库压力测试

pgbench 是对 PostgreSQL 进行压力测试的一款简单程序, SQL 命令可以在一个连接中顺序地
执行,通常会开多个数据库 Session, 并且在测试最后形成测试报告,得出每秒平均事务数,pgbench
可以测试 select,update,insert,delete 命令,用户可以编写自己的脚本进行测试,下面是 pgbench
的简单使用过程。


--1 初始化
语法: pgbench -i [ other-options ] dbname
[postgres@pg1 bin]$ pgbench -i  -s 10 skytf  -U skytf
NOTICE:  table "pgbench_branches" does not exist, skipping
NOTICE:  table "pgbench_tellers" does not exist, skipping
NOTICE:  table "pgbench_accounts" does not exist, skipping
NOTICE:  table "pgbench_history" does not exist, skipping
creating tables...


--2 数据情况
table                   # of rows
---------------------------------
pgbench_branches        10
pgbench_tellers         100
pgbench_accounts        1000000
pgbench_history         0


--3 测试的事务脚本
\set nbranches :scale
\set ntellers 10 * :scale
\set naccounts 100000 * :scale
\setrandom delta -5000 5000
\setrandom aid 1 :naccounts
\setrandom bid 1 :nbranches
\setrandom tid 1 :ntellers

BEGIN;
UPDATE pgbench_accounts SET abalance = abalance + :delta WHERE aid = :aid;
SELECT abalance FROM pgbench_accounts WHERE aid = :aid;
UPDATE pgbench_tellers SET tbalance = tbalance + :delta WHERE tid = :tid;
UPDATE pgbench_branches SET bbalance = bbalance + :delta WHERE bid = :bid;
INSERT INTO pgbench_history (tid, bid, aid, delta, mtime) VALUES (:tid, :bid, :aid, :delta, CURRENT_TIMESTAMP);
END;

    备注:上面是 pgbench 自带的测试脚本,用户也可以自己编写测试脚本。


你可能感兴趣的:(使用 pgbench 进行数据库压力测试)