Files
open-source-search-engine/memtest.cpp

65 lines
1.7 KiB
C++
Raw Normal View History

2013-08-02 13:12:24 -07:00
//#include "gb-include.h"
#include <malloc.h>
#include <sys/time.h>
#include <stdio.h>
#include <stdlib.h>
int main ( ) {
2014-11-10 14:45:11 -08:00
int32_t bufSize = 100*1024*1024;
2013-08-02 13:12:24 -07:00
// test random memory accesses
char *buf = (char *)malloc(bufSize);
if ( ! buf ) {
fprintf(stderr,"could not malloc 100MB.\n");
exit(-1);
}
2014-11-10 14:45:11 -08:00
int32_t np = 400000;
2013-08-02 13:12:24 -07:00
char *ptrs[np];
2014-11-10 14:45:11 -08:00
for ( int32_t i = 0 ; i < np ; i++ )
2013-08-02 13:12:24 -07:00
ptrs[i] = buf + (rand() % (bufSize-4));
// access it
struct timeval tv;
gettimeofday ( &tv , NULL );
2014-10-30 13:36:39 -06:00
int64_t now=(int64_t)(tv.tv_usec/1000)+((int64_t)tv.tv_sec)*1000;
2014-11-10 14:45:11 -08:00
char c; //int32_t c; // char c;
int32_t loops = 1;
for ( int32_t k = 0 ; k < loops ; k++ )
for ( int32_t i = 0 ; i < np ; i++ )
//c = *(int32_t *)ptrs[i];
2013-08-02 13:12:24 -07:00
c = *ptrs[i];
gettimeofday ( &tv , NULL );
2014-10-30 13:36:39 -06:00
int64_t now2=(int64_t)(tv.tv_usec/1000)+((int64_t)tv.tv_sec)*1000;
2013-08-02 13:12:24 -07:00
2021-05-06 01:52:55 +10:00
fprintf(stderr,"did %"INT32" access in %"INT64" ms.\n",loops*np,now2-now);
fprintf(stderr,"did %"INT64" access per second.\n",
2014-10-30 13:36:39 -06:00
(1000LL*(((int64_t)loops)*((int64_t)np)))/(now2-now));
2013-08-02 13:12:24 -07:00
return 0;
loop:
2014-11-10 14:45:11 -08:00
int32_t sizes[10000];
int32_t end = 0;
2013-08-02 13:12:24 -07:00
2014-11-10 14:45:11 -08:00
for ( int32_t i = 0 ; i < 10000 ; i++ ) {
2013-08-02 13:12:24 -07:00
sizes[i] = rand() % 1000;
2014-11-10 14:45:11 -08:00
//fprintf(stderr,"i=%"INT32"\n",i);
2013-08-02 13:12:24 -07:00
//ptrs[i] = (char *)mmalloc ( sizes[i] ,NULL );
ptrs[i] = (char *)malloc ( sizes[i] );
if ( ! ptrs[i] ) {
2014-11-10 14:45:11 -08:00
fprintf(stderr,"failed on #%"INT32"",i);
2013-08-02 13:12:24 -07:00
goto freeThem;
}
end = i;
2014-11-10 14:45:11 -08:00
//fprintf(stderr,"ptr=%"INT32"\n",(int32_t)ptrs[i]);
2013-08-02 13:12:24 -07:00
}
freeThem:
// now free all
2014-11-10 14:45:11 -08:00
for ( int32_t i = 0 ; i < end ; i++ ) {
// for ( int32_t i = 99 ; i >= 0 ; i-- ) {
//fprintf(stderr,"fi=%"INT32"\n",i);
2013-08-02 13:12:24 -07:00
//mfree ( ptrs[i] , sizes[i] , NULL );
free ( ptrs[i] );
}
//goto loop;
}