Mar
23
2010

mod_cache

21:32 pm

mod_cacheを利用したApacheのパフォーマンスチューニングです。
キャッシュにより、サイトの高速化を図ります。

Webサーバをチューニングしていると確実にやらなければならないのがパフォーマンスチューニングです。当然,同じWebサーバといえど用途はさまざまですね。たとえば,静的なコンテンツのみを配信しているような場合ですと,プロセス数やスレッド数のチューニングになることでしょう。
しかしその他,動的にコンテンツを返しているような場合もあると思います。そのようなときは,キャッシュをさせることで大幅なパフォーマンス向上をはかることができるのです。

mod_cacheはキャッシュを保存するストレージを選択することができます。メモリに保存する方法とDiskに保存する方法です。

/etc/apache2/mods-availableには、利用可能なモジュールが格納されてますが、そのなかから以下の5ファイルを有効化しなければなりません。
cache.load
mem_cache.conf
mem_cache.load
disk_cache.conf
disk_cache.load

具体的には、以下のコマンドを実行します。
$ sudo a2enmod cache
$ sudo a2enmod mem_cache
$ sudo a2enmod disk_cache

するとsudo a2enmod mem_cacheにリンクが張られ有効になります。
$ cd sudo a2enmod mem_cache
$ ls
で確認できます。

あとは、設定ファイルであるmem_cache.conf及びdisk_cache.confを適宜編集すれば良いということになります。

mem_cache.conf
$ sudo nano /etc/apache2/mods-enabled/mem_cache.conf でmem_cache.confを編集します。
デフォルトでは、以下のようになっています。

<IfModule mod_mem_cache.c>
        CacheEnable mem /
        MCacheSize 4096
        MCacheMaxObjectCount 100
        MCacheMinObjectSize 1
        MCacheMaxObjectSize 2048
</IfModule>

以下のようにカスタマイズしてみました。

<IfModule mod_mem_cache.c>
        CacheEnable mem /
        MCacheSize 4096
        MCacheMaxObjectCount 100
        MCacheMinObjectSize 1
        MCacheMaxObjectSize 2048
        CacheDefaultExpire 7200
        CacheLastModifiedFactor 0.2
</IfModule>

disk_cache.conf
$ sudo nano /etc/apache2/mods-enabled/disk_cache.conf でdisk_cache.confを編集します。
デフォルトでは、以下のようになっています。

<IfModule mod_disk_cache.c>
# cache cleaning is done by htcacheclean, which can be configured in
# /etc/default/apache2
#
# For further information, see the comments in that file,
# /usr/share/doc/apache2.2-common/README.Debian, and the htcacheclean(8)
# man page.
        # This path must be the same as the one in /etc/default/apache2
        CacheRoot /var/cache/apache2/mod_disk_cache
        # This will also cache local documents. It usually makes more sense to
        # put this into the configuration for just one virtual host.
        #CacheEnable disk /
        CacheDirLevels 5
        CacheDirLength 3
</IfModule>

以下のようにカスタマイズしてみました。

<IfModule mod_disk_cache.c>
# cache cleaning is done by htcacheclean, which can be configured in
# /etc/default/apache2
#
# For further information, see the comments in that file,
# /usr/share/doc/apache2.2-common/README.Debian, and the htcacheclean(8)
# man page.
        # This path must be the same as the one in /etc/default/apache2
        CacheRoot /var/cache/apache2/mod_disk_cache
        # This will also cache local documents. It usually makes more sense to
        # put this into the configuration for just one virtual host.
        CacheEnable disk /
        CacheMaxFileSize 50000
        CacheDirLevels 5
        CacheDirLength 4
        CacheDefaultExpire 3600
        CacheIgnoreCacheControl On
        CacheIgnoreNoLastMod On
</IfModule>

$ sudo ab -n 100 -c 10 http://192.168.0.4/
-nで同時リクエスト数、-cで同時接続数。↑の場合は同時10接続で100リクエスト。

http://以降にサーバーIPとかサーバー名。

$ sudo ab -n 100 -c 10 http://192.168.0.4/
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 192.168.0.4 (be patient).....done
Server Software:        Apache
Server Hostname:        192.168.0.4
Server Port:            80
Document Path:          /
Document Length:        1262 bytes
Concurrency Level:      10
Time taken for tests:   1.063 seconds
Complete requests:      100
Failed requests:        0
Write errors:           0
Total transferred:      154000 bytes
HTML transferred:       126200 bytes
Requests per second:    94.08 [#/sec] (mean)
Time per request:       106.287 [ms] (mean)
Time per request:       10.629 [ms] (mean, across all concurrent requests)
Transfer rate:          141.49 [Kbytes/sec] received
Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    0   0.7      0       3
Processing:    10  103  44.6    110     193
Waiting:        9   96  44.3    101     193
Total:         10  103  44.8    110     195
Percentage of the requests served within a certain time (ms)
  50%    110
  66%    121
  75%    133
  80%    145
  90%    168
  95%    175
  98%    191
  99%    195
 100%    195 (longest request)

hubuntu.netのApacheBench

$ sudo ab -n 100 -c 10 http://192.168.0.4/home/www/hubuntu.net/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 192.168.0.4 (be patient).....done
Server Software:        Apache
Server Hostname:        192.168.0.4
Server Port:            80
Document Path:          /home/www/hubuntu.net/wordpress/
Document Length:        229 bytes
Concurrency Level:      10
Time taken for tests:   0.721 seconds
Complete requests:      100
Failed requests:        0
Write errors:           0
Non-2xx responses:      100
Total transferred:      41600 bytes
HTML transferred:       22900 bytes
Requests per second:    138.63 [#/sec] (mean)
Time per request:       72.136 [ms] (mean)
Time per request:       7.214 [ms] (mean, across all concurrent requests)
Transfer rate:          56.32 [Kbytes/sec] received
Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    2   4.7      0      17
Processing:     2   69  88.3     18     283
Waiting:        2   26  41.6     14     220
Total:          2   71  91.7     18     292
Percentage of the requests served within a certain time (ms)
  50%     18
  66%     44
  75%    140
  80%    159
  90%    252
  95%    281
  98%    286
  99%    292
 100%    292 (longest request)

 

apache2を再起動します。
$ sudo /etc/init.d/apache2 restart

hbirds@UbuntuServer:/home/www$ sudo /etc/init.d/apache2 restart
 * Restarting web server htcacheclean                                                                          ...not running
                                                                                                        [ OK ]

htcacheclean を入れろということかな。
このサイトを参考に対応しました。

To make sure that our cache directory /var/cache/apache2/mod_disk_cache doesn’t fill up over time, we have to clean it with the htcacheclean command. That command is part of the apache2-utils package which we install as follows:

$ sudo aptitude install apache2-utils
$ htcacheclean -d30 -n -t -p /var/cache/apache2/mod_disk_cache -l 100M -i

This will clean our cache directory every 30 minutes and make sure that it will not get bigger than 100MB. To learn more about htcacheclean, take a look at

$ sudo nano /etc/rc.local として

#!/bin/sh -e
#
# rc.local
#
# This script is executed at the end of each multiuser runlevel.
# Make sure that the script will "exit 0" on success or any other
# value on error.
#
# In order to enable or disable this script just change the execution
# bits.
#
# By default this script does nothing.
/usr/sbin/htcacheclean -d60 -n -t -p /var/cache/apache2/mod_disk_cache -l 500M -i
exit 0

hubuntu.netに多少アクセスしてから、再度ApacheBench

$ sudo ab -n 100 -c 10 http://192.168.0.4/home/www/hubuntu.net/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 192.168.0.4 (be patient).....done
Server Software:        Apache
Server Hostname:        192.168.0.4
Server Port:            80
Document Path:          /home/www/hubuntu.net/wordpress/
Document Length:        229 bytes
Concurrency Level:      10
Time taken for tests:   0.273 seconds
Complete requests:      100
Failed requests:        0
Write errors:           0
Non-2xx responses:      100
Total transferred:      41600 bytes
HTML transferred:       22900 bytes
Requests per second:    366.32 [#/sec] (mean)
Time per request:       27.299 [ms] (mean)
Time per request:       2.730 [ms] (mean, across all concurrent requests)
Transfer rate:          148.82 [Kbytes/sec] received
Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    0   0.7      0       3
Processing:    10   26   6.3     26      66
Waiting:        7   24   3.3     25      30
Total:         13   26   6.4     26      69
Percentage of the requests served within a certain time (ms)
  50%     26
  66%     26
  75%     26
  80%     26
  90%     28
  95%     40
  98%     48
  99%     69
 100%     69 (longest request)

No Comments »

RSS feed for comments on this post. TrackBack URL

Leave a comment

You must be logged in to post a comment.

www.hbirds.net