码迷,mamicode.com
首页 > 其他好文 > 详细

提高redis性能一指禅

时间:2015-03-17 08:14:09      阅读:169      评论:0      收藏:0      [点我收藏+]

标签:

     江湖传言,在redis中,一个command("get or set key value")中value的长度超过1k的时候,其性能急剧下降。

     稍微看了下redis的代码,发现它的网络模块关闭了nagel算法,如果修改redis的源码启动这个算法会如何呢?

1 采用redis默认代码测试(即关闭nagel算法)

     为了验证这个算法启用与否,是否影响了redis的性能。先不改变redis的代码,利用redis-benchmark进行一番测试,执行下面的命令:

./redis-benchmark -p 30000 -n 50000 -k 1 -d 2048 -q

    上面命令相关参数意义为:连接服务器断开30000、测试50000次、打开keepalive选项、value长度2k、只输出结果不输出测试过程。 

    测试结果是:

SET: 51599.59 requests per second
GET: 50403.23 requests per second
INCR: 55493.89 requests per second
LPUSH: 54112.55 requests per second
LPOP: 48875.86 requests per second
SADD: 54644.81 requests per second
SPOP: 54112.55 requests per second
LPUSH (needed to benchmark LRANGE): 49067.71 requests per second
LRANGE_600 (first 600 elements): 400.68 requests per second
MSET (10 keys): 33134.53 requests per second

    

2 采用socket默认属性

    下面是/redis-2.8.19/networking.c的createClient函数:

  53 redisClient *createClient(int fd) {
  54     redisClient *c = zmalloc(sizeof(redisClient));
  55 
  56     /* passing -1 as fd it is possible to create a non connected client.
  57      * This is useful since all the Redis commands needs to be executed
  58      * in the context of a client. When commands are executed in other
  59      * contexts (for instance a Lua script) we need a non connected client. */
  60     if (fd != -1) {
  61         anetNonBlock(NULL,fd);
  62         // anetEnableTcpNoDelay(NULL,fd);
  63         if (server.tcpkeepalive)
  64             anetKeepAlive(NULL,fd,server.tcpkeepalive);
  65         if (aeCreateFileEvent(server.el,fd,AE_READABLE,
  66             readQueryFromClient, c) == AE_ERR)
  67         {
  68             close(fd);
  69             zfree(c);
  70             return NULL;
  71         }
  72     }

    请注意,我已经把line 62注释掉,采用系统默认设置。

    执行同样的命令,其测试结果为:

SET: 50968.40 requests per second
GET: 50607.29 requests per second
INCR: 55432.37 requests per second
LPUSH: 47438.33 requests per second
LPOP: 49950.05 requests per second
SADD: 54945.05 requests per second
SPOP: 55187.64 requests per second
LPUSH (needed to benchmark LRANGE): 47709.93 requests per second
LRANGE_600 (first 600 elements): 398.41 requests per second
MSET (10 keys): 26766.60 requests per second

3 明确启用Nagel算法 

  同样地,可以修改代码以明确启用Nagel算法,同样的代码块修改后为:

  53 redisClient *createClient(int fd) {
  54     redisClient *c = zmalloc(sizeof(redisClient));
  55 
  56     /* passing -1 as fd it is possible to create a non connected client.
  57      * This is useful since all the Redis commands needs to be executed
  58      * in the context of a client. When commands are executed in other
  59      * contexts (for instance a Lua script) we need a non connected client. */
  60     if (fd != -1) {
  61         anetNonBlock(NULL,fd);
  62         // anetEnableTcpNoDelay(NULL,fd);
  63         anetDisableTcpNoDelay(NULL,fd);
  64         if (server.tcpkeepalive)
  65             anetKeepAlive(NULL,fd,server.tcpkeepalive);
  66         if (aeCreateFileEvent(server.el,fd,AE_READABLE,
  67             readQueryFromClient, c) == AE_ERR)
  68         {
  69             close(fd);
  70             zfree(c);
  71             return NULL;
  72         }
  73     }

   明确地启用Nagel算法后,其测试结果为:

SET: 49164.21 requests per second
GET: 48496.61 requests per second
INCR: 52910.05 requests per second
LPUSH: 50556.12 requests per second
LPOP: 49164.21 requests per second
SADD: 53937.43 requests per second
SPOP: 53248.14 requests per second
LPUSH (needed to benchmark LRANGE): 48169.56 requests per second
LRANGE_600 (first 600 elements): 406.73 requests per second
MSET (10 keys): 34106.41 requests per second

4 总结

   从以上对比结果来看,大部分情况下启用nagel算法后,redis的性能约有百分之二的提升。


提高redis性能一指禅

标签:

原文地址:http://my.oschina.net/u/875730/blog/387715

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!