C++ MemCache测试报告

来源:互联网 发布:淘宝app电脑下载安装 编辑:程序博客网 时间:2024/06/06 19:48

1.1、测试代码

Set部分:

void testSingleSet( memcached_st *memc, unsigned int testTimes, string value ){

       unsigned int i = 0;

       memcached_return rc;

       char buf[30] = {0};

       struct timeval tpstart,tpend;

    float timeuse;

       gettimeofday(&tpstart,NULL);

 

       for( i = 0; i < testTimes; i++ ){

           sprintf( buf, "%d", i);

           string key(buf);

           size_t value_length = value.length();

           size_t keyLength = key.length();

        rc=memcached_set(memc,key.c_str(),key.length(),value.c_str(),value.length(),expiration,flags);

        if(rc !=MEMCACHED_SUCCESS)

        {

            //cout<<"Save data:"<<value<<" sucessful!"<<endl;

               getchar();

        }

       }

 

       gettimeofday(&tpend,NULL);

    timeuse=1000000*(tpend.tv_sec-tpstart.tv_sec)+

    tpend.tv_usec-tpstart.tv_usec;

    timeuse/=1000000;

    printf("Set %u items Used Time:%f/n", testTimes, timeuse);

}

 

Get部分:

void testSingleGet( memcached_st *memc, unsigned int testTimes, size_t valueLength ){

       unsigned int i = 0;

       memcached_return rc;

       char buf[30] = {0};

       struct timeval tpstart,tpend;

    float timeuse;

       gettimeofday(&tpstart,NULL);

 

       for( i = 0; i < testTimes; i++ ){

           sprintf( buf, "%d", i);

           string key(buf);

           size_t value_length = valueLength;

           size_t keyLength = key.length();

           char* result = memcached_get(memc,key.c_str(), keyLength, &value_length, &flags, &rc);

           if(rc != MEMCACHED_SUCCESS)

           {

              //cout<<"Get value:"<<result<<" sucessful!"<<endl;

                  getchar();

           }

       }

 

       gettimeofday(&tpend,NULL);

    timeuse=1000000*(tpend.tv_sec-tpstart.tv_sec)+

    tpend.tv_usec-tpstart.tv_usec;

    timeuse/=1000000;

    printf("Get %u Used Time:%f/n", testTimes, timeuse);

}

 

Delete部分:

void testSingleDel( memcached_st *memc, unsigned int testTimes ){

    char buf[20];

    memcached_return rc;

 

       struct timeval tpstart,tpend;

    float timeuse;

       gettimeofday(&tpstart,NULL);

 

    for( unsigned int i = 0; i < testTimes; i++ ){

           sprintf( buf, "%d", i);

           string key(buf);

        //string value = "test";

           //string value = "1|1|1301449476|67|33|13272|13272|79|23|7|3|3|74|1|2.000000|4.000000|1.000000|3|3|PandaTest";

        //size_t value_length = value.length();

        size_t key_length = key.length();

        rc=memcached_delete( memc, key.c_str(), key_length, expiration );

        if(rc != MEMCACHED_SUCCESS)

        {

               //cout<<"Delete key:"<<key<<" sucessful!"<<endl;

               getchar();

        }

    }

 

       gettimeofday(&tpend,NULL);

    timeuse=1000000*(tpend.tv_sec-tpstart.tv_sec)+

    tpend.tv_usec-tpstart.tv_usec;

    timeuse/=1000000;

    printf("Delete %u items Used Time:%f/n", testTimes, timeuse);

}

 

遍历部分:

memcached_return_t my_memcached_dump_fn(const memcached_st *ptr, const char *key,

              size_t key_length, void *context){

       memCount++;

       return MEMCACHED_SUCCESS;

}

 

void testList( memcached_st *memc ){

       unsigned int i = 0;

       memcached_return rc;

       //char* context = (char*)malloc(10);

       char* context;

       memcached_dump_fn fn = my_memcached_dump_fn;

 

       struct timeval tpstart,tpend;

    float timeuse;

       gettimeofday(&tpstart,NULL);

 

       memcached_dump(memc, &fn, context, 1);

 

       gettimeofday(&tpend,NULL);

    timeuse=1000000*(tpend.tv_sec-tpstart.tv_sec)+

    tpend.tv_usec-tpstart.tv_usec;

    timeuse/=1000000;

    printf("List %u items Used Time:%f/n",memCount, timeuse);

 

       printf("The memCount is %u/n", memCount);

}

 

 

 

 

1.2、测试结果

1.       测试结果说明

从测试数据可以看出来,memcache的删除并不是直接删除的,删除的东西还是存在的,只是无法get出来而已(这个我测试了,get会出错的),而超时的数据和被删除的处理方法是一样的,只是无法get,但是可以遍历找到相应的Key

 

结果显示,当数据量达到100000后,遍历过的数据和实际插入的数据的数量开始产生差异。说明当数据量到达一定程度之后,遍历出现了问题。而且遍历得到的仅仅是已经缓存在哪里的key的值,不包含其中的value,所以如果做处理的话,还需要将他们get出来。

上述结果都是对长度很短的Key进行操作,长度最多不会超过6位,如果我们将Keyvalue的长度加倍,进行10万次操作,可以看出对valuekey的长度对memcache的性能并没有太大的影响。

 

3.       仿真测试:

如果将所有的数据进行仿真,然后统计处理仅仅计算delay, loss, openport的平均值,在遍历不出错的情况下,我进行了测试,所需时间大概为2.419011 s,然后处理的数量是40000条。如果数量加大,会出现遍历不到,或者遍历到了但是没有调用回调函数的情况。

 

2. 使用Memcache可能带来的问题:

1. 因为遍历程序完全可以遍历到那些失效和被删除的k-v对,所以,如果使用memcache,那么我们在遍历的时候会发现我们遍历到了异常多的数据,然后用这些keyget,当get的时候,会对其生命周期和是否被删除进行检查,这时候那些被删除和已经失效的数据将不能get出来,但是已经浪费了大量的时间,同时,如果对于get错误记录日志的话,还会造成日志文件的大量浪费。

2. 由于遍历程序仅仅能够遍历key,同时对相应的key进行的操作需要在回调函数中解决,所以不能进行复杂的操作,只能将key放入容器中,等待遍历完进行处理,这样需要很多的内存空间。而且这些key中有些是失效的,是的有效利用率更低。