phpRedis性能测试

http://redis.io/clients
redis官方推荐了两个php的redis客户端,Predis和phpredis。其中,Predis为纯php实现,phpredis为C语言的扩展。

Predis为php源码码,扩展方便,但只支持php5.3+,phpredis性能应该会好一些,写了一个测试脚本来测试。
测试的笔记本配置为i7 2630Q+10G

phpredis
https://github.com/nicolasff/phpredis

redis.php
[php]
<?php
class cache
{
function __construct()
{
$this->_instance();
}

protected function _instance()
{
static $_instance;

if(empty($_instance))
$_instance = $this->connect();
return $_instance;
}

function connect()
{
$this->db = new redis();
$this->db->connect(‘127.0.0.1’, ‘6379’);
return $this;
}

function __call($method, $args)
{
return call_user_func_array(array($this->db, $method) , $args);
}
}

$redis = new cache();
$key_pre = time();
echo "pre : $key_pre\n";

//测试百万次循环
$s = microtime(true);
while($total < 1000000)
{
$total++;
}
$e = microtime(true);
printf("1 000 000 loop cost %4f\n", $e-$s);

//测试百万次写入
$total = 0;
$s = microtime(true);
while($total <= 1000000)
{
$redis->set("$key_pre:{$total}", $total);
$total++;
}
$e = microtime(true);
printf("1 000 000 set cost %4f, lastkey: %s\n", $e-$s, "$key_pre:{$total}");

//测试百万次读取
$total = 0;
$s = microtime(true);
while($total <= 1000000)
{
$redis->get( "$key_pre:{$total}" );
$total++;
}
$e = microtime(true);
printf("1 000 000 get cost %4f, lastkey: %s\n", $e-$s, "$key_pre:{$total}");

//测试百万级 key 查找
$total = 0;
$s = microtime(true);
$redis->keys("$key_pre:*");
$e = microtime(true);
printf("1 000 000 key find cost %4f\n", $e-$s);
[/php]

执行结果
[shell]
shuhai@Aspire:/wwwroot$ php redis.php
pre : 1358331812
1 000 000 loop cost 0.039773
1 000 000 set cost 91.499741, lastkey: 1358331812:1000001
1 000 000 get cost 111.465971, lastkey: 1358331812:1000001
1 000 000 key find cost 1.173917
[/shell]