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
";
$redis->flushAll();
$start_time = microtime(true);
yesPipeline($redis);
$end_time = microtime(true);
echo "yesPipeline:".($end_time - $start_time)."s
";;
}
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
*/