zoukankan      html  css  js  c++  java
  • redis 与 memcache 性能测试比较

    试验场景一:要对同一个list实时添加元素,且放入缓存中。代码如下:

        @Test  
        public void testMemCacheAndRedis() {  
          this.testInsert(1000);  
        }  
          
        public void testInsert(int size) {  
          MemCached memCached = (MemCached) ctx.getBean("configMemCache");  
          memCached.flushAll();  
          long before = System.currentTimeMillis();  
          List<Integer> list = new ArrayList<Integer>();  
          for (int i = 0; i < size; i++) {  
            list.add(i);  
            memCached.put("test", list);  
          }  
          long end = System.currentTimeMillis();  
          
          RedisTemplate<String, Object> redisTemplate = (RedisTemplate<String, Object>) ctx.getBean("bubbleRedisTemplate");  
          redisTemplate.delete("test123");  
          long before2 = System.currentTimeMillis();  
          for (int i = 0; i < size; i++) {  
            redisTemplate.boundListOps("test123").leftPush(i);  
          }  
          long end2 = System.currentTimeMillis();  
          System.out.println("memcached use time is " + (end - before));  
          System.out.println("redis use time is " + (end2 - before2));  
        }  



    当n=1000时的任意3次打印结果:
    =============================================
    memcached use time is 6282
    redis use time is 1156

    memcached use time is 6547
    redis use time is 1172

    memcached use time is 6234
    redis use time is 1375

    当n=2000时的任意3次打印结果:
    =============================================
    memcached use time is 22313
    redis use time is 2328

    memcached use time is 22969
    redis use time is 2907

    memcached use time is 22938
    redis use time is 2234

    当n=3000时的任意3次打印结果:
    =============================================
    memcached use time is 48859
    redis use time is 3297

    memcached use time is 48156
    redis use time is 4547

    memcached use time is 47765
    redis use time is 3375

    试验一结论(只对本次试验负责):当n在20条以内时,两者差异不大,但当n大于20时,redis在性能上的优势就会逐渐体现出来。

    试验场景二:对程序做些稍许的改动,即将memCached.put("xwq", list);这一行移到for循环外面执行,代码如下:

     1      @Test  
     2     public void testMemCacheAndRedis() {  
     3       this.testBatchInsert(3000);  
     4     }  
     5       
     6     public void testBatchInsert(int size) {  
     7       MemCached memCached = (MemCached) ctx.getBean("configMemCache");  
     8       memCached.flushAll();  
     9       long before = System.currentTimeMillis();  
    10       List<Integer> list = new ArrayList<Integer>();  
    11       for (int i = 0; i < size; i++) {  
    12         list.add(i);  
    13       }  
    14       memCached.put("test", list);  
    15       long end = System.currentTimeMillis();  
    16       
    17       RedisTemplate<String, Object> redisTemplate = (RedisTemplate<String, Object>) ctx.getBean("bubbleRedisTemplate");  
    18       redisTemplate.delete("test123");  
    19       long before2 = System.currentTimeMillis();  
    20       for (int i = 0; i < size; i++) {  
    21         redisTemplate.boundListOps("test123").leftPush(i);  
    22       }  
    23       long end2 = System.currentTimeMillis();  
    24       System.out.println("memcached use time is " + (end - before));  
    25       System.out.println("redis use time is " + (end2 - before2));  
    26     }  



    我们再做下试验:
    当n=1000时的任意3次打印结果:
    ===================================================
    memcached use time is 15
    redis use time is 1188

    memcached use time is 32
    redis use time is 1188

    memcached use time is 31
    redis use time is 1234

    当n=2000时的任意3次打印结果:
    ===================================================
    memcached use time is 47
    redis use time is 2437

    memcached use time is 31
    redis use time is 2344

    memcached use time is 47
    redis use time is 2265

    但n=3000时的任意3次打印结果:
    ===================================================
    memcached use time is 62
    redis use time is 3406

    memcached use time is 47
    redis use time is 3391

    memcached use time is 47
    redis use time is 3688

    试验二结论(只对本次试验负责):由此可以看出,memcached一次性将list放入到缓存中,比redis每次更新性能是要高很多的。

    试验场景三:对同样大小的两个list,同时进行相同次数的读操作,代码如下:
     

     1  @Test
     2   public void testMemCacheAndRedis() {
     3     // this.testInsert(3000);
     4     // this.testBatchInsert(3000);
     5     this.testRead(200);
     6   }
     7 
     8   public void testRead(int size) {
     9     MemCached memCached = (MemCached) ctx.getBean("configMemCache");
    10     memCached.flushAll();
    11     List<Integer> list = new ArrayList<Integer>();
    12     for (int i = 0; i < 1000; i++) {
    13       list.add(i);
    14     }
    15     memCached.put("test", list);
    16 
    17     long before = System.currentTimeMillis();
    18     for (int i = 0; i < size; i++) {
    19       memCached.get("test");
    20     }
    21     long end = System.currentTimeMillis();
    22 
    23     RedisTemplate<String, Object> redisTemplate = (RedisTemplate<String, Object>) ctx.getBean("bubbleRedisTemplate");
    24     redisTemplate.delete("test123");
    25     for (int i = 0; i < 1000; i++) {
    26       redisTemplate.boundListOps("test123").leftPush(i);
    27     }
    28 
    29     long before2 = System.currentTimeMillis();
    30     for (int i = 0; i < size; i++) {
    31       redisTemplate.boundListOps("xwq123").range(0, -1);
    32     }
    33     long end2 = System.currentTimeMillis();
    34     System.out.println("memcached use time is " + (end - before));
    35     System.out.println("redis use time is " + (end2 - before2));
    36   } 



    当n=100时的任意3次打印结果:
    ============================================
    memcached use time is 6765
    redis use time is 281

    memcached use time is 2187
    redis use time is 297

    memcached use time is 9547
    redis use time is 313

    当n=200时的任意3次打印结果:
    ============================================
    memcached use time is 34250
    redis use time is 546

    memcached use time is 12171
    redis use time is 547

    memcached use time is 8297
    redis use time is 516

    当n=500时的任意3次打印结果:
    ============================================
    memcached use time is 69437
    redis use time is 1297

    memcached use time is 43781
    redis use time is 1516

    memcached use time is 37750
    redis use time is 1281

    试验三结论(只对本次试验负责):同样是从缓存中读取数据,redis的性能是要远远高于memcached。


    综上所述结论(只对本次次试验负责):通过上述三次试验,redis在 读操作和写操作上是全面领先memcached的。但有一点,memcached可以一次性将集合的数据放入自己的缓存中,而redis却不行(有还是木 有?),所以memcached并不是一无是处的,在很多场景下,二者是需要交叉使用的,要根据实际场景进行取舍。

  • 相关阅读:
    eclipse开发环境搭建
    如何在不知道密码的情况下卸载趋势防毒墙网络版
    PB 将游标中的数据存到数组中
    SQL Server 系统表使用举例
    row_number() over(partition by a order by b desc) rn 用法
    C#选中当前一行,判断是否为空(顺序)
    C#DataTable与Grid的差别
    C#采番的前端与SQL的写法
    Grid的check
    Combox的去重方法
  • 原文地址:https://www.cnblogs.com/pan123/p/4252932.html
Copyright © 2011-2022 走看看