首頁 >後端開發 >php教程 >Centos下循環測試php對Redis和共享記憶體(shm)讀寫的效率

Centos下循環測試php對Redis和共享記憶體(shm)讀寫的效率

不言
不言原創
2018-07-06 15:44:292239瀏覽

這篇文章主要介紹了關於Centos下循環測試php對Redis和共享內存(shm)讀寫的效率,有著一定的參考價值,現在分享給大家,有需要的朋友可以參考一下

redis和memcache還有共享記憶體都是讀取記憶體的數據,為了測試一下到底效率誰更勝一籌,我在我的Centos虛擬機下做了一次公平的測試。

測試參數

環境:Centos (設定忽略)、語言:PHP、WebServer:Nginx、測試次數:10000、字元類型及長度:字串(1024 )

準備測試

<?php
//测试数据
$arr = array( 1 => 
  array (&#39;id&#39; => &#39;4146&#39;,&#39;attacksdfdsfdsddddddddddd&#39; => &#39;5.45&#39;,&#39;atk_class&#39; => &#39;神圣级&#39;,&#39;wufang&#39; => &#39;4.9&#39;,&#39;def_class&#39; => &#39;超神级&#39;,&#39;cefang&#39; => &#39;4.9&#39;,&#39;mdf_class&#39; => &#39;超神级&#39;,&#39;hp&#39; => &#39;6.43&#39;,&#39;hp_class&#39; => &#39;神王级&#39;,&#39;dodge&#39; => &#39;55&#39;,&#39;dodge_class&#39; => &#39;SSSS&#39;,&#39;crit&#39; => &#39;50&#39;,&#39;crit_class&#39; => &#39;SSS&#39;,&#39;penetrate&#39; => &#39;55&#39;,&#39;penetrate_class&#39; => &#39;SSSS&#39;,&#39;mingzhong&#39; => &#39;51&#39;,&#39;hit_class&#39; => &#39;SSSS&#39;,&#39;anti_dizziness&#39; => &#39;0&#39;,&#39;anti_dizzinesssdfdsfdsfdsfdsfds_class&#39; => &#39;F&#39;,&#39;anti_critical&#39; => &#39;20&#39;,&#39;anti_critical_class&#39; => &#39;F&#39;,&#39;tenacity&#39; => &#39;0&#39;,&#39;tenacity_class&#39; => &#39;F&#39;,
  ),
  2 => 
  array (&#39;id&#39; => &#39;414ddffff7&#39;,&#39;attack&#39; => &#39;5.45&#39;,&#39;atk_class&#39; => &#39;神圣级&#39;,&#39;wufang&#39; => &#39;4.95&#39;,&#39;def_class&#39; => &#39;超神级&#39;,&#39;cefang&#39; => &#39;4.95&#39;,&#39;mdf_class&#39; => &#39;超神级&#39;,&#39;hp&#39; => &#39;6.43&#39;,&#39;hp_class&#39; => &#39;神王级&#39;,&#39;dodge&#39; => &#39;55&#39;,&#39;dodge_class&#39; => &#39;SSSS&#39;,&#39;crit&#39; => &#39;50&#39;,&#39;crit_class&#39; => &#39;SSS&#39;,&#39;penetrate&#39; => &#39;55&#39;,&#39;penetrate_class&#39; => &#39;SSSS&#39;,&#39;mingzhong&#39; => &#39;51&#39;,&#39;hit_class&#39; => &#39;SSSS&#39;,&#39;anti_dizziness&#39; => &#39;0&#39;,&#39;anti_dizziness_clsdfdsfdsfdsfdsfdsass&#39; => &#39;F&#39;,&#39;anti_critical&#39; => &#39;20&#39;,&#39;anti_critical_class&#39; => &#39;F&#39;,&#39;tenacity&#39; => &#39;0&#39;,&#39;tenacity_class&#39; => &#39;F&#39;,
  ));

$str = json_encode($arr);
echo strlen($str);          //输出1024

對redis進行10000次寫入測試

<?php
$redis = new Redis();
$redis->connect(&#39;127.0.0.1&#39;,6379);
$redis->auth(&#39;310c8cabcdefghf2d8abcdefd44496ac80&#39;);
$start = microtime(true) ;

for($i = 0; $i<10000; $i++) {
    $redis->set(&#39;key&#39;, $str);
}
echo microtime(true) - $start;    //执行5次,大概平均结果为  1.7016470432281 秒

對共享記憶體(shm)進行10000次寫入測試

<?php
$key = 0x4337b123;  
$size = 1024;  
$shmid = @shmop_open($key, &#39;c&#39;, 0644, $size);  
$start = microtime(true);
for($i = 0; $i<10000; $i++) {
    shmop_write($shmid, $str, 0); 
}
@shmop_close($shmid);
echo microtime(true) - $start;  //执行5次,大概平均结果为 0.0025370121002197 秒

對redis進行10000次讀取測試

<?php
$redis = new Redis();
$redis->connect(&#39;127.0.0.1&#39;,6379);
$redis->auth(&#39;310c8cabcdefghf2d8abcdefd44496ac80&#39;);
$start = microtime(true) ;

for($i = 0; $i<10000; $i++) {
    $redis->get(&#39;key&#39;);
}
echo microtime(true) - $start;     //执行5次,大概平均结果为 2.1236310005188 秒

對共享記憶體(shm)進行10000次讀取測試

<?php
$key = 0x4337b123;  
$size = 1024;  
$shmid = @shmop_open($key, &#39;c&#39;, 0644, $size);  
$start = microtime(true);
for($i = 0; $i<10000; $i++) {
    shmop_read($shmid, 0,1024);
}
@shmop_close($shmid);
echo microtime(true) - $start;     //执行5次,大概平均结果为 0.0021078586578369 秒

結果清單

#Redis     讀:2.1秒            寫:1.7秒

Shm       讀:0.0021秒     寫:0.0025 秒

1025 秒##11625 秒時可見,效果不符。

以上就是本文的全部內容,希望對大家的學習有所幫助,更多相關內容請關注PHP中文網!

相關推薦:

php實作共享記憶體進程通訊函數(_shm)

PHP程式碼自動偵測(git/ svn整合PHP_CodeSniffer)

以上是Centos下循環測試php對Redis和共享記憶體(shm)讀寫的效率的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn