phpを使用してOracle Database XEとMySQLへのコネクションプーリングの方法・性能を比較しました。MySQL編 その3

前回の検証では、コネクションプーリングの利点を理解する事が出来たのですが、Apache Benchでの検証をするとどうなるかというコメントで終了していました。
今回Apache Benchでの検証をしたいと思います。


phpを使用してOracle Database XEとMySQLへのコネクションプーリングの方法・性能を比較しました。MySQL編 - @camelmasaの開発日記


phpを使用してOracle Database XEとMySQLへのコネクションプーリングの方法・性能を比較しました。MySQL編 その2 - @camelmasaの開発日記

mysql_connect関数でSELECTクエリ100万回の速度実験(for文内でMySQLに毎回接続しない場合)

mysql_connect.php

<?php
  $start = array_sum(explode(" ",microtime()));

  $link = mysql_connect('localhost', 'root', '[パスワード]');
  mysql_select_db('mysql_connect', $link);
  for($i=0;$i<1000000;$i++){
    $result = mysql_query("SELECT 1;");
  }
  mysql_close($link);

  $end = array_sum(explode(" ",microtime()));
  echo "\n".$end - $start;
?>

このスクリプトを下記に修正。
mysql_connect2.php

<?php
  $link = mysql_connect('localhost', 'root', '[パスワード]');
  mysql_select_db('mysql_connect', $link);
  $result = mysql_query("SELECT 1;");
  mysql_close($link);
?>

上記のコネクションプーリングしないプログラムで100万回リクエストのApache Benchを実行

Apache Benchの実行

ab -n 1000000 -c 100 http://localhost/mysql_connect2.php

Apache Benchの実行結果

[root@www time2]# ab -n 1000000 -c 100 http://localhost/mysql_connect2.php
This is ApacheBench, Version 2.0.40-dev <$Revision: 1.146 $> apache-2.0
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Copyright 2006 The Apache Software Foundation, http://www.apache.org/

Benchmarking localhost (be patient)
Completed 100000 requests
Completed 200000 requests
Completed 300000 requests
Completed 400000 requests
Completed 500000 requests
Completed 600000 requests
Completed 700000 requests
Completed 800000 requests
Completed 900000 requests
Finished 1000000 requests


Server Software: Apache/2.2.3
Server Hostname: localhost
Server Port: 80

Document Path: /mysql_connect2.php
Document Length: 1 bytes

Concurrency Level: 100
Time taken for tests: 364.58800 seconds
Complete requests: 1000000
Failed requests: 0
Write errors: 0
Total transferred: 191000000 bytes
HTML transferred: 1000000 bytes
Requests per second: 2746.81 [#/sec] (mean)
Time per request: 36.406 [ms] (mean)
Time per request: 0.364 [ms] (mean, across all concurrent requests)
Transfer rate: 512.34 [Kbytes/sec] received

Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 2 14.0 0 347
Processing: 0 33 140.2 4 7507
Waiting: 0 25 100.9 3 7486
Total: 0 35 143.5 4 7507

Percentage of the requests served within a certain time (ms)
50% 4
66% 9
75% 15
80% 22
90% 99
95% 192
98% 302
99% 394
100% 7507 (longest request)

Time taken for tests: 364.58800 secondsと出力されているので、6分以上かかった事になります。
結構かかりますね。

mysql_pconnect関数でSELECTクエリ100万回の速度実験(for文内でMySQLに毎回接続しない場合)

mysql_pconnect.php

<?php
  $start = array_sum(explode(" ",microtime()));

  $link = mysql_pconnect('localhost', 'root', '[パスワード]');
  mysql_select_db('mysql_connect', $link);
  for($i=0;$i<1000000;$i++){
    $result = mysql_query("SELECT 1;");
  }
  //mysql_close($link);

  $end = array_sum(explode(" ",microtime()));
  echo "\n".$end - $start;
?>

このスクリプトを下記に修正。

mysql_pconnect2.php

<?php
  $link = mysql_pconnect('localhost', 'root', '[パスワード]');
  mysql_select_db('mysql_connect', $link);
  $result = mysql_query("SELECT 1;");
?>

for文部分を外して上記のコネクションプーリングをしたプログラムでApache Benchを実行しました。

上記のコネクションプーリングをするプログラムで100万回リクエストのApache Benchを実行

Apache Benchの実行

ab -n 1000000 -c 100 http://localhost/mysql_pconnect2.php

Apache Benchの実行結果

[root@www time2]# ab -n 1000000 -c 100 http://localhost/mysql_pconnect2.php
This is ApacheBench, Version 2.0.40-dev <$Revision: 1.146 $> apache-2.0
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Copyright 2006 The Apache Software Foundation, http://www.apache.org/

Benchmarking localhost (be patient)
Completed 100000 requests
Completed 200000 requests
Completed 300000 requests
Completed 400000 requests
Completed 500000 requests
Completed 600000 requests
Completed 700000 requests
Completed 800000 requests
Completed 900000 requests
Finished 1000000 requests


Server Software: Apache/2.2.3
Server Hostname: localhost
Server Port: 80

Document Path: /mysql_pconnect2.php
Document Length: 1 bytes

Concurrency Level: 100
Time taken for tests: 195.267289 seconds
Complete requests: 1000000
Failed requests: 0
Write errors: 0
Total transferred: 191000000 bytes
HTML transferred: 1000000 bytes
Requests per second: 5121.19 [#/sec] (mean)
Time per request: 19.527 [ms] (mean)
Time per request: 0.195 [ms] (mean, across all concurrent requests)
Transfer rate: 955.22 [Kbytes/sec] received

Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 0 3.4 0 235
Processing: 0 18 43.3 14 2793
Waiting: 0 14 23.2 13 2480
Total: 0 19 43.6 15 2793

Percentage of the requests served within a certain time (ms)
50% 15
66% 17
75% 19
80% 20
90% 24
95% 30
98% 61
99% 114
100% 2793 (longest request)

Time taken for tests: 195.267289 secondsと出力されているので、3分以上かかった事になります。

まとめ

コネクションプーリングをした方が約2倍早く終了しました。
http接続等で同時接続が沢山ある時はコネクションプーリングはかなり効果的なのがわかりました。
今後MySQLを使用したアプリケーションを作成する時は、コネクションプーリングも考慮しようと思います。

次こそはphp + Oracle Database XEのコネクションプーリングを検証したいと思います!



[PR]Spreeの情報を集めています。

ECを持ちたい方、仕事でECを使いたい方向けのコミュニティサイトです。
このサイトでは世界で最も使用されているECの1つであるSpreeについての情報を提供しています。
http://spreecommerce.jp/