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 <pgsql-bugs@postgresql.org>.

 

常用參數:

類型 參數 說明
初始化 -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: <builtin: TPC-B (sort of)>
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 介紹

sysbench是一款開源的多線程性能壓測工具,可以對CPU、內存、線程、IO、數據庫等進行性能壓力測試。

2.2 安裝

源碼安裝

[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.3 參數介紹

查看幫助手冊

點擊查看代碼
[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 <testname> help' for a list of options for each test.

 

常用參數:

類型 參數 說明
一般參數 --threads=N 指定線程數量(默認是1)
--events=N 指定總事件數(默認0)
--time=N 指定壓測運行時間(默認是10秒)
--warmup-time=N 在啟用統計信息的情況下運行實際基准測試之前,在禁用統計信息的情況下執行這一秒的事件(默認0)
--forced-shutdown=STRING --time參數之后,指定是否強制關機(默認off)
--thread-stack-size=SIZE 每個線程的堆棧大小(默認64K)
--thread-init-timeout=N 以秒為單位的工作線程初始化的等待時間(默認30秒)
--rate=N 平均事務率。0表示無限利率(默認0)
--report-interval=N 以秒為單位,定期報告中間統計信息。0禁用中間報告(默認0)
--report-checkpoints=[LIST,...] 轉儲全部統計數據並在指定的時間點重置所有計數器。該參數是一個以逗號分隔的值列表,表示從測試開始到必須執行報告檢查點時所經過的時間(以秒為單位)。默認情況下,報告檢查點是關閉的。
--debug[=on|off] 打印更多調試信息(默認off)
--validate[=on|off] 在可能的情況下執行驗證檢查(off)
--help[=on|off] 打印幫助並退出(默認off)
--version[=on|off] 打印版本並退出(默認off)
--config-file=FILENAME 包含命令行選項的文件
偽隨機數生成器選項 --rand-type=STRING 默認使用隨機數分布{uniform, gaussian, pareto, zipfian}(默認uniform)
--rand-seed=N 隨機數生成器的種子。當為0時,當前時間被用作RNG種子。
--rand-pareto-h=N 形狀參數的帕累托分布(默認0.2)
--rand-zipfian-exp=N 形狀參數(exponent, theta)為Zipfian分布(默認0.8)
LOG選項 --verbosity=N 日志詳細級別{5 - debug, 0 - only critical messages}(默認3)
--percentile=N 在延遲統計中要計算的百分比(1-100)。使用特殊值0禁用百分比計算(默認95)
--histogram[=on|off] 在報告中打印延遲直方圖(默認off)
數據庫選項 --db-driver=STRING 指定要使用的數據庫驅動程序
--db-ps-mode=STRING 預置語句使用模式{auto, disable}(默認auto)
--db-debug[=on|off] 打印特定於數據庫的調試信息(默認off)
數據庫驅動  pgsql PostgreSQL數據庫驅動
pgsql選項 --pgsql-host=STRING 連接PostgreSQL的主機(默認是localhost)
--pgsql-port=N 連接PostgreSQL的服務端口(默認是5432)
--pgsql-user=STRING 連接PostgreSQL的用戶(默認是sbtest)
--pgsql-password=STRING 連接PostgreSQL的用戶密碼
--pgsql-db=STRING 連接PostgreSQL的數據庫名稱(默認是sbtest)
其他 fileio 文件系統I/O測試
cpu CPU性能測試
memory 內存功能速度測試
threads 線程子系統性能測試
mutex 互斥鎖的性能測試

2.4 壓力測試

提示:

在sysbench/src/lua路徑有十幾種壓測腳本,應根據壓測目的選擇對應的壓測腳本。

select_random_ranges.lua
select_random_points.lua
prime-test.lua
oltp_write_only.lua
oltp_update_non_index.lua
oltp_update_index.lua
oltp_read_write.lua
oltp_read_only.lua
oltp_point_select.lua
oltp_insert.lua
oltp_delete.lua
oltp_common.lua
empty-test.lua
bulk_insert.lua

 

准備數據

創建10張測試表,每張表100萬行

[postgres]$ sysbench /home/postgres/sysbench/src/lua/oltp_insert.lua \
 --db-driver=pgsql \
 --pgsql-host=127.0.0.1 \
 --pgsql-port=5432 \
 --pgsql-user=postgres \
 --pgsql-password=Passwd@123 \
 --pgsql-db=postgres \
 --tables=10 \
 --table-size=1000000 \
 prepare

sysbench 1.1.0-ead2689 (using bundled LuaJIT 2.1.0-beta3)

Creating table 'sbtest1'...
Inserting 1000000 records into 'sbtest1'
Creating a secondary index on 'sbtest1'...
Creating table 'sbtest2'...
Inserting 1000000 records into 'sbtest2'
Creating a secondary index on 'sbtest2'...
Creating table 'sbtest3'...
Inserting 1000000 records into 'sbtest3'
Creating a secondary index on 'sbtest3'...
Creating table 'sbtest4'...
Inserting 1000000 records into 'sbtest4'
Creating a secondary index on 'sbtest4'...
Creating table 'sbtest5'...
Inserting 1000000 records into 'sbtest5'
Creating a secondary index on 'sbtest5'...
Creating table 'sbtest6'...
Inserting 1000000 records into 'sbtest6'
Creating a secondary index on 'sbtest6'...
Creating table 'sbtest7'...
Inserting 1000000 records into 'sbtest7'
Creating a secondary index on 'sbtest7'...
Creating table 'sbtest8'...
Inserting 1000000 records into 'sbtest8'
Creating a secondary index on 'sbtest8'...
Creating table 'sbtest9'...
Inserting 1000000 records into 'sbtest9'
Creating a secondary index on 'sbtest9'...
Creating table 'sbtest10'...
Inserting 1000000 records into 'sbtest10'
Creating a secondary index on 'sbtest10'...

 

壓力測試

10張測試表,每張表100萬行,每5秒打印測試結果,100個線程,總運行100秒

[root]$ sysbench /home/postgres/sysbench/src/lua/oltp_insert.lua \
 --db-driver=pgsql \
 --pgsql-host=127.0.0.1 \
 --pgsql-port=5432 \
 --pgsql-user=postgres \
 --pgsql-password=Passwd@123 \
 --pgsql-db=postgres \
 --tables=10 \
 --table-size=1000000 \
 --report-interval=5 \
 --threads=100 \
 --time=60 \
 run

sysbench 1.1.0-ead2689 (using bundled LuaJIT 2.1.0-beta3)

Running the test with following options:
Number of threads: 100
Report intermediate results every 5 second(s)
Initializing random number generator from current time


Initializing worker threads...

Threads started!

[ 5s ] thds: 100 tps: 74693.84 qps: 74693.84 (r/w/o: 0.00/74693.84/0.00) lat (ms,95%): 3.96 err/s: 0.00 reconn/s: 0.00
[ 10s ] thds: 100 tps: 77222.15 qps: 77222.15 (r/w/o: 0.00/77222.15/0.00) lat (ms,95%): 3.89 err/s: 0.00 reconn/s: 0.00
[ 15s ] thds: 100 tps: 76497.68 qps: 76497.68 (r/w/o: 0.00/76497.68/0.00) lat (ms,95%): 3.89 err/s: 0.00 reconn/s: 0.00
[ 20s ] thds: 100 tps: 78223.86 qps: 78223.86 (r/w/o: 0.00/78223.86/0.00) lat (ms,95%): 3.68 err/s: 0.00 reconn/s: 0.00
[ 25s ] thds: 100 tps: 77842.72 qps: 77842.92 (r/w/o: 0.00/77842.92/0.00) lat (ms,95%): 3.89 err/s: 0.00 reconn/s: 0.00
[ 30s ] thds: 100 tps: 77398.63 qps: 77398.43 (r/w/o: 0.00/77398.43/0.00) lat (ms,95%): 3.82 err/s: 0.00 reconn/s: 0.00
[ 35s ] thds: 100 tps: 72489.04 qps: 72489.04 (r/w/o: 0.00/72489.04/0.00) lat (ms,95%): 3.96 err/s: 0.00 reconn/s: 0.00
[ 40s ] thds: 100 tps: 76023.09 qps: 76023.09 (r/w/o: 0.00/76023.09/0.00) lat (ms,95%): 3.82 err/s: 0.00 reconn/s: 0.00
[ 45s ] thds: 100 tps: 75742.57 qps: 75742.57 (r/w/o: 0.00/75742.57/0.00) lat (ms,95%): 3.82 err/s: 0.00 reconn/s: 0.00
[ 50s ] thds: 100 tps: 75072.73 qps: 75072.73 (r/w/o: 0.00/75072.73/0.00) lat (ms,95%): 3.82 err/s: 0.00 reconn/s: 0.00
[ 55s ] thds: 100 tps: 75156.60 qps: 75156.80 (r/w/o: 0.00/75156.80/0.00) lat (ms,95%): 3.89 err/s: 0.00 reconn/s: 0.00
[ 60s ] thds: 100 tps: 73926.29 qps: 73926.09 (r/w/o: 0.00/73926.09/0.00) lat (ms,95%): 3.89 err/s: 0.00 reconn/s: 0.00
SQL statistics:
    queries performed:
        read:                            0
        write:                           4551688
        other:                           0
        total:                           4551688
    transactions:                        4551688 (75831.07 per sec.)
    queries:                             4551688 (75831.07 per sec.)
    ignored errors:                      0      (0.00 per sec.)
    reconnects:                          0      (0.00 per sec.)

Throughput:
    events/s (eps):                      75831.0714
    time elapsed:                        60.0240s
    total number of events:              4551688

Latency (ms):
         min:                                    0.09
         avg:                                    1.32
         max:                                   47.33
         95th percentile:                        3.89
         sum:                              5990512.42

Threads fairness:
    events (avg/stddev):           45516.8800/281.21
    execution time (avg/stddev):   59.9051/0.01

 

清除壓測數據

[root]$ sysbench /home/postgres/sysbench/src/lua/oltp_insert.lua \
 --db-driver=pgsql \
 --pgsql-host=127.0.0.1 \
 --pgsql-port=5432 \
 --pgsql-user=postgres \
 --pgsql-password=Passwd@123 \
 --pgsql-db=postgres \
 --tables=10 \
 cleanup
 
sysbench 1.1.0-ead2689 (using bundled LuaJIT 2.1.0-beta3)

Dropping table 'sbtest1'...
Dropping table 'sbtest2'...
Dropping table 'sbtest3'...
Dropping table 'sbtest4'...
Dropping table 'sbtest5'...
Dropping table 'sbtest6'...
Dropping table 'sbtest7'...
Dropping table 'sbtest8'...
Dropping table 'sbtest9'...
Dropping table 'sbtest10'...

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM