首先来熟悉一下Postgresql自带的pgbench工具:
Postgresql pgbench:
内部脚本测试:
准备命令(自定义初始化):
pgbench -i -s 20 pgbenchdb
测试命令
pgbench -r -j 2 -c 4 -T 60 pgbenchdb
一些参数说明:
程序自带的脚本如下,包括一条插入语句,三条更新语句和一条查询语句。
\set nbranches :scale
\set ntellers 10 * :scale
\set naccounts 100000 * :scale
\setrandom aid 1 :naccounts
\setrandom bid 1 :nbranches
\setrandom tid 1 :ntellers
\setrandom delta -5000 5000
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;
可以使用自己的脚本进行独立测试,准备一个sql文件,命名为test.sql文件。
\set nbranches :scale
\set ntellers 10 * :scale
\set naccounts 100000 * :scale
\setrandom aid 1 :naccounts
\setrandom bid 1 :nbranches
\setrandom tid 1 :ntellers
\setrandom delta -5000 5000
INSERT INTO pgbench_history (tid, bid, aid, delta, mtime) VALUES (:tid, :bid, :aid, :delta, CURRENT_TIMESTAMP);
运行自己的脚本:
pgbench -rf test.sql -c 4 -j 2 -T 60 pgbenchdb
-l 记录日志log,记录的日志在test.sql所在的文件夹内。
因为 -j 选项为2,多个工作者进程都有自己的日志文件。
日志的格式是:
client_id | transaction_no | time | script_no | time_epoch | time_us [schedule_lag]
pgbench -rf try.sql -c 4 -j 2 -T 60 -n testdb
注意:一定要加 -n ,否则会遇到如下报错,因为testdb不是由pgbench -i 初始化自动生成的。
starting vacuum...
ERROR: relation "pgbench_branches" does not exist(ignoring this error and continuing anyway)
ERROR: relation "pgbench_tellers" does not exist(ignoring this error and continuing anyway)
ERROR: relation "pgbench_history" does not exist(ignoring this error and continuing anyway)
try.sql
实际是tpc-h内置模板 query 6自动生成的。
参考资料:
https://www.postgresql.org/docs/9.5/pgbench.html
https://blog.csdn.net/enzesheng/article/details/42720691
https://blog.phpgao.com/how_to_use_pgbench.html(参数说明)
https://blog.csdn.net/ctypyb2002/article/details/78910990
https://blog.csdn.net/sunbocong/article/details/80021146
https://blog.phpgao.com/how_to_use_pgbench.html