redis memcache 性能比較_負載集群教程

      編輯Tag賺U幣

      redis和memcache非常像的,都是key,value的方式,將數據存放內存中。最近在學習redis,在網上看了一些這方面的資料,有三種觀點:

      1,redis讀寫內存比memcache快

      2,memcache讀寫內存比redis快

      3,memcache讀寫內存比redis快,但是redis整體性能優于memcache

      所以我做了一下測試。關于redis和memcache的安裝,請參考linux redis 安裝配置, 以及redis php擴展

      linux memcache 安裝


      1,redis的測試文件


      1.<?php  
      2.function get_data (){  
      3.   mysql_connect("localhost", "root", "") or die("Could not connect: " . mysql_error());  
      4.   mysql_select_db("ugc");  
      5. 
      6.   $result = mysql_query("SELECT task_id FROM ugc_tasks");  
      7.   $return = array();  
      8.   while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {  
      9.      $return[] = $row;  
      10.   }  
      11. 
      12.   mysql_free_result($result);  
      13.   return $return;  
      14.}  
      15. 
      16.$redis = new redis();  
      17.$redis->connect('127.0.0.1', 6379);  
      18. 
      19.if ($redis->exists('test')) {  
      20.   $value = $redis->get("test");  
      21.}else{  
      22.   $value = get_data();  
      23.   $redis->set('test',json_encode($value));  
      24.}  
      25. 
      26.print_r(json_decode($value));  
      27.?> 
      <?php
      function get_data (){
         mysql_connect("localhost", "root", "") or die("Could not connect: " . mysql_error());
         mysql_select_db("ugc");

         $result = mysql_query("SELECT task_id FROM ugc_tasks");
         $return = array();
         while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
            $return[] = $row;
         }

         mysql_free_result($result);
         return $return;
      }

      $redis = new redis();
      $redis->connect('127.0.0.1', 6379);

      if ($redis->exists('test')) {
         $value = $redis->get("test");
      }else{
         $value = get_data();
         $redis->set('test',json_encode($value));
      }

      print_r(json_decode($value));
      ?>
      2,redis的測試結果

      第一次
      root@ubuntu:/home/zhangying/download/webbench-1.5# webbench -c 10000 -t 30 http://localhost/php-redis/test_redis.php
      Webbench - Simple Web Benchmark 1.5
      Copyright (c) Radim Kolar 1997-2004, GPL Open Source Software.

      Benchmarking: GET http://localhost/php-redis/test_redis.php
      10000 clients, running 30 sec.

      Speed=48324 pages/min, 40318471 bytes/sec.
      Requests: 22599 susceed, 1563 failed.

      telnet 127.0.0.1 6379 telnet登錄一下,把test對應的值清除掉,保重測試的公平性
      del test

      第二次
      root@ubuntu:/home/zhangying/download/webbench-1.5# webbench -c 10000 -t 30 http://localhost/php-redis/test_redis.php
      Webbench - Simple Web Benchmark 1.5
      Copyright (c) Radim Kolar 1997-2004, GPL Open Source Software.

      Benchmarking: GET http://localhost/php-redis/test_redis.php
      10000 clients, running 30 sec.

      Speed=53570 pages/min, 41217689 bytes/sec.
      Requests: 23106 susceed, 3679 failed.

      telnet 127.0.0.1 6379
      del test

      第三次
      root@ubuntu:/home/zhangying/download/webbench-1.5# webbench -c 10000 -t 30 http://localhost/php-redis/test_redis.php
      Webbench - Simple Web Benchmark 1.5
      Copyright (c) Radim Kolar 1997-2004, GPL Open Source Software.

      Benchmarking: GET http://localhost/php-redis/test_redis.php
      10000 clients, running 30 sec.

      Speed=49450 pages/min, 39694073 bytes/sec.
      Requests: 22301 susceed, 2424 failed.

      telnet 127.0.0.1 6379
      del test

      3,memcache測試文件


      1.<?php  
      2.function get_data (){  
      3.   mysql_connect("localhost", "root", "") or die("Could not connect: " . mysql_error());  
      4.   mysql_select_db("ugc");  
      5. 
      6.   $result = mysql_query("SELECT task_id FROM ugc_tasks");  
      7.   $return = array();  
      8.   while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {  
      9.       $return[] = $row;  
      10.   }  
      11. 
      12.   mysql_free_result($result);  
      13.   return $return;  
      14.}  
      15. 
      16.$mem = new Memcache;  
      17.$mem->connect("127.0.0.1",11211) or die ("Could not connect");  
      18.$value = $mem->get('test1');  
      19.if (emptyempty($value)) {  
      20.   $value = json_encode(get_data());  
      21.   $mem->set('test1',$value,0, 600);  
      22.}  
      23. 
      24.print_r(json_decode($value));  
      25.?> 
      <?php
      function get_data (){
         mysql_connect("localhost", "root", "") or die("Could not connect: " . mysql_error());
         mysql_select_db("ugc");

         $result = mysql_query("SELECT task_id FROM ugc_tasks");
         $return = array();
         while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
             $return[] = $row;
         }

         mysql_free_result($result);
         return $return;
      }

      $mem = new Memcache;
      $mem->connect("127.0.0.1",11211) or die ("Could not connect");
      $value = $mem->get('test1');
      if (empty($value)) {
         $value = json_encode(get_data());
         $mem->set('test1',$value,0, 600);
      }

      print_r(json_decode($value));
      ?>
      4,memcache測試結果

      第一次

      root@ubuntu:/home/zhangying/download/webbench-1.5# webbench -c 10000 -t 30 http://localhost/php-redis/test_memcache.php
      Webbench - Simple Web Benchmark 1.5
      Copyright (c) Radim Kolar 1997-2004, GPL Open Source Software.

      Benchmarking: GET http://localhost/php-redis/test_memcache.php
      10000 clients, running 30 sec.

      Speed=61632 pages/min, 52228667 bytes/sec.
      Requests: 29205 susceed, 1611 failed.

      telnet 127.0.0.1 11211 telnet登錄一下,把test1對應的值清除掉,保重測試的公平性
      delete test1

      第二次

      root@ubuntu:/home/zhangying/download/webbench-1.5# webbench -c 10000 -t 30 http://localhost/php-redis/test_memcache.php
      Webbench - Simple Web Benchmark 1.5
      Copyright (c) Radim Kolar 1997-2004, GPL Open Source Software.

      Benchmarking: GET http://localhost/php-redis/test_memcache.php
      10000 clients, running 30 sec.

      Speed=64160 pages/min, 52601449 bytes/sec.
      Requests: 29426 susceed, 2654 failed.

      telnet 127.0.0.1 11211
      delete test1

      第三次

      root@ubuntu:/home/zhangying/download/webbench-1.5# webbench -c 10000 -t 30 http://localhost/php-redis/test_memcache.php
      Webbench - Simple Web Benchmark 1.5
      Copyright (c) Radim Kolar 1997-2004, GPL Open Source Software.

      Benchmarking: GET http://localhost/php-redis/test_memcache.php
      10000 clients, running 30 sec.

      Speed=65190 pages/min, 52506614 bytes/sec.
      Requests: 29348 susceed, 3247 failed.

      telnet 127.0.0.1 11211
      delete test1

      從上面比較結果,可以看出,memcache比redis快的。redis對key,value的管理,更靈活。有很多人把redis歸于nosql的范圍,細細想,還真是那么一回事。redis還可以把內在中的數據,放到磁盤中,這一點上,redis更像memcachedb。關于使用哪一種,看個人喜好而定了。

      來源:網絡搜集//所屬分類:負載集群教程/更新時間:2011-12-08
      相關負載集群教程