Redis pipeline 在连续一万次插入操作测试中的使用中
如下代码运行后的结果是
10000noPipeline:0.36595296859741s
10000yesPipeline:0.016739130020142s
可以看出pipeline确实可以节省很多时间
<?php
/**
* 这里我想测试下Redis pipeline的功能
*/
function main(){
$redis = new Redis();
$redis->connect("127.0.0.1");
$redis->flushAll();
$start_time = microtime(true);
noPipeline($redis);
$end_time = microtime(true);
echo "noPipeline:".($end_time - $start_time)."s\n";
$redis->flushAll();
$start_time = microtime(true);
yesPipeline($redis);
$end_time = microtime(true);
echo "yesPipeline:".($end_time - $start_time)."s\n";;
}
function noPipeline(Redis $redis)
{
$i = 0;
while ($i < 10000) {
$redis->incr("x");
$i++;
}
echo $redis->get("x");
}
function yesPipeline(Redis $redis)
{
$pipe = $redis->multi(Redis::PIPELINE);
$i = 0;
while ($i < 10000) {
$pipe->incr("x");
$i++;
}
$pipe->exec();
echo $redis->get("x");
}
main();
/*
* 10000noPipeline:0.36595296859741s
* 10000yesPipeline:0.016739130020142s
*/