PostgreSQL自带的命令行工具11- pgbench
基础信息
OS版本:Red Hat Enterprise Linux Server release 7.9 (Maipo)
DB版本:16.2
pg软件目录:/home/pg16/soft
pg数据目录:/home/pg16/data
端口:5777
pgbench
是 PostgreSQL 附带的一个强大的基准测试工具,它被设计来评估 PostgreSQL 在不同类型的事务和查询负载下的性能表现。pgbench
可以模拟客户端请求,执行多种类型的数据库操作,包括基本的 SQL 语句执行和复杂的事务处理,从而帮助数据库管理员或开发人员理解当前数据库配置下的性能瓶颈所在。
通过help查看帮助文档。
[pg16@test ~]$ pgbench --help
pgbench is a benchmarking tool for PostgreSQL.
Usage:
pgbench [OPTION]... [DBNAME]
Initialization options:
-i, --initialize invokes initialization mode
-I, --init-steps=[dtgGvpf]+ (default "dtgvp")
run selected initialization steps
-F, --fillfactor=NUM set fill factor
-n, --no-vacuum do not run VACUUM during initialization
-q, --quiet quiet logging (one message each 5 seconds)
-s, --scale=NUM scaling factor
--foreign-keys create foreign key constraints between tables
--index-tablespace=TABLESPACE
create indexes in the specified tablespace
--partition-method=(range|hash)
partition pgbench_accounts with this method (default: range)
--partitions=NUM partition pgbench_accounts into NUM parts (default: 0)
--tablespace=TABLESPACE create tables in the specified tablespace
--unlogged-tables create tables as unlogged tables
Options to select what to run:
-b, --builtin=NAME[@W] add builtin script NAME weighted at W (default: 1)
(use "-b list" to list available scripts)
-f, --file=FILENAME[@W] add script FILENAME weighted at W (default: 1)
-N, --skip-some-updates skip updates of pgbench_tellers and pgbench_branches
(same as "-b simple-update")
-S, --select-only perform SELECT-only transactions
(same as "-b select-only")
Benchmarking options:
-c, --client=NUM number of concurrent database clients (default: 1)
-C, --connect establish new connection for each transaction
-D, --define=VARNAME=VALUE
define variable for use by custom script
-j, --jobs=NUM number of threads (default: 1)
-l, --log write transaction times to log file
-L, --latency-limit=NUM count transactions lasting more than NUM ms as late
-M, --protocol=simple|extended|prepared
protocol for submitting queries (default: simple)
-n, --no-vacuum do not run VACUUM before tests
-P, --progress=NUM show thread progress report every NUM seconds
-r, --report-per-command report latencies, failures, and retries per command
-R, --rate=NUM target rate in transactions per second
-s, --scale=NUM report this scale factor in output
-t, --transactions=NUM number of transactions each client runs (default: 10)
-T, --time=NUM duration of benchmark test in seconds
-v, --vacuum-all vacuum all four standard tables before tests
--aggregate-interval=NUM aggregate data over NUM seconds
--failures-detailed report the failures grouped by basic types
--log-prefix=PREFIX prefix for transaction time log file
(default: "pgbench_log")
--max-tries=NUM max number of tries to run transaction (default: 1)
--progress-timestamp use Unix epoch timestamps for progress
--random-seed=SEED set random seed ("time", "rand", integer)
--sampling-rate=NUM fraction of transactions to log (e.g., 0.01 for 1%)
--show-script=NAME show builtin script code, then exit
--verbose-errors print messages of all errors
Common options:
-d, --debug print debugging output
-h, --host=HOSTNAME database server host or socket directory
-p, --port=PORT database server port number
-U, --username=USERNAME connect as specified database user
-V, --version output version information, then exit
-?, --help show this help, then exit
Report bugs to pgsql-bugs@lists.postgresql.org.
PostgreSQL home page: https://www.postgresql.org/
基本用法
-
初始化数据库:在运行基准测试之前,你需要使用
pgbench
创建测试用的数据库。这个过程会生成一些标准的测试表和数据。pgbench -i -s [缩放因子] [数据库名]
-i
选项表示初始化测试数据库,-s
指定了缩放因子,决定了测试数据的规模(默认为 1,即约 100,000 条记录)。较高的缩放因子会生成更多的数据,用于模拟更大的数据库环境。 -
运行基准测试:初始化数据库后,你可以开始执行基准测试。
pgbench -c [并发数] -j [线程数] -T [持续时间] [数据库名]
-c
指定客户端的数量,即并发连接数;-j
设置用于测试的线程数(通常与-c
一致或小于-c
);-T
定义了测试的总持续时间(秒)。
常用选项
-n
:不执行内建事务测试脚本的初始清除步骤。-t
:为每个客户端执行的事务总数。-P
:每隔指定秒数报告一次中间结果。-f
:指定自定义 SQL 脚本文件进行测试。-N
:执行只包含非事务性查询的自定义测试。-S
:执行只包含 SELECT 语句的脚本,评估纯查询性能。
示例
- 初始化数据库:
[pg16@test ~]$ pgbench -i -s 10 white
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)...
1000000 of 1000000 tuples (100%) done (elapsed 3.02 s, remaining 0.00 s)
vacuuming...
creating primary keys...
done in 4.08 s (drop tables 0.00 s, create tables 0.01 s, client-side generate 3.07 s, vacuum 0.14 s, primary keys 0.86 s).
这将为 white
数据库初始化测试数据,使用缩放因子 10。
- 执行基准测试:
[pg16@test ~]$ pgbench -c 10 -j 2 -T 60 white
pgbench (16.2)
starting vacuum...end.
transaction type: <builtin: TPC-B (sort of)>
scaling factor: 10
query mode: simple
number of clients: 10
number of threads: 2
maximum number of tries: 1
duration: 60 s
number of transactions actually processed: 267227
number of failed transactions: 0 (0.000%)
latency average = 2.245 ms
initial connection time = 18.248 ms
tps = 4454.341017 (without initial connection time)
这个命令将在 white
数据库上以 10 个并发客户端、2 个线程进行 60 秒的基准测试。
注意事项
pgbench
进行基准测试期间可能会对数据库服务器产生显著负载,可能影响其他应用或服务。建议在非生产环境或低峰时段进行测试。- 通过修改测试脚本或使用
-f
选项指定自定义查询,可以让pgbench
更贴合实际应用场景的测试需求。 - 基准测试结果受多种因素影响,如硬件配置、数据库设置、查询优化等,因此解读结果时需要综合考虑这些因素。
pgbench
是评估 PostgreSQL 性能的有力工具,合理使用能够帮助优化数据库配置和架构设计。
谨记:心存敬畏,行有所止。