php中使用memcache扩展的性能问题 [文章作者:孙立 链接:http://www.cnblogs.com/sunli/更新时间:2010-08-28] Memcached是一个高性能分布式缓存系统,目前估计是很多公司的标配了,因为他被非常广泛的使用,在各种语言下都有其丰富的client,所以很多后端服
php中使用memcache扩展的性能问题
[文章作者:孙立 链接:http://www.cnblogs.com/sunli/ 更新时间:2010-08-28]
Memcached是一个高性能分布式缓存系统,目前估计是很多公司的标配了,因为他被非常广泛的使用,在各种语言下都有其丰富的client,所以很多后端服务也采用了兼容的memcached协议,比如memcachedb, ttserver等等。所以最近我开发的队列服务器也采用了memcached协议,底层是基于磁盘的存储,单独测试底层存储的性能非常好,在我台式机能达到每秒写入3万1K的数据。
今天用php测试了下往刚刚开发的队列服务器(icqueue)插入数据,代码如下:
<p><span></span><span>php<br></span><span>$mem</span><span>=</span><span>new</span><span> Memcache();<br></span><span>$mem</span><span>-></span><span>addServer (</span><span>"</span><span>127.0.0.1</span><span>"</span><span>,</span><span>12001</span><span>,</span><span>false</span><span>,</span><span>1</span><span>,</span><span>100</span><span>);<br></span><span>$start</span><span>=</span><span>microtime</span><span>(</span><span>true</span><span>);<br></span><span>$str</span><span>=</span><span>str_repeat</span><span> (</span><span>"</span><span>a</span><span>"</span><span>,</span><span>1024</span><span>);</span><span>//</span><span>1k数据</span><span><br></span><span>for</span><span>(</span><span>$i</span><span>=</span><span>0</span><span>;</span><span>$i</span><span><span>100000</span><span>;</span><span>$i</span><span>++</span><span>){<br> </span><span>$mem</span><span>-></span><span>add(</span><span>"</span><span>bbs_password_</span><span>"</span><span>,</span><span>$str</span><span>,</span><span>0</span><span>,</span><span>0</span><span>);</span><span>//</span><span>入队,bbs为队列名称,password为密码</span><span><br></span><span>}<br></span><span>echo</span><span>microtime</span><span>(</span><span>true</span><span>)</span><span>-</span><span>$start</span><span>;<br> </span><span>?></span></span></p>
执行:[root@sunlitest1 bbs]# /usr/local/php/bin/php mem.php
5.6957609653473
花了5秒,感觉速度还是挺快,还是单进程跑的。接着我想测试大一点的数据,把其中的
<p><span>$str</span><span>=</span><span>str_repeat</span><span> (</span><span>"</span><span>a</span><span>"</span><span>,</span><span>1024</span><span>);</span><span>//</span><span>1k数据</span></p>
改为
<p><span>$str</span><span>=</span><span>str_repeat</span><span> (</span><span>"</span><span>a</span><span>"</span><span>,</span><span>1024</span><span>*</span><span>10</span><span>);</span><span>//</span><span>10k数据</span></p>
执行:[root@sunlitest1 bbs]# /usr/local/php/bin/php mem.php发现速度非常的慢,等了1分钟也没有成功。怀疑是队列服务(icqueue)有问题,又换成memcached服务器写数据测试,发现速度同样的慢。
这就排除了是服务端的问题。经过调整数据的大小,发现只要达到8K就非常慢。
然后怀疑是不是到了8K memcach自动进行了压缩,是压缩很慢呢?又去telnet进去,发现数据是没有压缩的。
剩下的问题应该就是网络问题了,翻看了memcach扩展的手册,发现其中memcache.chunk_size的大小默认是8K,灵感一现,希望就是这这个问题导致的。
然后又改代码:
<p><span></span><span>php<br></span><span>ini_set</span><span>(</span><span>'</span><span>memcache.chunk_size</span><span>'</span><span>,</span><span>1024</span><span>*</span><span>15</span><span>);</span><span>//</span><span>设置到15K</span><span><br></span><span>$mem</span><span>=</span><span>new</span><span> Memcache();<br></span><span>$mem</span><span>-></span><span>addServer (</span><span>"</span><span>127.0.0.1</span><span>"</span><span>,</span><span>12001</span><span>,</span><span>false</span><span>,</span><span>1</span><span>,</span><span>100</span><span>);<br></span><span>$start</span><span>=</span><span>microtime</span><span>(</span><span>true</span><span>);<br></span><span>$str</span><span>=</span><span>str_repeat</span><span> (</span><span>"</span><span>a</span><span>"</span><span>,</span><span>1024</span><span>*</span><span>10</span><span>);</span><span>//</span><span>10k数据</span><span><br></span><span>for</span><span>(</span><span>$i</span><span>=</span><span>0</span><span>;</span><span>$i</span><span><span>100000</span><span>;</span><span>$i</span><span>++</span><span>){<br> </span><span>$mem</span><span>-></span><span>add(</span><span>"</span><span>bbs_password_</span><span>"</span><span>,</span><span>$str</span><span>,</span><span>0</span><span>,</span><span>0</span><span>);</span><span>//</span><span>入队,bbs为队列名称,password为密码</span><span><br></span><span>}<br></span><span>echo</span><span>microtime</span><span>(</span><span>true</span><span>)</span><span>-</span><span>$start</span><span>;<br> </span><span>?></span></span></p>
执行:[root@sunlitest1 bbs]# /usr/local/php/bin/php mem.php
10.800881147385
跑了10完成,OK,问题解决了。
memcache.chunk_size在php.ini设置的。大小推荐设置为你的数据的常规大小。