简介
pgbench是一种在postgresql上运行基准测试的简单程序, 它是pg自带的工具;pgbench是一种在postgresql上运行基准测试的简单程序。它可能在并发的数据库会话中一遍一遍地运行相同序列的 sql 命令,并且计算平均事务率(每秒的事务数)。默认情况下,pgbench会测试一种基于 tpc-b 但是要更宽松的场景,其中在每个事务中涉及五个select、update以及insert命令。但是,通过编写自己的事务脚本文件很容易用来测试其他情况。测试的目的是了解硬件的处理能力;通过调整参数优化数据库事务处理性能。
pgbench 的测试结果报告
transaction type: <builtin: tpc-b (sort of)>
scaling factor: 500
query mode: prepared
number of clients: 512
number of threads: 10
duration: 600 s
number of transactions actually processed: 562451
latency average = 546.467 ms
initial connection time = 176.577 ms
tps = 936.927622 (without initial connection time)
transaction type 表明本次测试所使用的测试类型
scaling factor 表明pgbench在初始化时设置的数据量的比例因子
query mode 表明指定的查询模式,包括 simple查询模式(默认)、extended查询模式和prepared 查询模式
number of clients表明指定的客户端连接数
number of threads表明测试时每个客户端的线程数
number of transactions actually processed 测试结束时实际处理的事务数
latency average 测试过程的平均响应时间
tps 单位时间内执行的事务数
pgbench工具的使用
1 通过内置脚本进行测试
1.1 初始化测试数据
pgbench -i [ other-options ] dbname
pgbench -i会创建四个表pgbench_accounts、 pgbench_branches、pgbench_history以及pgbench_tellers,如果同名表已经存在会被先删除。如果你已经有同名表,一定注意要使用另一个数据库!
pgbench -i testdb
table_name | counts |
---|---|
pgbench_accounts | 100000 |
pg_branches | 1 |
pgbench_history | 0 |
pg_tellers | 10 |
如果指定 -s 参数可以放大数据量: pgbench -i -s 500 testdb
table_name | counts |
---|---|
pgbench_accounts | 50000000 |
pg_branches | 50 |
pgbench_history | 0 |
pg_tellers | 500 |
基准选项
pgbench接受下列命令行基准参数:
测试案例
内置:
create database testdb;
pgbench -i -f 90 -s 500 testdb -p 5432 -u postgres -d postgres // 初始化,填充率90%,放大倍数500
pgbench -c 256 -j 10 -m prepared -n -t 600 -r -h 10.229.89.212 -p 5678 -u pg14 -d postgres
// 256 客户端连接, 每个客户端 10个线程, prepared 查询协议, 运行时间 600s,
pgbench -c 256 -j 10 -m prepared -n -t 10000 -r -h 10.229.89.212 -p 5678 -u pg14
-d postgres >> 、home/postgres/test_data/1000_transaction_test.log
// 256 客户端连接, 每个客户端 10个线程, prepared 查询协议,运行事务数 10000,将结果输出至指定日志
-t 与 -t 互斥
自定义测试
1 首先在指定数据库创建测试表
2 准备数据
3 测试结果
pgbench -f insert.sql -c 10 -j 10 -m prepared -n -t 600 -r -h 10.229.89.212 -p 5678 -u wp_pg14 -d postgres
transaction type: insert.sql
scaling factor: 1
query mode: prepared
number of clients: 10
number of threads: 10
duration: 600 s
number of transactions actually processed: 11980
latency average = 501.071 ms
initial connection time = 4.248 ms
tps = 19.957239 (without initial connection time)
statement latencies in milliseconds:
501.163 \sleep 500ms
0.065 \set id random(1,100000)
0.036 \set age random(18,32)
0.453 insert into test(id, age) values(:id, :age);
发表评论