9ping.c 1.5 KB

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