TPC-C是经典的衡量在线事务处理(OLTP)系统性能和可伸缩性的基准测试规范。常见的开源数据库的基准测试工具有benchmarksql、 sysbench等,PostgreSQL自带运行基准测试的简单程序pgbench。pgbench是一个类TPC-B 的基准测试工具,可以执行内置的测试脚本,也可以自定义脚本文件。
官方文档:
PostgreSQL: Documentation: 15: pgbench
https://www.postgresql.org/docs/15/pgbench.html
使用pgbench --help可以查询pgbecnh的帮助信息。
01
—
初始化pgbench测试数据
使用下面的命令初始化测试数据
代码语言:javascript复制-bash-4.2$ pgbench -i -s 2 -F 80 -U postgres
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)...
200000 of 200000 tuples (100%) done (elapsed 0.93 s, remaining 0.00 s)
vacuuming...
creating primary keys...
done in 1.69 s (drop tables 0.00 s, create tables 0.07 s, client-side generate 1.07 s, vacuum 0.19 s, primary keys 0.36 s).
02
—
使用内置脚本进行测试
pgbench 的内置测试脚本有tpcb-like、s simple-update 和 select-only 三种 可以通过以下 命令查看当前版本的p pgbench包含哪些集成的测试脚本:
代码语言:javascript复制-bash-4.2$ pgbench -b list
Available builtin scripts:
tpcb-like: <builtin: TPC-B (sort of)>
simple-update: <builtin: simple update>
select-only: <builtin: select only>
下面的测试使用默认的tpcb-like方式进行10秒钟、10个客户端的测试:
代码语言:javascript复制-bash-4.2$ pgbench -U postgres -T 10 -c 10
pgbench (14.3)
starting vacuum...end.
transaction type: <builtin: TPC-B (sort of)>
scaling factor: 2
query mode: simple
number of clients: 10
number of threads: 1
duration: 10 s
number of transactions actually processed: 4056
latency average = 24.613 ms
initial connection time = 50.735 ms
tps = 406.295741 (without initial connection time)
下面的测试使用默认的simple-update方式进行测试:
代码语言:javascript复制-bash-4.2$ pgbench -b simple-update -U postgres -T 10 -c 10
pgbench (14.3)
starting vacuum...end.
transaction type: <builtin: simple update>
scaling factor: 2
query mode: simple
number of clients: 10
number of threads: 1
duration: 10 s
number of transactions actually processed: 15846
latency average = 6.289 ms
initial connection time = 47.659 ms
tps = 1590.073191 (without initial connection time)
03
—
使用自定义脚本进行测试
创建一个表
代码语言:javascript复制CREATE TABLE tb1 (
id SERIAL PRIMARY KEY,
ncode INT
);
向表中插入数据的脚本
代码语言:javascript复制-bash-4.2$ cat Insert.sql
set ncode random(1,900000)
INSERT INTO tb1 (ncode) VALUES (:ncode) ;
运行pgbench 使用该脚本进行测试:
代码语言:javascript复制-bash-4.2$ pgbench -f Insert.sql -U postgres
pgbench (14.3)
starting vacuum...end.
transaction type: Insert.sql
scaling factor: 1
query mode: simple
number of clients: 1
number of threads: 1
number of transactions per client: 10
number of transactions actually processed: 10/10
latency average = 9.194 ms
initial connection time = 5.623 ms
tps = 108.761855 (without initial connection time)
检查一下插入的数据
代码语言:javascript复制postgres=# select * from tb1;
id | ncode
---- --------
1 | 247869
2 | 595899
3 | 285595
4 | 179089
5 | 408114
6 | 507809
7 | 453365
8 | 231417
9 | 306921
10 | 533048
(10 rows)