eAcceleratorを使ってPHPスクリプトを最適化・高速化する

この記事は公開されてから1年以上経過しています。情報が古い可能性がありますので、ご注意ください。

PHPの処理が重い場合

PHPのWebサイトを見ていると何を処理しているのか重いことがあります。まず始めに疑うのがデータベースへのクエリー処理なのですが、直接クエリーをたたいた結果が良好だった場合はどうしましょうか。

  • データベースへのクエリー結果の取得に時間が掛かっていますか?
  • データベースへ大量の問合せを行っていませんか?
  • データベースへのコネクションはプーリングはされていますか?
  • アプリケーションサーバー内の処理は高速ですか?
  • 別サーバーとの通信で待ち時間が発生していませんか?
  • ディスクI/Oが大量に発生していませんか?
  • クライアントとサーバーとの通信量や回数が多くないですか?
  • コンテンツをキャッシュや圧縮するように指定していますか?

そして、今回試すeAcceleratorは、PHPスクリプトを最適化して事前にコンパイルしてキャッシュしてくれる仕組みです。PHPの処理の遅さは、スクリプトのロードとコンパイルにあるそうです。これを事前に行ってくれるという事で、最大で10倍速も夢ではありません。他にもキャッシュするソフトはあるようですが、ベンチマークの結果や事例を見てeAcceleratorを試してみたいと思います!

eAcceleratorのインストール

まずはApacheとPHPの環境を整えます。

$ sudo yum install php-devel
$ sudo yum install gcc

ソースをダウンロードしてコンパイルします。

$ wget http://bit.ly/ymYXBI
$ unzip eaccelerator-0.9.6.1.zip
$ cd eaccelerator-0.9.6.1

$ phpize
Configuring for:
PHP Api Version:         20090626
Zend Module Api No:      20090626
Zend Extension Api No:   220090626

$ ./configure \
  --enable-eaccelerator=shared \
  --with-php-config=/usr/bin/php-config

$ make
$ make install

起動用の設定ファイルを作る

続いて、起動用の設定ファイルを作ります。

$ cd /etc/php.d/
$ vi eaccelerator.ini

eaccelerator.iniの中身はこんな感じ。ほとんどREADMEをコピーしていますが、eaccelerator.shm_sizeを16から32にして、eaccelerator.compressとeaccelerator.compress_levelを追加しています。また、管理コンソール用に、eaccelerator.allowed_admin_pathを追加しました。

extension="eaccelerator.so"
eaccelerator.shm_size="32"
eaccelerator.cache_dir="/tmp/eaccelerator"
eaccelerator.enable="1"
eaccelerator.optimizer="1"
eaccelerator.check_mtime="1"
eaccelerator.debug="0"
eaccelerator.filter=""
eaccelerator.shm_max="0"
eaccelerator.shm_ttl="0"
eaccelerator.shm_prune_period="0"
eaccelerator.shm_only="0"
eaccelerator.compress="1"
eaccelerator.compress_level="9"
eaccelerator.allowed_admin_path="/var/www/html/"

キャッシュ用のフォルダを作る

キャッシュ用のフォルダを指定して書き込みできるようにします。

$ mkdir /tmp/eaccelerator
$ chmod 0777 /tmp/eaccelerator

とりあえず動作確認をする

Apacheを再起動して動作を確認してみましょう。大丈夫そうですね。

$ sudo service httpd restart

$ php -vPHP 5.3.9 (cli) (built: Jan 23 2012 21:05:00) 
Copyright (c) 1997-2012 The PHP Group
Zend Engine v2.3.0, Copyright (c) 1998-2012 Zend Technologies
    with eAccelerator v0.9.6.1, Copyright (c) 2004-2010 eAccelerator, by eAccelerator

管理コンソールを使う

eAcceleratorはWebベースの管理コンソールがあります。これを使ってみましょう。管理者ののみアクセスできるようにcontrol.phpのユーザ名とパスワードを設定しておいてください。そして、eaccelerator.iniのeaccelerator.allowed_admin_pathで指定したパスにコピーします。

$ cd eaccelerator-0.9.6.1
$ vi control.php
$ cp control.php /var/www/html/

ブラウザから見てみます。とってもイイ感じ。

Apache Benchでパフォーマンスを測定する

動作確認ができましたので、実際にどの程度パフォーマンスが上がっているのか確認してみまよう。今回は、WordPrssを新規で構築してApache Benchからアクセスしてみます。WordPressの構築自体は割愛します。

eAcceleratorがOFFの場合

# ab -n 5 -c 1 http://ec2-XXX-XXX-XXX-XXX.ap-northeast-1.compute.amazonaws.com/wordpress/
This is ApacheBench, Version 2.3 <$Revision: 655654 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/

Benchmarking ec2-XXX-XXX-XXX-XXX.ap-northeast-1.compute.amazonaws.com (be patient).....done

Server Software:        Apache/2.2.21
Server Hostname:        ec2-XXX-XXX-XXX-XXX.ap-northeast-1.compute.amazonaws.com
Server Port:            80

Document Path:          /wordpress/
Document Length:        8915 bytes

Concurrency Level:      1
Time taken for tests:   1.183 seconds
Complete requests:      5
Failed requests:        3
   (Connect: 0, Receive: 0, Length: 3, Exceptions: 0)
Write errors:           0
Total transferred:      45904 bytes
HTML transferred:       44564 bytes
Requests per second:    4.23 [#/sec] (mean)
Time per request:       236.634 [ms] (mean)
Time per request:       236.634 [ms] (mean, across all concurrent requests)
Transfer rate:          37.89 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    0   0.0      0       0
Processing:   235  237   1.0    237     238
Waiting:      202  208   3.2    209     210
Total:        235  237   1.0    237     238

Percentage of the requests served within a certain time (ms)
  50%    236
  66%    237
  75%    237
  80%    238
  90%    238
  95%    238
  98%    238
  99%    238
 100%    238 (longest request)

eAcceleratorがONの場合

$ ab -n 5 -c 1 http://ec2-XXX-XXX-XXX-XXX.ap-northeast-1.compute.amazonaws.com/wordpress/
This is ApacheBench, Version 2.3 <$Revision: 655654 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/

Benchmarking ec2-XXX-XXX-XXX-XXX.ap-northeast-1.compute.amazonaws.com (be patient).....done

Server Software:        Apache/2.2.21
Server Hostname:        ec2-XXX-XXX-XXX-XXX.ap-northeast-1.compute.amazonaws.com
Server Port:            80

Document Path:          /wordpress/
Document Length:        8910 bytes

Concurrency Level:      1
Time taken for tests:   0.623 seconds
Complete requests:      5
Failed requests:        3
   (Connect: 0, Receive: 0, Length: 3, Exceptions: 0)
Write errors:           0
Total transferred:      45899 bytes
HTML transferred:       44559 bytes
Requests per second:    8.03 [#/sec] (mean)
Time per request:       124.569 [ms] (mean)
Time per request:       124.569 [ms] (mean, across all concurrent requests)
Transfer rate:          71.97 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    0   0.0      0       0
Processing:   122  124   1.7    125     126
Waiting:      108  111   1.4    111     112
Total:        122  124   1.7    126     126
WARNING: The median and mean for the total time are not within a normal deviation
        These results are probably not that reliable.

Percentage of the requests served within a certain time (ms)
  50%    125
  66%    126
  75%    126
  80%    126
  90%    126
  95%    126
  98%    126
  99%    126
 100%    126 (longest request)

パフォーマンス測定結果

eAcceleratorの使用によって、2倍のパフォーマンス差となりました。

eAccelerator なし eAccelerator あり
Requests per second 4.23 8.03
Time per request 236.634 124.569
Transfer rate 37.89 71.97

まとめ

最近は、クラウドの登場によって、サーバー台数を増やすとか、サイズを大きくするなど、富豪的にインフラを使えるようになりましたが、アプリケーションのアーキテクチャーや処理の手順を理解する事で、チューニングポイントが分かり、システム全体を最適化できるようになります。今回実験したeAcceleratorに限らず、様々なレイヤーでのチューニングを楽しみましょう。今日から君も、オプティマイズ、プリコンパイル、キャッシュのマスターだ!

参考資料

Welcome to the eAccelerator project.