plan9port

fork of plan9port with libvec, libstr and libsdb
Log | Files | Refs | README | LICENSE

9ping.c (1559B)


      1 #include <u.h>
      2 #include <libc.h>
      3 
      4 typedef uvlong u64int;
      5 
      6 #define TWID64	((u64int)~(u64int)0)
      7 
      8 
      9 u64int
     10 unittoull(char *s)
     11 {
     12 	char *es;
     13 	u64int n;
     14 
     15 	if(s == nil)
     16 		return TWID64;
     17 	n = strtoul(s, &es, 0);
     18 	if(*es == 'k' || *es == 'K'){
     19 		n *= 1024;
     20 		es++;
     21 	}else if(*es == 'm' || *es == 'M'){
     22 		n *= 1024*1024;
     23 		es++;
     24 	}else if(*es == 'g' || *es == 'G'){
     25 		n *= 1024*1024*1024;
     26 		es++;
     27 	}
     28 	if(*es != '\0')
     29 		return TWID64;
     30 	return n;
     31 }
     32 
     33 void
     34 main(int argc, char *argv[])
     35 {
     36 	int fd, i;
     37 	int n = 1000, m;
     38 	int s = 1;
     39 	double *t, t0, t1;
     40 	uchar *buf;
     41 	double a, d, max, min;
     42 
     43 	m = OREAD;
     44 	ARGBEGIN{
     45 	case 'n':
     46 		n = atoi(ARGF());
     47 		break;
     48 	case 's':
     49 		s = unittoull(ARGF());
     50 		if(s < 1 || s > 1024*1024)
     51 			sysfatal("bad size");
     52 		break;
     53 	case 'r':
     54 		m = OREAD;
     55 		break;
     56 	case 'w':
     57 		m = OWRITE;
     58 		break;
     59 	}ARGEND
     60 
     61 	fd = 0;
     62 	if(argc == 1){
     63 		fd = open(argv[0], m);
     64 		if(fd < 0)
     65 			sysfatal("could not open file: %s: %r", argv[0]);
     66 	}
     67 
     68 	buf = malloc(s);
     69 	t = malloc(n*sizeof(double));
     70 
     71 	t0 = nsec();
     72 	for(i=0; i<n; i++){
     73 		if(m == OREAD){
     74 			if(pread(fd, buf, s, 0) < s)
     75 				sysfatal("bad read: %r");
     76 		}else{
     77 			if(pwrite(fd, buf, s, 0) < s)
     78 				sysfatal("bad write: %r");
     79 		}
     80 		t1 = nsec();
     81 		t[i] = (t1 - t0)*1e-3;
     82 		t0 = t1;
     83 	}
     84 
     85 	a = 0.;
     86 	d = 0.;
     87 	max = 0.;
     88 	min = 1e12;
     89 
     90 	for(i=0; i<n; i++){
     91 		a += t[i];
     92 		if(max < t[i])
     93 			max = t[i];
     94 		if(min > t[i])
     95 			min = t[i];
     96 	}
     97 
     98 	a /= n;
     99 
    100 	for(i=0; i<n; i++)
    101 		d += (a - t[i]) * (a - t[i]);
    102 	d /= n;
    103 	d = sqrt(d);
    104 
    105 	print("avg = %.0fµs min = %.0fµs max = %.0fµs dev = %.0fµs\n", a, min, max, d);
    106 
    107 	exits(0);
    108 }