PHPでAPCとMemcacheのローカルでのキャッシュを比べてみる
VirtualBoxにCentOSをとりあえずインストール
の続き
PHPでAPCとMemcacheのローカルでのキャッシュを比べてみる。
インストール
ApacheとPHPとAPCとMemcacheをインストールする。
$ sudo yum -y install httpd php memcached php-pecl-apc php-pecl-memcache
$ sudo vi /etc/php.ini date.timezone = "Asia/Tokyo"
$ sudo service httpd start
$ sudo chkconfig httpd on
$ sudo service memcached start
$ sudo chkconfig memcached on
$ sudo chown hoge:hoge /var/www/html/
インストールされているのが確認できる。
$ php -i | grep apc Additional .ini files parsed => /etc/php.d/apc.ini, apc MMAP File Mask => /tmp/apc.XXXXXX apc.cache_by_default => On => On apc.canonicalize => Off => Off apc.coredump_unmap => Off => Off apc.enable_cli => Off => Off apc.enabled => On => On apc.file_md5 => Off => Off apc.file_update_protection => 2 => 2 apc.filters => no value => no value apc.gc_ttl => 3600 => 3600 apc.include_once_override => Off => Off apc.lazy_classes => Off => Off apc.lazy_functions => Off => Off apc.max_file_size => 1M => 1M apc.mmap_file_mask => /tmp/apc.XXXXXX => /tmp/apc.XXXXXX apc.num_files_hint => 1024 => 1024 apc.preload_path => no value => no value apc.report_autofilter => Off => Off apc.rfc1867 => Off => Off apc.rfc1867_freq => 0 => 0 apc.rfc1867_name => APC_UPLOAD_PROGRESS => APC_UPLOAD_PROGRESS apc.rfc1867_prefix => upload_ => upload_ apc.rfc1867_ttl => 3600 => 3600 apc.serializer => default => default apc.shm_segments => 1 => 1 apc.shm_size => 64M => 64M apc.slam_defense => On => On apc.stat => On => On apc.stat_ctime => Off => Off apc.ttl => 7200 => 7200 apc.use_request_time => On => On apc.user_entries_hint => 4096 => 4096 apc.user_ttl => 7200 => 7200 apc.write_lock => On => On
$ php -i | grep memcache /etc/php.d/memcache.ini, memcache memcache support => enabled memcache.allow_failover => 1 => 1 memcache.chunk_size => 32768 => 32768 memcache.compress_threshold => 20000 => 20000 memcache.default_port => 11211 => 11211 memcache.hash_function => crc32 => crc32 memcache.hash_strategy => consistent => consistent memcache.lock_timeout => 15 => 15 memcache.max_failover_attempts => 20 => 20 memcache.protocol => ascii => ascii memcache.redundancy => 1 => 1 memcache.session_redundancy => 2 => 2 Registered save handlers => files user memcache
APCの場合
$ vi /var/www/html/apc.php
<?php $time_start = microtime(true); define('CNT', 100000); // write cache $time_write = microtime(true); for ($i = 0; $i < CNT; $i++) { apc_store($i, $i); } // read cache $time_read = microtime(true); for ($i = 0; $i < CNT; $i++) { apc_fetch($i); } $time_end = microtime(true); echo 'read : ' . round($time_read - $time_write, 2) . ' sec<br />'; echo 'write : ' . round($time_end - $time_read, 2) . ' sec<br />'; echo 'total : ' . round($time_end - $time_start, 2) . ' sec<br />';
ブラウザから何回かアクセスしてみる
read : 1.07 sec write : 0.07 sec total : 1.15 sec
read : 0.76 sec write : 0.05 sec total : 0.81 sec
read : 1.01 sec write : 0.07 sec total : 1.08 sec
Memcacheの場合
$$ vi /var/www/html/memcache.php
$<?php $time_start = microtime(true); define('CNT', 100000); $m = new Memcache; $m->connect('localhost'); // write cache $time_write = microtime(true); for ($i = 0; $i < CNT; $i++) { $m->set($i, $i); } // read cache $time_read = microtime(true); for ($i = 0; $i < CNT; $i++) { $m->get($i); } $time_end = microtime(true); echo 'read : ' . round($time_read - $time_write, 2) . ' sec<br />'; echo 'write : ' . round($time_end - $time_read, 2) . ' sec<br />'; echo 'total : ' . round($time_end - $time_start, 2) . ' sec<br />';
ブラウザから何回かアクセスしてみる。
read : 5.46 sec write : 5.69 sec total : 11.15 sec
read : 5.65 sec write : 5.59 sec total : 11.24 sec
read : 5.63 sec write : 5.67 sec total : 11.3 sec
結果
APCのほうが速かった。