#include "hashbench.h"

static int test_size[num_sizes] = {XSMALL,SMALL,MEDIUM,LARGE,XLARGE};

double cpusecs()
{
  struct rusage ru;
  getrusage(RUSAGE_SELF,&ru);
  return(ru.ru_utime.tv_sec + ((double)ru.ru_utime.tv_usec)/1000000);
}

char *fill_crap(int size, int seed) {

  int i = 0;

  char *crap_buf = malloc(size);
  if ( crap_buf == NULL ) {
    perror("hashbench");
    exit(1);
  }
  
  srand(seed);
  for ( i = 0 ; i < size ; i++ )
    /* keep to a subset of ascii characters */
    crap_buf[i] = (char) (1+(int) (94.0*rand()/(RAND_MAX+1.0))+32);

  return crap_buf;
}

int main(int argc, char **argv) {

  int i,j,k;
  char *buf;

  /* setup our tests */
  test_func tests[3];
  tests[0] = &test_mdfour;
  tests[1] = &test_tiger;
  tests[2] = &test_md5;

  /* for each algorithm */
  for ( i = 0 ; i < 3 ; i++ ) {
    /* for each size */
    for ( j = 0 ; j < num_sizes ; j++ ) {
      double start, end, total=0;
      /* do it 20 times */
      buf = fill_crap( test_size[j] , SEED );
      for ( k = 0 ; k < 100 ; k++ ) {
	start = cpusecs();
	(*tests[i])(buf, test_size[j]);
	end = cpusecs();
	total += (end - start);
      } /* end repeat */
      
      /* average out and print display */
      printf("Test %d (size %d) Total CPU time for %d runs %fs\n\t Average Time %fs \t\t  %.0f KB/s\n", i, test_size[j], k, total, total/k, test_size[j]/total/1024);
    } /* size */
    printf("\n");
  } /* alg */


}

 

