その他
    ホーム 技術発信 DoRuby [mysqlのベンチマーク]MyBenchの設定

    [mysqlのベンチマーク]MyBenchの設定

    この記事はアピリッツの技術ブログ「DoRuby」から移行した記事です。情報が古い可能性がありますのでご注意ください。

    お疲れさまです。佐久間です。

     最近、railsアプリケーションのDBとして使っている、mysqlのチューニングをしようと頑張っています。
     今日、mysqlのベンチマークテストをしようと、MyBenchというテストツールの設定をしてみました。

    MyBench自体はperlだったりします。rubyとは書き方や実行が違っていて、とりあえずインストールしてみてもよくわからない部分が結構あったりしました。

    実行環境:ubuntu 

    1. MyBenchをインストールしてみる


      # wget http://jeremy.zawodny.com/mysql/mybench/mybench-1.0.tar.gz
      # tar -vxf mybench-1.0.tar.gz
      mybench-1.0/
      mybench-1.0/MyBench.pm
      mybench-1.0/Makefile.PL
      mybench-1.0/META.yml
      mybench-1.0/bench_example
    2. Makefileを作成する

      # cd mybench-1.0
      # ls
      META.yml  Makefile.PL  MyBench.pm  bench_example
      # perl Makefile.PL
      Writing Makefile for mybench
      # ls
      META.yml  Makefile  Makefile.PL  MyBench.pm  bench_example


      Makefileができました。
    3. とりあえず、bench_exampleを実行してみる

      # bench_example    
      forking: ++++++++++
      sleeping for 2 seconds while kids get ready
      waiting: DBD::mysql::st execute failed: called with 1 bind variables when 0 are needed at /usr/local/bin/bench_example line 42.
      DBD::mysql::st execute failed: called with 1 bind variables when 0 are needed at /usr/local/bin/bench_example line 42.
      (省略)
      Use of uninitialized value in numeric gt (>) at /usr/local/share/perl/5.8.8/MyBench.pm line 134.
      Illegal division by zero at /usr/local/share/perl/5.8.8/MyBench.pm line 115.


      エラーが出ました・・・。

      というか、実行方法がすでに違います。
      実は、perlをさわるのは初めてで、よくわかっていません。

      あらためて、
    4. perl bench_exampleとして実行してみます

      # perl bench_example
      forking: ++++++++++
      sleeping for 2 seconds while kids get ready
      waiting: ———-
      Use of uninitialized value in scalar chomp at /usr/local/share/perl/5.8.8/MyBench.pm line 98.
      Use of uninitialized value in split at /usr/local/share/perl/5.8.8/MyBench.pm line 99.
      Use of uninitialized value in addition (+) at /usr/local/share/perl/5.8.8/MyBench.pm line 101.
      (省略)
      Use of uninitialized value in numeric gt (>) at /usr/local/share/perl/5.8.8/MyBench.pm line 134.
      Use of uninitialized value in numeric gt (>) at /usr/local/share/perl/5.8.8/MyBench.pm line 134.
      Illegal division by zero at /usr/local/share/perl/5.8.8/MyBench.pm line 115.


      ・・・bench_exampleの設定何一つ替えていないので、エラーが出るのも当たり前ですよ。

      ということで、
    5. bench_exampleの編集

      # cp bench_example bench_example.org
      # vi bench_example


      bench_exampleファイル

      #!/usr/local/bin/perl -w

      use strict;
      use MyBench;
      use Getopt::Std;
      use Time::HiRes qw(gettimeofday tv_interval);
      use DBI;

      my %opt;
      Getopt::Std::getopt(‘n:r:h:’, \%opt);

      my $num_kids  = $opt{n} || 10;
      my $num_runs  = $opt{r} || 100;
      my $db        = “test”;
      my $user      = “test”;
      my $pass      = “”;
      my $port      = 3306;
      my $host      = $opt{h} || “192.168.0.1”;
      my $dsn       = “DBI:mysql:$db:$host;port=$port”;

      my $callback = sub
      {
          my $id  = shift;
          my $dbh = DBI->connect($dsn, $user, $pass, { RaiseError => 1 });
          my $sth = $dbh->prepare(“SELECT * FROM mytable WHERE ID = ?”);

          my $cnt = 0;
          my @times = ();

      (以下略)


      このファイルを以下のように編集

      #!/usr/local/bin/perl -w

      use strict;
      use MyBench;
      use Getopt::Std;
      use Time::HiRes qw(gettimeofday tv_interval);
      use DBI;

      my %opt;
      Getopt::Std::getopt(‘n:r:h:’, \%opt);

      my $num_kids  = $opt{n} || 10;
      my $num_runs  = $opt{r} || 100;
      my $db        = “xxx_dev”; <=ここ(ベンチマークテストしたいDB名)
      my $user      = “root”; <=ここ (上に記載したDBのユーザー)
      my $pass      = “”;
      my $port      = 3306;
      my $host      = $opt{h} || “localhost”; <=ここ(上に記載したDBがあるホスト)
      my $dsn       = “DBI:mysql:$db:$host;port=$port”;

      my $callback = sub
      {
          my $id  = shift;
          my $dbh = DBI->connect($dsn, $user, $pass, { RaiseError => 1 });
          my $sth = $dbh->prepare(“SELECT * FROM users WHERE ID = ?”); <=ここ(ベンチマークテストしたいSQL文。)

          my $cnt = 0;
          my @times = ();

      (以下略)

      ちなみに、
      36行目あたりにある

      my $v = int(rand(100_000));


      の$vが

      my $sth = $dbh->prepare(“SELECT * FROM users WHERE ID = ?”);


      の?に入ります。
    6. いざ実行!

      # perl bench_example
      forking: ++++++++++
      sleeping for 2 seconds while kids get ready
      waiting: ———-
      test: 1000 0.000173 0.367284 0.001990417 1.990417 5024.07284503699
        clients : 10
        queries : 1000
        fastest : 0.000173
        slowest : 0.367284
        average : 0.001990417
        serial  : 1.990417
        q/sec   : 5024.07284503699


      できた!!やりました!!


      注意:今回bench_exampleで編集した部分を一つでも間違えると、以下のエラーが発生します。やっほー。


      forking: ++++++++++
      sleeping for 2 seconds while kids get ready
      waiting: ———-
      Use of uninitialized value in scalar chomp at /usr/local/share/perl/5.8.8/MyBench.pm line 98.
      Use of uninitialized value in split at /usr/local/share/perl/5.8.8/MyBench.pm line 99.
      Use of uninitialized value in addition (+) at /usr/local/share/perl/5.8.8/MyBench.pm line 101.
      (省略)
      Use of uninitialized value in numeric gt (>) at /usr/local/share/perl/5.8.8/MyBench.pm line 134.
      Use of uninitialized value in numeric gt (>) at /usr/local/share/perl/5.8.8/MyBench.pm line 134.
      Illegal division by zero at /usr/local/share/perl/5.8.8/MyBench.pm line 115.



      さらに
      今回、私の環境ではひっかかりませんでしたが、以下のperlモジュールが入っていないと、動かないみたいです。
      ・DBI
      ・DBD::mysql
      ・Time::HiRes

      おまけ
      macでのインストール方法。
      以下のコマンドを叩き込んでください。(perl入っていること前提です。)

      sudo port install p5-test-harness p5-test-simple p5-dbd-mysql p5-dbi p5-time-hires


      参考サイト: http://d.hatena.ne.jp/sukesam/20041015/1097812531