redis和mysql的數據寫入測試

1)PHP 寫mysql數據的代碼

 

  1. <?php  
  2. $mysqli=mysqli_connect("10.1.0.169","root","123456","test","3306");  
  3. class runtime  
  4. {  
  5.     var $StartTime = 0;  
  6.     var $StopTime = 0;  
  7.  
  8.     function get_microtime()  
  9.     {  
  10.         list($usec$sec) = explode(' ', microtime());  
  11.         return ((float)$usec + (float)$sec);  
  12.     }  
  13.  
  14.     function start()  
  15.     {  
  16.         $this->StartTime = $this->get_microtime();  
  17.     }  
  18.  
  19.     function stop()  
  20.     {  
  21.         $this->StopTime = $this->get_microtime();  
  22.     }  
  23.  
  24.     function spent()  
  25.     {  
  26.         return round(($this->StopTime - $this->StartTime) * 1000, 1);  
  27.     }  
  28.  
  29. }  
  30. $runtimenew runtime;  
  31. $runtime->start();  
  32.  
  33. for($i=1;$i<100000;$i++){  
  34. $ins = "insert into `test_order` (`id`)vlaue ('$i')";  
  35. mysqli_query($mysqli,$ins);  
  36. }  
  37. $runtime->stop();  
  38. echo "insert sucess! 頁面執行時間: ".$runtime->spent()." 毫秒\n";  
  39. mysqli_close($mysqli);  
  40. ?>  

結果:

使用PHP mysql 插入10W條數據時的狀態:執行時間是70

 

使用PHP mysql 插入50W條數據時的狀態:執行時間是335

 

 

2) 安裝phpredis模塊

1.         https://github.com/owlient/phpredis 下載nicolasff-phpredis-2.1.3-167-ga5e53f1.zip

解壓 unzip –q nicolasff-phpredis-2.1.3-167-ga5e53f1.zip

2.         cd phpredis

/usr/local/php/bin/phpize      這個phpize是安裝php模塊的

./configure --with-php-config=/usr/local/php/bin/php-config

make && make install

確認是否生成redis擴展庫:

/usr/local/php/lib/php/extensions/no-debug-non-zts-20090626/

php.ini中添加extension=redis.so

重新加載php  /etc/init.d/php-fpm reload

 

3.         php代碼測試

<?php

         $redis = new Redis();

         $redis->connect('10.1.0.169',6379);

         $redis->set('test','hello world!');

         echo $redis->get('test');

?>

輸出hello world!

3) PHP 寫入redis數據的代碼

rpush.php代碼:

$redis = new Redis();

$redis->connect('10.1.0.169',6379);

$redis->rpush("order","$ins");

 

lpop.php代碼:

$num=$redis->llen("order");

while ($num) {

  $ins=$redis->lpop("order");

  mysqli_query($mysqli,$ins);

  $num=$redis->llen("order");

}

 

 

結論:

使用PHP redis插入10W條數據時的狀態:

執行時間是24

使用redis mysql插入10W條數據時的狀態:

執行時間是120

使用PHP redis 插入50W條數據時的狀態:

執行時間是124

使用redis mysql插入50W條數據時的狀態:

執行時間是735

 

4) 通過測試,驗證redismysql每秒的插入效率:

 

測試結論:在網絡rtt平均時間爲0.2msvalue值大小平均是1kb的情況下,

redis 插入隊列數據的效率大約爲每秒 4200value

 

同等條件下,mysql插入數據的效率大約爲每秒 700,同樣大小數據的insert操作

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章