pgbench的应用

pgbench是一种在PostgreSQL上运行基准测试的简单程序。它可能在并发的数据库会话中一遍 一遍地运行相同序列的 SQL 命令,并且计算平均事务率(每秒的事务数)。pgbench支持两种测试方式:
1、默认情况 下,pgbench会测试一种基于 TPC-B 但是要更宽松的场景,其中在每个事务中涉及五 个SELECT、UPDATE以及INSERT命令。
2、用户可以通过编写自己的事务脚本文件很容易用来测 试其他情况。
我们先来看下pgbench默认的测试方法, 默认的类 TPC-B 事务测试要求预先设置好特定的表。可以使用-i(初始化)选项调用pgbench 来创建并且填充这些表(当你在测试一个自定义脚本时,你不需要这一步,但是需要按你自 己的测试需要做一些设置工作)。初始化类似这样:

pgbench -i [ other-options ] dbname
这里我们将比例放大100倍进行测试:

pg12@isdtest-> pgbench -i postgres -s 100
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…

postgres=# /d
List of relations
Schema | Name | Type | Owner
--------±-----------------±------±---------
public | pgbench_accounts | table | postgres
public | pgbench_branches | table | postgres
public | pgbench_history | table | postgres
public | pgbench_tellers | table | postgres
(4 rows)

postgres=# select count(*) from pgbench_accounts ;

count

10000000
(1 row)
可以看到pgbench_accounts这张表有1千万条数据(默认10万).
pgbench默认测试的脚本如下:

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;
初始化默认环境之后,我们开始进行压测:

pg12@isdtest-> pgbench -n -r -T 60 -P 1 -c 64 -j 64
transaction type:
scaling factor: 100
query mode: simple
number of clients: 63
number of threads: 63
duration: 60 s
number of transactions actually processed: 1659519
latency average = 2.281 ms
latency stddev = 64.946 ms
tps = 27586.607969 (including connections establishing)
tps = 27593.617598 (excluding connections establishing)
statement latencies in milliseconds:
0.002 /set aid random(1, 100000 * :scale)
0.001 /set bid random(1, 1 * :scale)
0.001 /set tid random(1, 10 * :scale)
0.001 /set delta random(-5000, 5000)
0.053 BEGIN;
1.116 UPDATE pgbench_accounts SET abalance = abalance + :delta WHERE aid = :aid;
0.166 SELECT abalance FROM pgbench_accounts WHERE aid = :aid;
0.352 UPDATE pgbench_tellers SET tbalance = tbalance + :delta WHERE tid = :tid;
0.299 UPDATE pgbench_branches SET bbalance = bbalance + :delta WHERE bid = :bid;
0.160 INSERT INTO pgbench_history (tid, bid, aid, delta, mtime) VALUES (:tid, :bid, :aid, :delta, CURRENT_TIMESTAMP);
0.133 END;
这种默认的tpcb测试在实际使用中其实用处不大,一般pgbench多是用来通过从一个文件中(-f选项)读取事务脚本替换默认的事务脚本(如上文所述) 来运行自定义的基准场景。在这种情况中,一个“事务”就是一个脚本文件的一次执行。

这里简单测试一个点查的脚本.

构造环境:

postgres=# create table test(
postgres(# id int8 primary key,
postgres(# info text default ‘tessssssssssssssssssssssssssssssssssssst’,
postgres(# state int default 0,
postgres(# crt_time timestamp default now(),
postgres(# mod_time timestamp default now()
postgres(# );
CREATE TABLE
postgres=# insert into test select generate_series(1,10000000);

INSERT 0 10000000
构建脚本:

vi test.sql

/set id random(1,100000000)
select * from test where id=:id;
测试:

pg12@isdtest-> pgbench -M prepared -n -r -P 1 -f ./test.sql -c 32 -j 32 -T 60


transaction type: ./test.sql
scaling factor: 1
query mode: prepared
number of clients: 32
number of threads: 32
duration: 60 s
number of transactions actually processed: 4931686
latency average = 0.388 ms
latency stddev = 1.248 ms
tps = 82187.250560 (including connections establishing)
tps = 82200.894111 (excluding connections establishing)
statement latencies in milliseconds:
0.001 /set id random(1,10000000)
0.385 select * from test where id=:id;
TPS:82187
平均响应时间:0.388 毫秒

你可能感兴趣的:(postgresql)