2018年3月30日金曜日

■pgbenchの使い方をメモっとく

インストールパッケージ:
postgresql92-9.2.1-1PGDG.rhel5.i386.rpm
postgresql92-libs-9.2.1-1PGDG.rhel5.i386.rpm
postgresql92-server-9.2.1-1PGDG.rhel5.i386.rpm

必要パッケージ:postgresql92-contrib-9.2.1-1PGDG.rhel5.i386.rpm
           uuid-1.5.1-3.el5.i386.rpm(上記の依存パッケージ)

①必要パッケージ2つをインストール
# rpm -ivh postgresql92-contrib-9.2.1-1PGDG.rhel5.i386.rpm
# rpm -ivh uuid-1.5.1-3.el5.i386.rpm

②ユーザ、データベース作成
$ createuser -d -r -l -P testuser
 d…新しいユーザに対してデータベースの作成を許可
 r…新しいユーザに対して新しいロールの作成を許可
 P…createuserは新しいユーザのパスワードのプロンプトを表示
$ Enter password for new role:xxxxxxx

$ createdb -O testuser testdb2
 OはOWNER

③ベンチマーク用のテーブルを作成(初期化)
★測定するときは都度、初期化をする
$ pgbench -U testuser -i testdb2
= $ pgbench -U testuser -i -s 1 testdb2
    ※スケールファクタ1
 ⇒以下のようなテーブルが追加される
  public | pgbench_accounts | table | testuser
  public | pgbench_branches | table | testuser
  public | pgbench_history  | table | testuser
  public | pgbench_tellers  | table | testuser

[初期状態]
testdb2=> select count(*) from pgbench_history;
 count
-------
     0
(1 row)

testdb2=> select count(*) from pgbench_accounts;
 count
--------
 100000
(1 row)

testdb2=> select count(*) from pgbench_branches;
 count
-------
     1
(1 row)

testdb2=> select count(*) from pgbench_tellers;
 count
-------
    10
(1 row)


testdb2=> select * from pgbench_branches;
 bid | bbalance | filler
-----+----------+--------
   1 |        0 |
(1 row)

testdb2=> select * from pgbench_tellers;
 tid | bid | tbalance | filler
-----+-----+----------+--------
   1 |   1 |        0 |
   2 |   1 |        0 |
   3 |   1 |        0 |
   4 |   1 |        0 |
   5 |   1 |        0 |
   6 |   1 |        0 |
   7 |   1 |        0 |
   8 |   1 |        0 |
   9 |   1 |        0 |
  10 |   1 |        0 |
(10 rows)

testdb2=> select * from pgbench_accounts;
  aid   | bid | abalance |                                        filler

--------+-----+----------+------------------------------------------------------
--------------------------------
      1 |   1 |        0 |

      2 |   1 |        0 |

      3 |   1 |        0 |

      4 |   1 |        0 |

      5 |   1 |        0 |

      6 |   1 |        0 |

      7 |   1 |        0 |

      8 |   1 |        0 |

      9 |   1 |        0 |

     10 |   1 |        0 |

④ベンチマーク実行
$ ./pgbench -c 10 -t 1000 testdb2
 starting vacuum...end.
 transaction type: TPC-B (sort of)
 scaling factor: 1
 query mode: simple
 number of clients: 10
 number of threads: 1
 number of transactions per client: 1000
 number of transactions actually processed: 10000/10000
 tps = 280.879365 (including connections establishing)
 tps = 281.992691 (excluding connections establishing)
 ⇒-c 10は同時に10人のユーザが利用(接続)できることを想定
 ⇒-t 1000は各接続あたり、1000回のトランザクションを実行

[実行後]
testdb2=> select count(*) from pgbench_history;
 count
-------
 10000
(1 row)

testdb2=> select count(*) from pgbench_accounts;
 count
--------
 100000
(1 row)

testdb2=> select count(*) from pgbench_branches;
 count
-------
     1
(1 row)

testdb2=> select count(*) from pgbench_tellers;
 count
-------
    10
(1 row)


testdb2=> select * from pgbench_branches;
 bid | bbalance | filler
-----+----------+--------
   1 |   219104 |
(1 row)

testdb2=> select * from pgbench_tellers;
 tid | bid | tbalance | filler
-----+-----+----------+--------
   5 |   1 |    74665 |
   3 |   1 |    76425 |
   8 |   1 |   -64329 |
   6 |   1 |    43609 |
   2 |   1 |   122252 |
   7 |   1 |    19944 |
   9 |   1 |    99234 |
  10 |   1 |   -60867 |
   4 |   1 |   -90336 |
   1 |   1 |    -1493 |
(10 rows)

testdb2=> select * from pgbench_accounts;
  aid   | bid | abalance |                                        filler

--------+-----+----------+------------------------------------------------------
--------------------------------
      1 |   1 |        0 |

      2 |   1 |        0 |

      3 |   1 |        0 |

      4 |   1 |        0 |

      5 |   1 |        0 |

      6 |   1 |        0 |

      7 |   1 |        0 |

      8 |   1 |        0 |

      9 |   1 |        0 |

     10 |   1 |        0 |
   …
    125 |   1 |    -2035 |

    177 |   1 |    -2745 |

    165 |   1 |    -2258 |

    148 |   1 |     2020 |

    179 |   1 |    -1359 |

    136 |   1 |     1237 |

    146 |   1 |     4247 |

    180 |   1 |      711 |

⑤出力内容
 scaling factor: 1とは
 ⇒10万件のデータを利用してベンチマーク実行

 query mode: simple
 ⇒psqlによる単純な問い合わせを実施

 number of clients: 10
 ⇒同時接続ユーザ10

 number of transactions per client: 1000
 ⇒1クライアントあたり1000回トランザクションを実施
 ※-Tを利用すると指定した秒数の間、トランザクションを実施

 number of transactions actually processed: 10000/10000
 ⇒正常に実行されたトランザクションの割合。
  10*1000=10000回正常に実行できてるということ。

 tps = 280.879365 (including connections establishing)
 tps = 281.992691 (excluding connections establishing)
 ⇒1秒間に実行できたトランザクションの数を表示。
  数値が大きいほど性能がよいということ。
  前者はDBに接続する時間を含んでいる。

⑥通常のpg_benchの流れ
 1.pgbench_accountsを1件更新
 2.pgbench_accountsから1件検索
 3.pgbench_tellersを1件更新
 4.pgbench_branchesを1件更新
 5.pgbench_historyに1件行を追加
 ⇒3,4を省略するには-Nをつける。
  検索処理のみ測定するには-Sをつける。

デフォルトのトランザクションスクリプトは、1トランザクションで以下の7コマンドを発行します。
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;

-Nを指定した場合、第4コマンドと第5コマンドはトランザクションに含まれません。
-Sを指定した場合、SELECTのみが発行されます。
:deltaは1から1000までの値を取る乱数,:aid は 1から100000までの値を取る乱数
:tid は 1から10の間の値をとる乱数
:bid は 1 から[スケリングファクター]の間の値を取る乱数

⑦その他
 任意のSQLをトランザクションとして実行できる独自スクリプト機能
 ⇒以下のようなSQL文を用意
 BEGIN;
 SELECT 文;
 SELECT 文;
 END;
 上記をtestuser.pgbenchのような名前のファイルで保存して実行。
  $ /usr/pgsql-9.2/bin/pgbench testdb2 -f testuser.pgbench
 starting vacuum...end.
 transaction type: Custom query
 scaling factor: 1
 query mode: simple
 number of clients: 1
 number of threads: 1
 number of transactions per client: 10
 number of transactions actually processed: 10/10
 tps = 175.152821 (including connections establishing)
 tps = 282.613611 (excluding connections establishing)

以上

■証明書の流れ

証明書の流れ