美文网首页
内存延时测试

内存延时测试

作者: yanfeizhang | 来源:发表于2019-10-11 08:56 被阅读0次
#include <stdlib.h>
#include <stdio.h>
#include "clock.h" /* routines to access the cycle counter */

#define MINBYTES (1 << 11)  /* Working set size ranges from 2 KB */
#define MAXBYTES (1 << 26)  /* ... up to 64 MB */
#define MAXSTRIDE 64        /* Strides range from 1 to 64 elems */
#define MAXELEMS MAXBYTES/sizeof(double) 

#define ASIZE (1 << 17)

double data[MAXELEMS];      /* The global array we'll be traversing */

/* start declare functions */
void init_data(double *data, int n);
void run_delay_testing();
double get_seque_access_result(int size, int stride, int type);
double get_random_access_result(int size, int type);
/* end declare functions */


int main()
{       
    init_data(data, MAXELEMS);  
    
    printf("Delay  (ns)\n");    
    run_delay_testing();
    printf("\n\n");
    
    exit(0);
}

void run_delay_testing(){   
    int size;        /* Working set size (in bytes) */
    int stride;      /* Stride (in array elements) */
    
    /*Print the size header */
    printf("\t");
    for (size = MAXBYTES; size >= MINBYTES; size >>= 1) {
        if (size > (1 << 20)){
            printf("%dm\t", size / (1 << 20));
        }else{
            printf("%dk\t", size / 1024);
        }
    }
    printf("\n");   

    /* estmate the band width sequencely */
    for (stride = 1; stride <= MAXSTRIDE; stride=stride+1) {
        printf("s%d\t", stride);        
        for (size = MAXBYTES; size >= MINBYTES; size >>= 1) {   
            printf("%.2f\t", get_seque_access_result(size, stride, 1));
        }
        printf("\n");
    }
    
    /*  estmate the band width real randomly*/
    printf("\random\t");
    for (size = MAXBYTES; size >= MINBYTES; size >>= 1) {       
        printf("%.2f\t", get_random_access_result(size,1));
    }
    printf("\n");
}

/* $end mountainmain */

/* init_data - initializes the array */
void init_data(double *data, int n)
{
    int i;

    for (i = 0; i < n; i++)
    data[i] = i;
}

void create_rand_array(int max, int count, int* pArr)
{
    int i;
    for (i = 0; i < count; i ++,pArr++) {
        int rd = rand();        
        int randRet = (long int)rd * max / RAND_MAX;
        *pArr = randRet;
    }
    return;
}



/*
 * seque_access 
 *      - access global data array step by step 
 */
void seque_access(int elems, int stride) /* The test function */
{
    int i;
    double result = 0.0; 
    volatile double sink; 

    for (i = 0; i < elems; i += stride) {
    result += data[i];  
    }
    sink = result; /* So compiler doesn't optimize away the loop */
}


/*
 * random_access 
 *      - access global by random way indexed by random_index_arr 
 */
void random_access(int* random_index_arr, int count) /* The test function */
{ 
    int i;
    double result = 0.0; 
    volatile double sink; 

    for (i = 0; i < count; i++) {
        result += data[*(random_index_arr+i)];  
    }
    sink = result; /* So compiler doesn't optimize away the loop */
}


/*
 * get_random_access_result : test the computer storage(register,cache or memory) band width. 
 *      - size: data size will be tested
 *      - stride: steps
 *      - type: 0 get the band width, 
 *      -       1 get the consumed cpu cycles per operation.
 */
double get_seque_access_result(int size, int stride, int type)
{   
    int i;  
    long int operations;
    long int total_accessed_bytes;
    long int used_microseconds;
    
    int samples = 1000;     
    int elems = size / sizeof(double); 
            
    /* run the test*/
    start_timer();
    for(i=0; i<samples; i++){
        seque_access(elems, stride);
    }
    used_microseconds = get_timer();
    if(0==used_microseconds){
        return 0;
    }
    
    /* analysize result*/
    operations = (long int)samples * (long int)elems / stride;  
    total_accessed_bytes = operations * sizeof(double);

    
    double result = 0;
    if(0==type){ /* get width */
    
        /* width    = size(M)/ time(s) 
                    = (total_accessed_bytes / 1000000) / (used / 1000000000) 
                    = total_accessed_bytes*1000/used_microseconds;
        */  
        result = total_accessed_bytes * 1000  / used_microseconds;
        
    }else if(1==type){/* get cycles_per_operation */        
        result = (double)used_microseconds/operations; 
    }   
    
    return result;
}
    
/*
 * get_random_access_result : test the computer storage(register,cache or memory) band width. 
 *      - size: data size will be tested
 *      - type: 0 get the band width, 
 *      -       1 get the consumed cpu cycles per operation.
 */
double get_random_access_result(int size, int type)
{   
    int i;
    int *p;
    
    long int operations;
    long int total_accessed_bytes;
    long int used_microseconds;
    
    int samples = 300;      
    int elems = size / sizeof(double); 
    int access_count = elems;
    
    /* prepare for random access*/
    int* random_access_arr = malloc(access_count*sizeof(int));  
    for(i=0,p=random_access_arr; i<access_count; i++,p++){
        *p = 0;
    }   
    create_rand_array(elems, access_count, random_access_arr);  
    
        
    /* run the test*/
    start_timer();
    for(i=0; i<samples; i++){
        random_access(random_access_arr, access_count);
    }
    used_microseconds = get_timer();
    
    /* analysize result*/
    operations = (long int)samples * (long int)access_count;    
    total_accessed_bytes = operations * sizeof(double);

    
    double result = 0;;
    if(0==type){ /* get width */
    
        /* width    = size(M)/ time(s) 
                    = (total_accessed_bytes / 1000000) / (used / 1000000000) 
                    = total_accessed_bytes*1000/used_microseconds;
        */  
        result = total_accessed_bytes * 1000  / used_microseconds;
        
    }else if(1==type){/* get cycles_per_operation */        
        result = used_microseconds/operations*2.4; 
    }   
    
    return result;
}
/* $end mountainfuns */

相关文章

  • 内存延时测试

  • Storm、Spark、Hadoop比较

    1、Hadoop:磁盘级别;适合分布式处理;延时高,磁盘访问速度是内存延时的75000倍;MapReduce 并行...

  • 内存泄漏/管理

    ARC 下内存泄露的那些点performSelector延时调用导致的内存泄露iOS ARC下几种导致内存泄露的场...

  • 宏观把控

    对问题的一些探讨。 1.类比 延时----->电脑缓存:CPU与内存之间的临时存储器延时------>传输距离①t...

  • FLAnimatedImage源码剖析

    FLAnimatedImage iOS平台上播放GIF动画的一个优秀解决方案,支持可变帧间延时、内存内存表现良好、...

  • iOS延时执行测试

    在iOS程序中运行结果的原理 performSelector:@selector(logb)知识点:告诉线程直接调...

  • 客户端专项一些相关测试随笔

    内存泄漏、性能测试、稳定性测试 一、内存泄漏 内存泄漏的原因:https://blog.csdn.net/hizh...

  • 御Pro2首飞

    今天御Pro2首飞,主要测试新的延时功能,有自由、轨迹、定向、环绕延时四种。在延时过程中还可以调整飞行姿态、俯仰角...

  • 移动端测试经验-专项测试

    专项测试测什么? 资源类性能测试 Ø CPU占用 Ø 内存占用/内存泄漏 Ø 低资源环境表现 Ø 弱网络测试 速度...

  • 内存计算

    内存计算对低延时处理是一个巨大的进步,内存计算主要是随着今年来内存越来越便宜造成的,我们可以把数据都放在内存中。

网友评论

      本文标题:内存延时测试

      本文链接:https://www.haomeiwen.com/subject/doozpctx.html