PostgreSQL压力测试


1 pgbench

1.1 介绍

  pgbench是PostgreSQL自带的压测工具。

1.2 参数介绍

pgbench --help

点击查看代码
[postgres]$ pgbench --help
pgbench is a benchmarking tool for PostgreSQL.

Usage:
  pgbench [OPTION]... [DBNAME]

Initialization options:
  -i, --initialize         invokes initialization mode
  -F, --fillfactor=NUM     set fill factor
  -n, --no-vacuum          do not run VACUUM after 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
  --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-latencies   report average latency 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
  --progress-timestamp     use Unix epoch timestamps for progress
  --sampling-rate=NUM      fraction of transactions to log (e.g., 0.01 for 1%)

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 .

常用参数:

类型 参数 说明
初始化 -i, --initialize 调用初始化模式
-F, --fillfactor=NUM 填充元素个数
-n, --no-vacuum 初始化后不运行VACUUM
-q, --quiet 记录日志(每5秒记录一条日志)
-s, --scale=NUM 比例因子
--foreign-keys 创建表之间的外键约束
--index-tablespace=TABLESPACE 指定索引默认存储表空间
--tablespace=TABLESPACE 指定表默认存储表空间
--unlogged-tables 指定创建的表不记录日志
select运行选项 -b, --builtin=NAME[@W] 指定运行脚本
-f, --file=FILENAME[@W] 指定运行脚本
-N, --skip-some-updates 跳过pgbench_tellers和pgbench_branches的更新
-S, --select-only 只执行select操作
压测选项 -c, --client=NUM 指定客户端数量
-C, --connect 为每个事务建立新的连接
-D, --define=VARNAME=VALUE 定义变量以供自定义脚本使用
-j, --jobs=NUM 指定线程数
-l, --log 将事务时间写入日志文件
-L, --latency-limit=NUM 将持续时间超过M毫秒的事务计数为延迟
-M, --protocol=simple|extended|prepared 查询的协议(默认:simple)
-n, --no-vacuum 测试前不运行VACUUM操作
-P, --progress=NUM 指定N秒显示线程进度报告
-r, --report-latencies 报告每个命令的平均延迟
-R, --rate=NUM 每秒事务处理的目标速率
-s, --scale=NUM 在输出中报告这个比例因素
-t, --transactions=NUM 每个客户端运行的事务数(默认为10)
-T, --time=NUM 基准测试持续时间(以秒为单位)
-v, --vacuum-all 在测试前清空4个标准表
--aggregate-interval=NUM 指定N秒内聚合数据
--progress-timestamp 进程使用Unix时间戳
--sampling-rate=NUM 事务占日志的百分比(例如,1%占0.01)
常用选项 -d, --debug 输出调试结果
-h, --host=HOSTNAME 数据库主机或socket路径
-p, --port=PORT 数据库端口
-U, --username=USERNAME 指定连接数据库用户名

1.3 压力测试

初始化(生成4个测试表)

[postgres]$ pgbench -h 127.0.0.1 -p 5432 -U postgres --initialize 
Password: 
NOTICE:  table "pgbench_history" does not exist, skipping
NOTICE:  table "pgbench_tellers" does not exist, skipping
NOTICE:  table "pgbench_accounts" does not exist, skipping
NOTICE:  table "pgbench_branches" does not exist, skipping
creating tables...
100000 of 100000 tuples (100%) done (elapsed 0.02 s, remaining 0.00 s)
vacuum...
set primary keys...
done.

200个客户端,10个线程,将事务写入日志文件,每5秒报告进度,每个客户端100个事务,测试前清空标准的测试表

[postgres]$ pgbench -h 127.0.0.1 -p 5432 -U postgres --client=200 --connect --jobs=10 --log --progress=5 --transactions=100 --vacuum-all
Password: 
starting vacuum...end.
starting vacuum pgbench_accounts...end.
progress: 5.0 s, 1217.7 tps, lat 152.132 ms stddev 197.571
progress: 10.0 s, 1025.9 tps, lat 188.665 ms stddev 314.154
progress: 15.0 s, 1185.9 tps, lat 170.930 ms stddev 235.899
transaction type: 
scaling factor: 1
query mode: simple
number of clients: 200
number of threads: 10
number of transactions per client: 100
number of transactions actually processed: 20000/20000
latency average = 157.293 ms
latency stddev = 239.600 ms
tps = 1177.215350 (including connections establishing)
tps = 1189.014326 (excluding connections establishing)

2 sysbench

2.1 安装

源码安装

[root]# git clone https://github.com/akopytov/sysbench.git
[root]# cd sysbench
[root]# sh autogen.sh
[root]# ./configure --with-pgsql --with-pgsql-includes=/usr/local/pgsql/include --with-pgsql-libs=/usr/local/pgsql/lib --without-mysql
[root]# make && make install

查看sysbench版本

[root]# sysbench --version
sysbench 1.1.0-ead2689

2.2 参数介绍

查看帮助手册

点击查看代码
[root]# sysbench --help
Usage:
  sysbench [options]... [testname] [command]

Commands implemented by most tests: prepare run cleanup help

General options:
  --threads=N                     number of threads to use [1]
  --events=N                      limit for total number of events [0]
  --time=N                        limit for total execution time in seconds [10]
  --warmup-time=N                 execute events for this many seconds with statistics disabled before the actual benchmark run with statistics enabled [0]
  --forced-shutdown=STRING        number of seconds to wait after the --time limit before forcing shutdown, or 'off' to disable [off]
  --thread-stack-size=SIZE        size of stack per thread [64K]
  --thread-init-timeout=N         wait time in seconds for worker threads to initialize [30]
  --rate=N                        average transactions rate. 0 for unlimited rate [0]
  --report-interval=N             periodically report intermediate statistics with a specified interval in seconds. 0 disables intermediate reports [0]
  --report-checkpoints=[LIST,...] dump full statistics and reset all counters at specified points in time. The argument is a list of comma-separated values representing the amount of time in seconds elapsed from start of test when report checkpoint(s) must be performed. Report checkpoints are off by default. []
  --debug[=on|off]                print more debugging info [off]
  --validate[=on|off]             perform validation checks where possible [off]
  --help[=on|off]                 print help and exit [off]
  --version[=on|off]              print version and exit [off]
  --config-file=FILENAME          File containing command line options
  --luajit-cmd=STRING             perform LuaJIT control command. This option is equivalent to 'luajit -j'. See LuaJIT documentation for more information

Pseudo-Random Numbers Generator options:
  --rand-type=STRING   random numbers distribution {uniform, gaussian, pareto, zipfian} to use by default [uniform]
  --rand-seed=N        seed for random number generator. When 0, the current time is used as an RNG seed. [0]
  --rand-pareto-h=N    shape parameter for the Pareto distribution [0.2]
  --rand-zipfian-exp=N shape parameter (exponent, theta) for the Zipfian distribution [0.8]

Log options:
  --verbosity=N verbosity level {5 - debug, 0 - only critical messages} [3]

  --percentile=N       percentile to calculate in latency statistics (1-100). Use the special value of 0 to disable percentile calculations [95]
  --histogram[=on|off] print latency histogram in report [off]

General database options:

  --db-driver=STRING  specifies database driver to use ('help' to get list of available drivers)
  --db-ps-mode=STRING prepared statements usage mode {auto, disable} [auto]
  --db-debug[=on|off] print database-specific debug information [off]


Compiled-in database drivers:
  pgsql - PostgreSQL driver

pgsql options:
  --pgsql-host=STRING     PostgreSQL server host [localhost]
  --pgsql-port=N          PostgreSQL server port [5432]
  --pgsql-user=STRING     PostgreSQL user [sbtest]
  --pgsql-password=STRING PostgreSQL password []
  --pgsql-db=STRING       PostgreSQL database name [sbtest]

Compiled-in tests:
  fileio - File I/O test
  cpu - CPU performance test
  memory - Memory functions speed test
  threads - Threads subsystem performance test
  mutex - Mutex performance test

See 'sysbench  help' for a list of options for each test.

常用参数:

类型 参数 说明
一般参数