| Subcribe via RSS

PHP与Perl操作Memcached速度差异比较

9月 26th, 2008 | 4 Comments | Posted in PHP, Perl, memcached < by Johnny Woo >

由于最近在进行memcached方面的工作
在性能测试中
使用了php以及perl对memcached进行操作
结果发现php与perl对memcached操作的性能
差异大约在40~50%之间
以下是测试脚本
所作的操作一样.使用1k的数据重复512000次.
总共插入memcached 500M的数据

php操作脚本

<?php
ini_set("memcache.hash_function","crc32");
$memcache = new Memcache;
$memcache->addServer('localhost', 30001);
$memcache->flush();
for($i=0;$i<512000;$i++){
        
$memcache->set($i,
"共1k的填充数据",0,1000);
}
?>

接着是perl脚本

#!/usr/bin/perl
use Cache::Memcached();
$memcache=new Cache::Memcached{'servers'=>["localhost:30001"]};
$memcache->flush_all();
for($i=0;$i<512000;$i++){
    
$memcache->set($i,
"共1k的填充数据");
}
$memcache->disconnect_all();

从代码行数上来看.两者也几近一致
但是测试结果却是大相径庭
我们在linux下使用time对执行进行计时
3次执行结果如下

[root@lenovo5 ~]# time ./test1k.pl
real    1m2.265s
user    0m36.427s
sys     0m17.114s
[root@lenovo5 ~]# time ./test1k.pl
real    1m2.814s
user    0m36.380s
sys     0m17.463s
[root@lenovo5 ~]# time ./test1k.pl
real    1m13.684s
user    0m44.603s
sys     0m18.366s
[root@lenovo5 ~]# time php ./test1k.php
real    0m38.055s
user    0m11.768s
sys     0m13.891s
[root@lenovo5 ~]# time php ./test1k.php
real    0m38.892s
user    0m12.416s
sys     0m14.044s
[root@lenovo5 ~]# time php ./test1k.php
real    0m38.955s
user    0m12.430s
sys     0m13.088s

差异很明显.perl执行需要1分左右而php只需要40秒不到
就是php的执行比perl的大约快40%
分析之后有几个因素的可能
1.perl的字串处理速度较慢.我们看到perl版本的set中不需要加入长度参数.这样每次插入可能都会需要set函数去判断传入的字串长度.这样可能较慢.但是随后我们发现php的set虽然有长度参数.但是这个参数并非是强制性的.比如我参数写了1000,实际字串有1200.结果将会是插入1200长度的字串,而并没有截断.所以这一点不是非常站得住脚
2.perl的扩展与php扩展实现方式不同.php的memcache客户端是PECL.也就是C扩展,而perl的扩展实现很有可能还是perl.所以会有性能差异.

阅读内文

使用memcached分布式保存PHP session

9月 19th, 2008 | 1 Comment | Posted in PHP, memcached < by Johnny Woo >

安装完memcached之后
参考安装memcached客户端
在php.ini中
将session.save_handler 修改为memcache,并修改save_path指向memcached的地址和端口即可
session.save_handler = memcache
session.save_path = tcp://127.0.0.1:10001

Memcache的PECL这个扩展非常强大
可以支持failover以及分布存储
使用方法很讲但.
只需要在session.save_path的参数列表中
使用逗号分隔各个memcached服务器
则保存的session会经过hash之后保存到各个mc服务器中
而hash的算法.memcache支持两种,crc32以及fnv
memcache.hash_function= {crc32,fnv}
文档中很少有提到fnv算法的,据说其散列要比crc32更好
但是我通过以下小小的程序实验之后,发现仍旧是crc32的散列算法分布的更加平均.

<?php
ini_set("memcache.hash_function","crc32");
$memcache = new Memcache;
$memcache1 = new Memcache;
$memcache2 = new Memcache;
$memcache->addServer('localhost', 11211);
$memcache->addServer('localhost', 11212);
$memcache->flush();
$memcache1->connect('localhost',11211);
$memcache2->connect('localhost',11212);
$fp1 = fopen("mem1.txt","w");
$fp2 = fopen("mem2.txt","w");
for($i=0;$i<1000;$i++){
        
$memcache->set($i,$i,0,1000);
        
fwrite($fp1,$memcache1->get($i)." ");
        
fwrite($fp2,$memcache2->get($i)." ");
}
fclose($fp1);
fclose($fp2);
?>

接着我就session的保存进行了测试
我开了3个memcached进程进行测试

<?php
ini_set("memcache.hash_function","fnv");
ini_set("error_reporting","E_CORE_ERROR");
$memcache1 = new Memcache;
$memcache1->connect('localhost',11211);
$memcache1->flush();
 
$memcache2 = new Memcache;
$memcache2->connect('localhost',11212);
$memcache2->flush();
 
$memcache3 = new Memcache;
$memcache3->connect('localhost',11213);
$memcache3->flush();
$fp1 = fopen("mem1.txt","w");
$fp2 = fopen("mem2.txt","w");
$fp3 = fopen("mem3.txt","w");
for($i=0;$i<1000;$i++){
        
session_start();
        
$ssid=session_id();
        
echo $ssid;
        
session_register("id");
        
$_SESSION["id"]=$ssid;
        
session_write_close();
        
fwrite($fp1,$memcache1->get($ssid)." ");
        
fwrite($fp2,$memcache2->get($ssid)." ");
        
fwrite($fp3,$memcache3->get($ssid)." ");
        
//session_destroy();
}
fclose($fp1);
fclose($fp2);
fclose($fp3);
?>

比较奇怪的是.memcached2一般都会不被选中
而1,3的内容是一致的.可能是为了failover
而当我把1,3关闭一台后.2中将会出现内容,说明memcached2是正常工作的
而不论我的散列算法使用crc32还是fnv
这种现象都存在
最后我发现.这个测试程序存在问题
因为在session_write_close之后.整个程序的session都是唯一的了.
也就是虽然循环了这么多次.里面包含了session_destroy调用.但是返回的session id都是同样的
这就导致了两个文件中的内容一致而另一个文件中没有内容
基于此点
我只能分次调用脚本,脚本修改如下

<?php
ini_set("memcache.hash_strategy","consistent");
ini_set("memcache.hash_function","crc32");
ini_set("error_reporting","E_CORE_ERROR");
ini_set("memcache.allow_failover","0");
$memcache1 = new Memcache;
$memcache1->connect('localhost',10001);
$memcache1->flush();
 
$memcache2 = new Memcache;
$memcache2->connect('localhost',10002);
$memcache2->flush();
 
$memcache3 = new Memcache;
$memcache3->connect('localhost',10003);
$memcache3->flush();
$fp1 = fopen("mem1.txt","a+");
$fp2 = fopen("mem2.txt","a+");
$fp3 = fopen("mem3.txt","a+");
    
session_start();
    
$ssid=session_id();
    
echo $ssid."\n";
    
session_register("id");
    
$_SESSION["id"]=$ssid;
    
//session_destroy();
    
session_write_close();
    
fwrite($fp1,$memcache1->get($ssid)." ");
    
fwrite($fp2,$memcache2->get($ssid)." ");
    
fwrite($fp3,$memcache3->get($ssid)." ");
    
session_destroy();
fclose($fp1);
fclose($fp2);
fclose($fp3);
?>

然后再shell中重复运行多次.返回的ID不同了
再打开mem*.txt文件查看
发现3个文件中,每个session会保存在其中两个文件.然后分布不同
这证明了使用memcache来保存session.一个是做到了failover.第二会按照session id来做hash分布保存

阅读内文 Tags: , , ,

memcached 入门到理解

8月 28th, 2008 | No Comments | Posted in memcached < by Michael Field >

这是由 mixi 株式会社 开发部系统运营组的两位工程师所写,日常负责程序的运营。本文将针对最近在Web应用的可扩展性领域的热门话题memcached,说明其内部结构和使用。

文章分为5个章节、案例讲解,推荐大家阅读学习,中文版PDF文档下载地址见最下!

  • 第1章 memcached的基础
    • 1.1 memcached是什么?
      1.2 memcached的特征
      1.3 安装memcached
      1.4 用客户端连接
      1.5 使用Cache::Memcached
      1.6 总结
  • 第2章 理解memcached的内存存储
    • 2.1 Slab Allocation机制:整理内存以便重复使用
      2.2 在Slab中缓存记录的原理
      2.3 Slab Allocator的缺点
      2.4 使用Growth Factor进行调优
      2.5 查看memcached的内部状态
      2.6 查看slabs的使用状况
      2.7 总结
  • 第3章 memcached的删除机制和发展方向
    • 3.1 memcached在数据删除方面有效利用资源
      3.2 LRU:从缓存中有效删除数据的原理
      3.3 memcached的最新发展方向
      3.4 外部引擎支持
      3.5 总结
  • 第4章 memcached的分布式算法
    • 4.1 memcached的分布式
      4.2 Cache::Memcached的分布式方法
      4.3 Consistent Hashing
      4.4 总结
  • 第5章 memcached的应用和兼容程序
    • 5.1 mixi案例研究
      5.2 memcached应用经验
      5.3 兼容应用程序
      5.4 总结
      如今,越来越多的Web应用程序开始使用memcached这个高速的缓存服务器软件。然而,memcached的基础知识远远未能像其他Web技术那样普及,memcached在国内的大规模应用也鲜为人知。而日本的mixi(http://mixi.jp)则在这方面走在了前面,不仅大规模使用memcached作为缓存来加速Web应用,而且自行开发了Tokyo Cabinit、Tokyo Tyrant等一系列相关的软件。

      最近,日本的技术评论社的网站上刊登了mixi的两名工程师长野雅广、前坂徹撰写的一篇连载《memcachedを知り尽くす》。这篇连载语言简洁、通俗易懂,非常适合memcached入门的人阅读。

    PDF档下载地址

    阅读内文 Tags: , , , ,