1.初始化:
pgbench -i pgbench
如果端口號有變化,需要先手動創建數據庫,再執行。
pgbench -i -s 10 -p 5433 pgbench
重點:主要用到兩個參數,-i:初始化模式,-s 插入的倍數,默認是1,即插入100000條;也就是執行多少次generate_series(1,100000)。
2.開始測試:
pgbench -c 96 -j 12 -T 20 -r -p 5433 pgbench
-bash-4.1$ pgbench -c 96 -j 12 -T 20 -r -p 5433 pgbench
starting vacuum...end. transaction type: TPC-B (sort of) scaling factor: 1 query mode: simple number of clients: 96 number of threads: 12 duration: 20 s number of transactions actually processed: 1536 latency average: 1345.443 ms tps = 71.351954 (including connections establishing) tps = 71.400363 (excluding connections establishing) statement latencies in milliseconds: 0.003744 \set nbranches 1 * :scale 0.001051 \set ntellers 10 * :scale 0.000855 \set naccounts 100000 * :scale 0.001345 \setrandom aid 1 :naccounts 0.000850 \setrandom bid 1 :nbranches 0.000680 \setrandom tid 1 :ntellers 0.000797 \setrandom delta -5000 5000 0.425127 BEGIN; 3.685616 UPDATE pgbench_accounts SET abalance = abalance + :delta WHERE aid = :aid; 0.189167 SELECT abalance FROM pgbench_accounts WHERE aid = :aid; 1144.710503 UPDATE pgbench_tellers SET tbalance = tbalance + :delta WHERE tid = :tid; 121.211975 UPDATE pgbench_branches SET bbalance = bbalance + :delta WHERE bid = :bid; 0.194937 INSERT INTO pgbench_history (tid, bid, aid, delta, mtime) VALUES (:tid, :bid, :aid, :delta, CURRENT_TIMESTAMP); 13.740971 END;
主要參數:
-c 總連接數,創建多少個連接到數據庫,一般數據庫接受連接數默認為100,其中需要預留3個左右的連接。
-j 進程數量,每個進程創建n個連接,那么就存在如下關系:-c = -j *n。
-T 測試持續時間,指定了-T就不能指定-t,每個連接執行的事物數量。即,要么指定測試多長時間,要么指定測試多少個事物。
-r 顯示每一步操作的平均時間。
-f 指定測試腳本,不指定則使用默認腳本。
3.測試結果分析
可以看看設置的參數,也可以看到TPS,當然可以算到QPS,也可以看到系統的讀寫更新的速度,知道該往哪方面優化。
4.pgbench初始化參數:
pgbench accepts the following command-line initialization arguments: -F fillfactor Create the pgbench_accounts, pgbench_tellers and pgbench_branches tables with the given fillfactor. Default is 100. -i Required to invoke initialization mode. -s scale_factor Multiply the number of rows generated by the scale factor. For example, -s 100 will create 10,000,000 rows in the pgbench_accounts table. Default is 1.
5.pgbench 壓力測試參數:
pgbench accepts the following command-line benchmarking arguments: -c clients Number of clients simulated, that is, number of concurrent database sessions. Default is 1. -C Establish a new connection for each transaction, rather than doing it just once per client session. This is useful to measure the connection overhead. -d Print debugging output. -D varname=value Define a variable for use by a custom script (see below). Multiple -D options are allowed. -f filename Read transaction script from filename. See below for details. -N, -S, and -f are mutually exclusive. -j threads Number of worker threads within pgbench. Using more than one thread can be helpful on multi-CPU machines. The number of clients must be a multiple of the number of threads, since each thread is given the same number of client sessions to manage. Default is 1. -l Write the time taken by each transaction to a log file. See below for details. -M querymode Protocol to use for submitting queries to the server: simple: use simple query protocol. extended: use extended query protocol. prepared: use extended query protocol with prepared statements. The default is simple query protocol. (See Chapter 46 for more information.) -n Perform no vacuuming before running the test. This option is necessary if you are running a custom test scenario that does not include the standard tables pgbench_accounts, pgbench_branches, pgbench_history, and pgbench_tellers. -N Do not update pgbench_tellers and pgbench_branches. This will avoid update contention on these tables, but it makes the test case even less like TPC-B. -s scale_factor Report the specified scale factor in pgbench is output. With the built-in tests, this is not necessary; the correct scale factor will be detected by counting the number of rows in the pgbench_branches table. However, when testing custom benchmarks (-f option), the scale factor will be reported as 1 unless this option is used. -S Perform select-only transactions instead of TPC-B-like test. -t transactions Number of transactions each client runs. Default is 10. -T seconds Run the test for this many seconds, rather than a fixed number of transactions per client. -t and -T are mutually exclusive. -v Vacuum all four standard tables before running the test. With neither -n nor -v, pgbench will vacuum the pgbench_tellers and pgbench_branches tables, and will truncate pgbench_history.
6.默認測試腳本:
\set nbranches :scale \set ntellers 10 * :scale \set naccounts 100000 * :scale \setrandom delta -5000 5000 \setrandom aid 1 :naccounts \setrandom bid 1 :nbranches \setrandom tid 1 :ntellers BEGIN; UPDATE pgbench_accounts SET abalance = abalance + :delta WHERE aid = :aid; SELECT abalance FROM pgbench_accounts WHERE aid = :aid; UPDATE pgbench_tellers SET tbalance = tbalance + :delta WHERE tid = :tid; UPDATE pgbench_branches SET bbalance = bbalance + :delta WHERE bid = :bid; INSERT INTO pgbench_history (tid, bid, aid, delta, mtime) VALUES (:tid, :bid, :aid, :delta, CURRENT_TIMESTAMP); END; 備注:上面是 pgbench 自帶的測試腳本,用戶也可以自己編寫測試腳本。
補充20190619:
7.在pg11上測試時,\setrandom報錯,修改腳本:
\set nbranches :scale \set ntellers 10 * :scale \set naccounts 100000 * :scale \set delta random(-5000,5000) \set aid random(1,:naccounts) \set bid random(1,:nbranches) \set tid random(1,:ntellers) BEGIN; UPDATE pgbench_accounts SET abalance = abalance + :delta WHERE aid = :aid; SELECT abalance FROM pgbench_accounts WHERE aid = :aid; UPDATE pgbench_tellers SET tbalance = tbalance + :delta WHERE tid = :tid; UPDATE pgbench_branches SET bbalance = bbalance + :delta WHERE bid = :bid; INSERT INTO pgbench_history (tid, bid, aid, delta, mtime) VALUES (:tid, :bid, :aid, :delta, CURRENT_TIMESTAMP); END;
二、如果我們有繪圖需要,一般可以這樣解決:
1)使用pgbench 選用-P 選項,多少s報告一次進程的tps等信息,將這些信息可以放到excel或者Gnuplot、Matlab中繪圖。
2)使用pgbench_tools來做綜合測試,並給出一個趨勢報告圖。
下面是對pgbench_tools的介紹和使用:
https://github.com/kuang17/pgbench-tools
需要注意的是:
1)PG10修改了幾個函數,例如pg_current_xlog_location變為了pg_current_wal_lsn。
2)在測試腳本中,\setrandom 需要對應的修改為\set xxx random(xxx, xxx)的方式。
3)在繪圖時,需要先按照Gnuplot繪圖工具及相關的字體。
yum install gnuplot -y
wget http://olea.org/paquetes-rpm//msttcore-fonts-2.0-6.noarch.rpm
rpm -ivh msttcore-fonts-2.0-6.noarch.rpm
修改/etc/profile或~/.bashrc,這樣設置可以固定下來。
export GDFONTPATH="/usr/share/fonts/msttcore"
export GNUPLOT_DEFAULT_GDFONT="arial"
. /etc/profile