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; } }
int main(int argc, char **argv) { int cnt = 0; int i; int j; int opt; extern int optind; extern char * optarg; testdata_t td3; while( (opt=getopt(argc,argv, "hkv")) != -1 ) { switch(opt) { case 'v': /* number of threads */ VERBOSE_INC(); break; case 'k': exitOnError = true; break; default: fprintf(stderr, "Unknown options: %s\n", optarg); /* fall through */ case 'h': fprintf(stderr, "Usage: barrier_test [-n #]\n"); fprintf(stderr, " -h - this help message\n"); fprintf(stderr, " -k - kill test on first failure\n"); fprintf(stderr, " -v - increase verbosity output(multiple ok)\n"); exit(10); break; } } /* * fill the smallbuffs */ for(i=0; i < SMALLBUF_CNT; i++) { for(j=0; j < SMALLBUF_LEN; j++) { smallbuf[i][j] = '0' + j; } } /* * give us room for 3 entries */ gtcache_init(1024*3, 1024, 0); /* * Test1: save/get of single item works */ gtcache_set(td[0].key, td[0].data, td[0].size); cnt += testGet("Test 1a: save/get of single item", &td[0], true); cnt += testGet("Test 1b: other item not found", &td[1], false); /* * Test2: add a 2nd item and see it is still there */ gtcache_set(td[1].key, td[1].data, td[1].size); cnt += testGet("Test 2a: save/get of another item", &td[1], true); cnt += testGet("Test 2b: can still get the 1st item", &td[0], true); /* * Test3: add a 3rd item and see if they are all still there */ gtcache_set(td[2].key, td[2].data, td[2].size); cnt += testGet("Test 3a: save/get of another item", &td[2], true); cnt += testGet("Test 3b: can still get the 2nd item", &td[1], true); cnt += testGet("Test 3c: can still get the 1st item", &td[0], true); /* * Test4: add a 4th item and now 4th, 2nd and 1st should be there */ gtcache_set(td[3].key, td[3].data, td[3].size); cnt += testGet("Test 4a: save/get of another item", &td[3], true); cnt += testGet("Test 4b: 3rd item is no longer in cache", &td[2], false); cnt += testGet("Test 4c: can still get the 2nd item", &td[1], true); cnt += testGet("Test 4d: can still get the 1st item", &td[0], true); /* * cleanup so we can start again */ gtcache_destroy(); /* * give us room for 4 entries */ gtcache_init(1024*4, 1024, 0); /* * Test5: save/get of single item works */ cnt += testGet("Test 5a: get w/o put gets null", &td[0], false); /* * test 6 - repeating test1 on new cache */ gtcache_set(td[0].key, td[0].data, td[0].size); cnt += testGet("Test 6a: save/get of single item", &td[0], true); cnt += testGet("Test 6b: other item not found", &td[1], false); /* * Test7: add a 2nd item and see it is still there */ gtcache_set(td[1].key, td[1].data, td[1].size); cnt += testGet("Test 7a: save/get of another item", &td[1], true); cnt += testGet("Test 7b: can still get the 1st item", &td[0], true); /* * Test8: add a 3rd item and see if they are all still there */ gtcache_set(td[2].key, td[2].data, td[2].size); cnt += testGet("Test 8a: save/get of another item", &td[2], true); cnt += testGet("Test 8b: can still get the 2nd item", &td[1], true); cnt += testGet("Test 8c: can still get the 1st item", &td[0], true); /* * Test9: add a 4th item and all 4 should be there */ gtcache_set(td[3].key, td[3].data, td[3].size); cnt += testGet("Test 9a: save/get of another item", &td[3], true); cnt += testGet("Test 9b: can still get the 3rd item", &td[2], true); cnt += testGet("Test 9c: can still get the 2nd item", &td[1], true); cnt += testGet("Test 9d: can still get the 1st item", &td[0], true); /* * Test10: add a 5th item and first 3 + 5th should be there */ gtcache_set(td[4].key, td[4].data, td[4].size); cnt += testGet("Test 10a: save/get of another item", &td[4], true); cnt += testGetMultiple("Test 10b: 3 of the others are still here", &td[0], 3, 4); /* * Test11: add a 6th item and first 3 + 6th should be there */ gtcache_set(td[5].key, td[5].data, td[5].size); cnt += testGet("Test 11a: save/get of another item", &td[5], true); cnt += testGetMultiple("Test 11b: 3 of the others are still here", &td[0], 3, 5); /* * Test12: add a 7th item and first 3 + 6th should be there */ gtcache_set(td[6].key, td[6].data, td[6].size); cnt += testGet("Test 12a: save/get of another item", &td[6], true); cnt += testGetMultiple("Test 12b: 3 of the others are still here", &td[0], 3, 6); /* * Test13: add the 7th item again and make sure things are good */ gtcache_set(td[6].key, td[6].data, td[6].size); cnt += testGet("Test 13a: save/get of same item again", &td[6], true); cnt += testGetMultiple("Test 13b: 3 of the others are still here", &td[0], 3, 6); /* * Test14: Adding very large item fails */ gtcache_set(td[7].key, largebuf, sizeof(largebuf)); cnt += testGet("Test 14a: can't save beyond capacity", &td[7], false); cnt += testGet("Test 14b: can still get the 7th item", &td[6], true); cnt += testGetMultiple("Test 14c: 3 of the others are still here", &td[0], 3, 6); /* * cleanup so we can start again */ gtcache_destroy(); /* * give us room for 4 entries */ gtcache_init(1024*4, 1024, 0); /* * Test15: add 4 1k+1 entries results in 3 in the list */ gtcache_set(td2[0].key, td2[0].data, td2[0].size); cnt += testGet("Test 15a: 1st 1K buffer works", &td2[0], true); gtcache_set(td2[1].key, td2[1].data, td2[1].size); cnt += testGet("Test 15b: 2nd 1K buffer works", &td2[1], true); gtcache_set(td2[2].key, td2[2].data, td2[2].size); cnt += testGet("Test 15c: 3rd 1K buffer works", &td2[2], true); gtcache_set(td2[3].key, td2[3].data, td2[3].size); cnt += testGet("Test 15d: 4th 1K buffer works", &td2[3], true); cnt += testGetMultiple("Test 15e: 2 of the others are still here", &td2[0], 2, 3); /* * Test16: add an item that takes exactly the full capacity */ td3.key = "http://FullBufferTest.com"; td3.data = largebuf; td3.size = 1024*4; gtcache_set(td3.key, td3.data, td3.size); cnt += testGet("Test 16a: 4K buffer fits", &td3, true); cnt += testGet("Test 16b: 1st 1K buffer gone", &td2[0], false); cnt += testGet("Test 16c: 2nd 1K buffer gone", &td2[1], false); cnt += testGet("Test 16d: 3rd 1K buffer gone", &td2[2], false); cnt += testGet("Test 16e: 4th item gone", &td[4], false); gtcache_set(td[1].key, td[1].data, td[1].size); cnt += testGet("Test 16f: stored 1st item", &td[1], true); cnt += testGet("Test 16g: 4K buffer gone", &td3, false); gtcache_set(td[2].key, td[2].data, td[2].size); cnt += testGet("Test 16h: stored 2nd item", &td[2], true); gtcache_set(td[3].key, td[3].data, td[3].size); cnt += testGet("Test 16d: stored 3rd item", &td[3], true); gtcache_set(td3.key, td3.data, td3.size); cnt += testGet("Test 16i: 4K buffer fits again", &td3, true); cnt += testGet("Test 16j: 1st item gone", &td[1], false); cnt += testGet("Test 16k: 2nd item gone", &td[2], false); cnt += testGet("Test 16l: 3rd item gone", &td[3], false); if( cnt == 0 ) { fprintf(stderr, "All tests passed\n"); } else { fprintf(stderr, "FAILED: %d tests failed\n", cnt); } return(cnt); }
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; }