设为首页 加入收藏

TOP

Redis源码分析(十三)---redis-benchmark性能测试(一)
2014-11-24 07:47:35 来源: 作者: 【 】 浏览:1
Tags:Redis 源码 分析 十三 ---redis-benchmark 性能 测试

今天讲的这个是用来给redis数据库做性能测试的,说到性能测试,感觉这必然是高大上的操作了,redis性能测试,测的到底是哪方面的性能,如何测试,通过什么指标反映此次测试的性能好坏呢,下面我通过源码给大家做一一解答。

redis做的性能测试时对立面的基本操作做的检测,比如Client客户端执行set,get,lpush等数据操作的性能,可以从他的测试程序可以看出:

 if (test_is_selected("get")) {
            len = redisFormatCommand(&cmd,"GET key:__rand_int__");
            benchmark("GET",cmd,len);
            free(cmd);
        }

        if (test_is_selected("incr")) {
            len = redisFormatCommand(&cmd,"INCR counter:__rand_int__");
            benchmark("INCR",cmd,len);
            free(cmd);
        }

        if (test_is_selected("lpush")) {
            len = redisFormatCommand(&cmd,"LPUSH mylist %s",data);
            benchmark("LPUSH",cmd,len);
            free(cmd);
        }

        if (test_is_selected("lpop")) {
            len = redisFormatCommand(&cmd,"LPOP mylist");
            benchmark("LPOP",cmd,len);
            free(cmd);
        }
那么通过什么指标反映测试性能的好坏之分呢,在这里我们使用的就是延时性来判断,最简单的想法,就是在测试到额最开始,记录一个时间,中间执行测试操作,在操作结束在记录一个时间,中间的时间差就是执行的时间,时间越短说明性能越好。这也正是redis性能测试的做法。
/* 对指定的CMD命令做性能测试 */
static void benchmark(char *title, char *cmd, int len) {
    client c;

    config.title = title;
    config.requests_issued = 0;
    config.requests_finished = 0;

    c = createClient(cmd,len,NULL);
    createMissingClients(c);

    config.start = mstime();
    aeMain(config.el);
    //最后通过计算总延时,显示延时报告,体现性能测试的结果
    config.totlatency = mstime()-config.start;

    showLatencyReport();
    freeAllClients();
}

因为这样的操作要求时间精度比较高,用秒做单位肯定不行了,所以这里用的是ms毫秒,在这里添加个知识点,在这里用到了时间相关的结构体,在Linux里也存在:

/* 介绍一下struct timeva l结构体
struct timeva l结构体在time.h中的定义为:
struct timeva l
{
  __time_t tv_sec;        // Seconds. 
  __suseconds_t tv_usec;    // Microseconds. (微秒)
}; */

那么下面是最关键的问题了,如何测,测试肯定要通过一个模拟客户端,按照配置文件中的设置去测试,所以必须存在一个配置信息,然后我们通过我们想要的配置再去逐步测试,亮出config,配置信息:
/* config配置信息结构体,static静态变量,使得全局只有一个 */
static struct config {
	//消息事件
    aeEventLoop *el;
    const char *hostip;
    int hostport;
    //据此判断是否是本地测试
    const char *hostsocket;
    //Client总数量
    int numclients;
    int liveclients;
    //请求的总数
    int requests;
    int requests_issued;
    //请求完成的总数
    int requests_finished;
    int keysize;
    int datasize;
    int randomkeys;
    int randomkeys_keyspacelen;
    int keepalive;
    int pipeline;
    long long start;
    long long totlatency;
    long long *latency;
    const char *title;
    //Client列表,这个在下面会经常提起
    list *clients;
    int quiet;
    int csv;
    //判断是否loop循环处理
    int loop;
    int idlemode;
    int dbnum;
    sds dbnumstr;
    char *tests;
    char *auth;
} config;

typedef struct _client {
	//redis上下文
    redisContext *context;
    //此缓冲区将用于后面的读写handler
    sds obuf;
    //rand指针数组
    char **randptr;         /* Pointers to :rand: strings inside the command buf */
    //randptr中指针个数
    size_t randlen;         /* Number of pointers in client->randptr */
    //randptr中没有被使用的指针个数
    size_t randfree;        /* Number of unused pointers in client->randptr */
    unsigned int written;   /* Bytes of 'obuf' already written */
    //请求的发起时间
    long long start;        /* Start time of a request */
    //请求的延时
    long long latency;      /* Request latency */
    //请求的等待个数
    int pending;            /* Number of pending requests (replies to consume) */
    int selectlen;  /* If non-zero, a SELECT of 'selectlen' bytes is currently
                       used as a prefix of the pipline of commands. This gets
                       discarded the first time it's sent. */
} *client;
上面还附带了client的一些信息,这里的Client和之前的RedisClient还不是一个东西,也就是说,这里的Client会根据config结构体中的配置做相应的操作。client根据获得到命令无非2种操作,read和Write,所以在后面的事件处理中也是针对这2种事件的处理,这里给出read的方法:
/* 读事件的处理方法 *
首页 上一页 1 2 3 下一页 尾页 1/3/3
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇kettle参数、变量详细讲解 下一篇通过cmd命令连接mysql

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容:

·请问c语言刚入门,该 (2025-12-26 10:21:04)
·python 编程怎么定义 (2025-12-26 10:21:01)
·09-指 针 (一)-c语言 (2025-12-26 10:20:58)
·About - Redis (2025-12-26 08:20:56)
·Redis: A Comprehens (2025-12-26 08:20:53)