コード例 #1
0
ファイル: cache_test02.c プロジェクト: pranith/cs6210-prj3
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;
  }
}
コード例 #2
0
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;
}