Redis pipeline 在連續一萬次插入操做測試中的使用中
以下代碼運行後的結果是php
10000noPipeline:0.36595296859741s 10000yesPipeline:0.016739130020142s
能夠看出pipeline確實能夠節省不少時間redis
<?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 */