int test_destroy_erases(fake_www *www){ int i; size_t sz; struct MemoryStruct chunk; char* data; fake_www_get(www, www->sites[i].url, &chunk); /*Works once */ gtcache_init(FAKE_WWW_MIN_CACHE_LEN * 8, FAKE_WWW_MIN_CACHE_LEN, 1); gtcache_set(www->sites[0].url, chunk.memory, chunk.size + 1); gtcache_destroy(); free(chunk.memory); /*Should be empty now*/ gtcache_init(FAKE_WWW_MIN_CACHE_LEN, 1, FAKE_WWW_MIN_CACHE_LEN); data = gtcache_get(www->sites[0].url, &sz); if( data == NULL) return 1; else{ free(data); return 0; } }
static int testGetMultiple( char * test, testdata_t * pt, int pres_cnt, int check_cnt ) { int cnt = 0; int i; void * p = NULL; int rtn = 1; size_t size; for(i=0; i < check_cnt; i++) { p = gtcache_get(pt[i].key, &size); if( p != NULL ) { cnt++; free(p); p = NULL; } } if( cnt != pres_cnt ) { fprintf(stderr, "%s: FAILED - %d of %d found, expected %d\n", test, cnt, check_cnt, pres_cnt); } else { fprintf(stderr, "%s: passed\n", test); rtn = 0; } if( p != NULL ) { free(p); p = NULL; } /* * exit on failures if necessary */ assert( (rtn == 0) || (! exitOnError) ); return(rtn); }
static int testGet( char * test, testdata_t * pt, bool should_be_present) { void * p = NULL; void * p2 = NULL; int rtn = 1; size_t size; p = gtcache_get(pt->key, &size); if( ! should_be_present ) { if( p != NULL ) { fprintf(stderr, "%s: FAILED - %s was found\n", test, pt->key); } else { fprintf(stderr, "%s: passed\n", test); rtn = 0; } } else { p2 = gtcache_get(pt->key, NULL); if( p == NULL ) { fprintf(stderr, "%s: FAILED - %s not found\n", test, pt->key); } else if( p2 == NULL ) { fprintf(stderr, "%s: FAILED - %s not found with null size\n", test, pt->key); } else if( p == p2 ) { fprintf(stderr, "%s: FAILED - not returning independent data\n", test); } else if( size != pt->size ) { fprintf(stderr, "%s: FAILED - size(%ld) not %ld\n", test, size, pt->size); } else if( memcmp(p, pt->data, size) != 0 ) { fprintf(stderr, "%s: FAILED - data doesn't match ('%s' vs '%s')\n", test, (char *)p, pt->data); } else { fprintf(stderr, "%s: passed\n", test); rtn = 0; } } if( p != NULL ) { free(p); p = NULL; } if( p2 != NULL ) { free(p2); p2 = NULL; } /* * exit on failures if necessary */ assert( (rtn == 0) || (! exitOnError) ); return(rtn); }
int main(int argc, char* argv[]){ struct MemoryStruct chunk; char url[FAKE_WWW_MAX_URL_LEN]; char *data, *res; int i, hits, misses, Npts, started; int min_cache_sz, max_cache_sz, cache_sz; int num_requests, nlevels, urlcount; fake_www www; size_t sz; if(argc < 3){ fprintf(stderr,"Usage: cache_test [FAKE_WWW] [NUM_REQUESTS] \n"); exit(0); } srand(12314); fake_www_init(&www, argv[1], 0); num_requests = strtol(argv[2], NULL, 10); Npts = 20; /*Setting minimum and maximum cache sizes*/ min_cache_sz = 1 << 20; max_cache_sz = 1 << 24; nlevels = numlevels(&www); for(i = 0; i < Npts; i++){ /*Cache sizes go up on a logarithmic scale*/ cache_sz = (int) (min_cache_sz * pow( 1.0 * max_cache_sz / min_cache_sz, 1.0 * i / (Npts-1))); gtcache_init(cache_sz, FAKE_WWW_MIN_CACHE_LEN, nlevels); hits = 0; misses = 0; started = 0; urlcount = 0; rewind(stdin); while(hits + misses < num_requests){ res = fgets(url, FAKE_WWW_MAX_URL_LEN, stdin); if (res == NULL){ if(!feof(stdin)){ /*We should always be able to read from the file*/ fprintf(stderr, "Error parsing the list of requests\n"); exit(EXIT_FAILURE); } else{ /*Rewind to the beginning if we've reached the end*/ started = 1; rewind(stdin); if( urlcount == 0 ){ fprintf(stderr, "No valid requests in the input file.\n"); exit(EXIT_FAILURE); } urlcount = 0; continue; } } /*Treat lines starting in # as comments*/ if (url[0] == '#') continue; /*Stripping url of any trailing whitespace*/ rstrip(url); if(strlen(url) == 0) continue; urlcount++; data = gtcache_get(url, &sz); if(data != NULL){ /*Cache hit!*/ hits+= started; free(data); } else{ /*Cache miss!*/ misses+= started; fake_www_get(&www, url, &chunk); /*Accounting for cold start. Only once the cache_size is reached do we start counting*/ if( gtcache_memused() + chunk.size + 1> cache_sz) started = 1; gtcache_set(url, chunk.memory, chunk.size + 1); free(chunk.memory); } } gtcache_destroy(); printf("%d\t%f\n", cache_sz, 1.0*hits/(hits + misses)); } return 0; }