sequence 性能测试

来源:互联网 发布:招收淘宝客服 编辑:程序博客网 时间:2024/05/23 15:50

根据自己理解的oracle sequence的方式,实现了下,并做了性能测试,

测试环境为公司报废的笔记本电脑 dell E4300 ubuntu 13 64位系统.


代码实现:

#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <sys/time.h>


/**
* created by gongbo.yxh
  seq mod
  2013-06-11
**/


#define SEQ_CACHE_SIZE 1000 
#define START 1 


//两个seq cache
unsigned long long * seq_buf;
unsigned long long int *seq_pointer;  
int isprepared = 1;   //是否准备好 
unsigned long long int seq_extend_next_val = SEQ_CACHE_SIZE + 1;




int is_consumer_head = 1; //在定长数组中,是否生产者的指针在前面
unsigned long long int *seq_producer; //生产者指针 
unsigned long long int *seq_consumer; //消费者 


/**
初始话,将两个数组构造好,并按照sequence的要求来构造
程序关闭的时候:记住要销毁内存哦:
不销毁也不存在内存泄漏的:
**/
void init()
{
  seq_buf = (unsigned long long int *)calloc((size_t)SEQ_CACHE_SIZE,(size_t)sizeof(unsigned long long int));
  if(seq_buf == NULL){
    printf("ERROR:no memory!\n");
    exit(1);
  }
  
  for(int i=0;i < SEQ_CACHE_SIZE;i++)
  {
    seq_buf[i] = START + i; 
  }
  seq_pointer  = seq_buf;
  seq_consumer = seq_buf;
  seq_producer = seq_buf;
}


/**
* 输出打印,cache中的值:
**/
void disp()
{
  printf("first_a value:\n");
  for(int i=0;i < SEQ_CACHE_SIZE-1;i++)
  {
    printf("%lld\t",seq_buf[i]); 
  }
  printf("\n");
}


//串行化的getseq,获取序列值
unsigned long long int getseq_v1()
{
    if(seq_pointer - seq_buf == SEQ_CACHE_SIZE - 1 )
     {
        unsigned long long int rtn_val = *seq_pointer;
        //更新 buf
        for(int i=0;i <= SEQ_CACHE_SIZE-1;i++)
        {
          seq_buf[i] = seq_extend_next_val + i; 
        }


        //next val 要增加
        seq_extend_next_val = seq_extend_next_val + SEQ_CACHE_SIZE;
        seq_pointer = seq_buf;
        return rtn_val;
     }else{
      unsigned long long int *p_rtn = seq_pointer; 
      seq_pointer++;
      return *p_rtn;      
     }
}




// ----------------- test ---------------
struct timeval dwStart;
struct timeval dwEnd; 
unsigned long dwTime=0;


//测试v1的getseq函数
void test_v1()
{
  gettimeofday(&dwStart,NULL); //记录开始时间: 用于性能测量
  unsigned long long int num;
  for(int i = 0;i < 10;i++)
  {
      num = getseq_v1();
      //printf("%lld\t",num);
  }


  gettimeofday(&dwEnd,NULL); //记录结束时间: 用于性能测量
  dwTime = 1000000*(dwEnd.tv_sec-dwStart.tv_sec)+(dwEnd.tv_usec-dwStart.tv_usec);
  printf("%ld\n",dwTime);
}






int main(void)
{
  init();
  //disp();
  test_v1();


  return 0;
}


原创粉丝点击