本篇內容主要講解“PostgreSQL中pgbench有什么作用”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實用性強。下面就讓小編來帶大家學習“PostgreSQL中pgbench有什么作用”吧!
pgbench是面向PostgreSQL的一個基準測試工具。默認情況下(工具默認提供),pgbench 測試基于TPC-B場景,每個事務包括5個SELECT、UPDATE 和INSERT命令??梢酝ㄟ^編寫自己的事務腳本文件按需進行定制化測試。
通過-i參數,初始化基表和數據.創建測試基表pgbench_accounts/pgbench_branches/pgbench_history/pgbench_tellers
[atlasdb@localhost tmp]$ createdb pgbench [atlasdb@localhost tmp]$ pgbench -i -U atlasdb -p 5432 -d pgbench 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... 100000 of 100000 tuples (100%) done (elapsed 0.45 s, remaining 0.00 s) vacuuming... creating primary keys... done.
通過pgbench --help可以查看完整的參數列表.
其中:
-c, --client=NUM
數據庫客戶端數量,可以理解為數據庫會話數量(postgres進程數),默認為1
-C, --connect
每個事務創建一個連接,由于PG使用進程模型,可以測試頻繁Kill/Create進程的性能表現
-j, --jobs=NUM
pgbench的工作線程數
-T, --time=NUM
以秒為單位的壓測時長
-v, --vacuum-all
每次測試前執行vacuum命令,避免"垃圾"空間的影響
-M, --protocol=simple|extended|prepared
提交查詢命令到服務器使用的協議,simple是默認選項,prepared是類似綁定
-r, --report-latencies
報告每條命令(SQL語句)的平均延時
-S, --select-only
只執行查詢語句
[atlasdb@localhost tmp]$ pgbench --help pgbench is a benchmarking tool for PostgreSQL. Usage: pgbench [OPTION]... [DBNAME] Initialization options: -i, --initialize invokes initialization mode -I, --init-steps=[dtgvpf]+ (default "dtgvp") ...
執行基準測試
執行以下命令,啟動4個工作線程,創建8個客戶端連接PG:
pgbench -h 192.168.26.103 -U atlasdb -p 5432 -d pgbench -r -M prepared -v -T 120 -c 8 -j 4 --aggregate-interval=5 -l
以下為執行過程中的部分日志輸出:
client 5 receiving client 2 receiving client 2 sending INSERT INTO pgbench_history (tid, bid, aid, delta, mtime) VALUES (2, 1, 39145, -3294, CURRENT_TIMESTAMP); client 2 receiving client 4 receiving client 4 executing script "<builtin: TPC-B (sort of)>" client 4 executing \set aid client 4 executing \set bid client 4 executing \set tid client 4 executing \set delta client 4 sending BEGIN; client 4 receiving client 2 receiving client 2 sending END; client 2 receiving client 4 receiving client 4 sending UPDATE pgbench_accounts SET abalance = abalance + 4608 WHERE aid = 80792; client 4 receiving client 4 receiving client 4 sending SELECT abalance FROM pgbench_accounts WHERE aid = 80792; client 4 receiving client 2 receiving
下面是結果輸出:
transaction type: <builtin: TPC-B (sort of)> scaling factor: 1 query mode: prepared number of clients: 8 number of threads: 4 duration: 120 s number of transactions actually processed: 27809 latency average = 34.537 ms tps = 231.635472 (including connections establishing) tps = 231.685954 (excluding connections establishing) statement latencies in milliseconds: 0.035 \set aid random(1, 100000 * :scale) 0.008 \set bid random(1, 1 * :scale) 0.007 \set tid random(1, 10 * :scale) 0.006 \set delta random(-5000, 5000) 1.588 BEGIN; 1.987 UPDATE pgbench_accounts SET abalance = abalance + :delta WHERE aid = :aid; 1.829 SELECT abalance FROM pgbench_accounts WHERE aid = :aid; 9.305 UPDATE pgbench_tellers SET tbalance = tbalance + :delta WHERE tid = :tid; 15.904 UPDATE pgbench_branches SET bbalance = bbalance + :delta WHERE bid = :bid; 1.741 INSERT INTO pgbench_history (tid, bid, aid, delta, mtime) VALUES (:tid, :bid, :aid, :delta, CURRENT_TIMESTAMP); 2.101 END;
執行查詢基準測試
pgbench -h 192.168.26.103 -U atlasdb -p 5432 -d pgbench -S -r -M prepared -v -T 120 -c 8 -j 4 --aggregate-interval=5 -l
最終結果如下:
transaction type: <builtin: select only> scaling factor: 1 query mode: prepared number of clients: 8 number of threads: 4 duration: 120 s number of transactions actually processed: 418458 latency average = 2.294 ms tps = 3486.766688 (including connections establishing) tps = 3487.047954 (excluding connections establishing) statement latencies in milliseconds: 0.018 \set aid random(1, 100000 * :scale) 2.266 SELECT abalance FROM pgbench_accounts WHERE aid = :aid;
到此,相信大家對“PostgreSQL中pgbench有什么作用”有了更深的了解,不妨來實際操作一番吧!這里是億速云網站,更多相關內容可以進入相關頻道進行查詢,關注我們,繼續學習!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。