標籤:
pgbench是PostgreSQL內建的一個資料庫壓力測試工具,支援TPC-B測試模型, 或自訂測試模型.自訂測試模型支援元命令, 調用shell指令碼, 設定隨機數, 變數等等.支援3種非同步介面[simple|extended|prepared] 參數詳解列表
// TPC-B測試模型的初始化資料參數.Initialization options:-i invokes initialization mode //初始化參數-F NUM fill factor -n do not run VACUUM after initialization // 使用自訂測試模型時, 請帶上這個參數.-q quiet logging (one message each 5 seconds)-s 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// 壓力測試相關參數Benchmarking options:-c NUM number of concurrent database clients (default: 1) // 指定pgbench串連到資料庫的串連數-C establish new connection for each transaction // 是否使用短串連-D VARNAME=VALUE // 設定變數, 在自訂指令碼中使用:varname 引用. 可使用多個-D設定多個變數.-f FILENAME read transaction script from FILENAME // 指定自訂的測試檔案(由元命令和SQL組成), 可使用多個-f 指定多個檔案, 每個檔案作為一個事務, 每次執行事務時隨機播放一個檔案執行.-j NUM number of threads (default: 1) // pgbench的背景工作執行緒. 為-c一起用;為-c的約數-l write transaction times to log file // 開啟事務統計, 輸出檔案名格式pgbench_log.$PID.$threadID , (當-j >= 2時, threadID從1開始)-M simple|extended|prepared // libpq介面 default: simple-n do not run VACUUM before tests // vacuum開關, 使用自訂檔案時, 使用-n關閉vacuum.-N do not update tables "pgbench_tellers" and "pgbench_branches" // TPC-B 非預設測試模式, 少兩個表的更新.-r report average latency per command // 報告測試檔案中每條命令(包括元命令和SQL)的平均執行延遲.-s NUM report this scale factor in output // 使用自訂指令碼測試時, 指定scale的輸出. 沒有實質意義.-S perform SELECT-only transactions // TPC-B 非預設測試模式, 只查詢.-t NUM number of transactions each client runs (default: 10) // 指定每個串連的執行事務數.-T NUM duration of benchmark test in seconds // 指定總的壓力測試時間. 與-t不能同時使用.-v vacuum all four standard tables before tests // 測試前先vacuum 4個和tpc-b相關的表.--aggregate-interval=NUM // 輸出彙總後的事務統計資訊. 與-l連用//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
一 初始化;這裡我使用了自己的資料庫mydb;資料使用者lottu
pgbench -i -s 10 --foreign-keys --unlogged-tables -U lottu mydb
操作之後會在資料庫裡面產生下面的表
mydb=> \dt pgbench* List of relations Schema | Name | Type | Owner --------+------------------+-------+------- public | pgbench_accounts | table | lottu public | pgbench_branches | table | lottu public | pgbench_history | table | lottu public | pgbench_tellers | table | lottu(4 rows)
二 壓力測試示範
[[email protected]_210 ~]$ pgbench -M prepared -r -c 10 -j 2 -T 10 -U lottu mydbstarting vacuum...end. --預設是非n模式transaction type: TPC-B (sort of)scaling factor: 10 --跟上面初始化-s 10一致的query mode: prepared -- -M prepared 預設為simplenumber of clients: 10 --用戶端串連有10個 -c 10number of threads: 2 --線程為2個 -j2duration: 10 s --時間為10s -T 10number of transactions actually processed: 146890latency average: 0.681 mstps = 14687.531247 (including connections establishing)tps = 14690.762892 (excluding connections establishing)statement latencies in milliseconds: 0.001784 \set nbranches 1 * :scale 0.000633 \set ntellers 10 * :scale 0.000483 \set naccounts 100000 * :scale 0.001076 \setrandom aid 1 :naccounts 0.000647 \setrandom bid 1 :nbranches 0.000654 \setrandom tid 1 :ntellers 0.000762 \setrandom delta -5000 5000 0.034427 BEGIN; 0.094350 UPDATE pgbench_accounts SET abalance = abalance + :delta WHERE aid = :aid; 0.064211 SELECT abalance FROM pgbench_accounts WHERE aid = :aid; 0.110995 UPDATE pgbench_tellers SET tbalance = tbalance + :delta WHERE tid = :tid; 0.154680 UPDATE pgbench_branches SET bbalance = bbalance + :delta WHERE bid = :bid; 0.155552 INSERT INTO pgbench_history (tid, bid, aid, delta, mtime) VALUES (:tid, :bid, :aid, :delta, CURRENT_TIMESTAMP); 0.052141 END;--使用-l參數結合 --aggregate-interval 示範[[email protected]_210 ~]$ pgbench -M extended --aggregate-interval 2 -l -c 2 -j 2 -T 10 -U lottu mydbstarting vacuum...end.transaction type: TPC-B (sort of)scaling factor: 10query mode: extendednumber of clients: 2number of threads: 2duration: 10 snumber of transactions actually processed: 22613latency average: 0.884 mstps = 2261.073893 (including connections establishing)tps = 2261.413071 (excluding connections establishing)--這樣會組建檔案[[email protected]_210 ~]$ ll pgbench_log.5160*-rw-rw-r-- 1 postgres postgres 211 Jun 28 16:37 pgbench_log.5160-rw-rw-r-- 1 postgres postgres 210 Jun 28 16:37 pgbench_log.5160.1查看檔案內容可以判斷測試結果![[email protected]_210 ~]$ cat pgbench_log.51601467103061 1864 1663000 1739000000 0 90001467103063 2256 1998000 2000000000 0 20001467103065 2268 1997000 2001000000 0 20001467103067 2271 1996000 1998000000 0 20001467103069 2250 1996000 2268000000 0 17000這5列分別代表interval_start // epoch時間, 指這個統計段的開始時間.num_of_transactions // 這個統計段運行了多少個"事務", 指獨立的檔案運行次數.latency_sum // 這個統計段的事務執行總耗時, 單位微秒.latency_2_sum // 這個統計段的事務執行耗時平方的總和, 單位微秒.min_latency // 這個統計段內, 單個事務的最小耗時.max_latency // 這個統計段內, 單個事務的最大耗時.
--參考德哥視頻講解
postgresql壓力測試工具用法以及參數解讀