/*               Main program for RPC Performance Test
		 -------------------------------------
*/
#include <stdio.h>
#include <time.h>
#ifdef vms
#include <types.h>
#include <timeb.h>
#else
#ifdef OSK
#include <types.h>
#include <timeb.h>
#else
#include <sys/types.h>
#include <sys/timeb.h>
#endif
#endif
#include <math.h>      
                       
#include <rpcrts.h>
#include "time.ext"

char		name[100],host_name[100];

typedef rpc_byte my_array[10000];


int main (argc,argv)
int argc;
char *argv[];
{

  int passes,data,steps;

  if (argc<6) 
    usage();
  else {
    passes = atoi(argv[1]);
    if (passes<1 || passes>10000) {
        printf("Passes out of range: [1..10000]\n");
	exit(1);
    }
    data = atoi(argv[2]);
    if (data<1 || data>10000) {
       printf("Data out of range: [1..10000]\n");
       exit(1);
    }
    steps = atoi(argv[3]);
    if (steps<1 || steps>100) {
      printf("Steps out of range: [1..100]\n");
      exit(1);
    }
    if (argc>=4)
      strcpy(name,argv[4]);
      /*  remote host */                                       
    if (argc>=5)
      strcpy(host_name,argv[5]);                   
     /* local host */
    if (argc>6) {                                  
      printf("too many parameters\n");             
      exit(1);                                     
    }
  }

 
   {    extern char rpc_trace;   /* Trace flag */
/*
	rpc_init();
	printf("rpc init has been done\n");
*/
	open_timesub();
	printf("open_timesub has been done\n");

	user_client(passes,data,steps);
   }

}






int user_client(passes,data,steps)
int passes,data,steps;
{


/*  Time & statistic variables    */

  int            time_pass[10000];
  int            count_pass,count_rate;
  struct timeb   *time_pointer_init, *time_pointer_end;

  FILE           *fd;
  my_array       ary;
  int		 rate;
  char           *name_file="PERFORMANCE.LOG";
  char           *time_now;
  rate = data/steps;

  time_now = (char *) malloc(100);
  time_pointer_init = (struct timeb *) malloc(sizeof(struct timeb));
  time_pointer_end  = (struct timeb *) malloc(sizeof(struct timeb));

  if ((fd=fopen(name_file,"a"))==NULL) {
    fprintf(stderr,"Can't open file %s\n", name_file);
    exit(1);
  }  

/* Read the current time  */            
  ftime (time_pointer_init);                 
  time_now = ctime(&time_pointer_init->time);

  fprintf(fd,"\n\n\nPERFORMANCE TEST (OC-RPC) FROM %s TO %s AT %s\n",host_name,name,time_now);
  fprintf(fd,"Passes: %4d   Data: %4d   Rate: %d   Steps: %d\n\n",
		passes,data,rate,steps);
  printf("\n\n\nPERFORMANCE TEST (OC-RPC) FROM %s TO %s AT %s\n",host_name,name,time_now);
  printf("Passes: %4d  -  Data: %4d  -  Rate: %d  -  Steps: %d\n\n",
		passes,data,rate,steps);
 

  for (count_pass=0; count_pass<data; count_pass++) ary[count_pass] = ' ';

  for (count_rate=0; count_rate<=data; count_rate+=rate)  {

    for (count_pass=0; count_pass<passes; count_pass+=1 )  {

      ftime (time_pointer_init);

      test (ary,count_rate);  
      test (ary,count_rate);  
      test (ary,count_rate);  
      test (ary,count_rate);  
      test (ary,count_rate);  
      test (ary,count_rate);  
      test (ary,count_rate);  
      test (ary,count_rate);  
      test (ary,count_rate);  
      test (ary,count_rate);  
 
      ftime (time_pointer_end);

      time_pass[count_pass] = (time_pointer_end->time*1000
        	              + time_pointer_end->millitm 
			      - time_pointer_init->time*1000 
			      - time_pointer_init->millitm) / 10;
  
      }

    statistic_pass (fd,time_pass,passes,count_rate);

  }

  fclose(fd);

}





statistic_pass(fd,time_pass,passes,rate)
FILE *fd;
int time_pass[];
int passes,rate;
{
  int     count_pass,min=0,max=0;
  float   means = 0,meansqr=0;

  for (count_pass=0; count_pass<passes; count_pass+=1) {
     if (min > time_pass[count_pass] || min==0) min = time_pass[count_pass];   
     if (max < time_pass[count_pass]) max = time_pass[count_pass];
     means = means + time_pass[count_pass];   
     meansqr = meansqr + time_pass[count_pass]*time_pass[count_pass];
  }

  means = means/passes;
  meansqr = meansqr/passes;
  meansqr = sqrt(meansqr-means*means);

  fprintf(fd,"Performance at %dbytes\n",rate);
  fprintf(fd,"Min       :%6dms    \nMax       :%6dms\n",min,max);
  fprintf(fd,"Mean      :%6.0fms    \nDeviation :%6.0fms\n",means,meansqr);
  fprintf(fd,"\n\n");

  printf("Performance at %dbytes\n",rate);
  printf("Min       :%6dms    \nMax       :%6dms\n",min,max);
  printf("Mean      :%6.0fms    \nDeviation :%6.0fms\n",means,meansqr);
  printf("\n\n");

}




usage()                                                                         
{                                                                               
                                                                                
 printf("Usage: big_client <passes> <data> <steps> <remote_host> <local host>");
 exit(1);                                                                     
                                                                                
}