rpc_latency.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. #include <shim_unistd.h>
  2. #include <signal.h>
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <sys/time.h>
  6. #include <sys/wait.h>
  7. #include <unistd.h>
  8. #define NTRIES 10000
  9. #define TEST_TIMES 32
  10. int main(int argc, char** argv) {
  11. int times = TEST_TIMES;
  12. int pipes[6];
  13. int pids[TEST_TIMES][2];
  14. int i = 0;
  15. if (argc >= 2) {
  16. times = atoi(argv[1]) / 2;
  17. if (times > TEST_TIMES)
  18. return 1;
  19. }
  20. if (pipe(&pipes[0]) < 0 || pipe(&pipes[2]) < 0 || pipe(&pipes[4]) < 0) {
  21. perror("pipe error");
  22. return 1;
  23. }
  24. for (i = 0; i < times; i++) {
  25. pids[i][0] = fork();
  26. if (pids[i][0] < 0) {
  27. printf("fork failed\n");
  28. return 1;
  29. }
  30. if (pids[i][0] == 0) {
  31. close(pipes[0]);
  32. close(pipes[1]);
  33. close(pipes[3]);
  34. close(pipes[4]);
  35. close(pipes[5]);
  36. char byte;
  37. if (read(pipes[2], &byte, 1) != 1) {
  38. perror("read error");
  39. return 1;
  40. }
  41. close(pipes[2]);
  42. exit(0);
  43. }
  44. pids[i][1] = fork();
  45. if (pids[i][1] < 0) {
  46. printf("fork failed\n");
  47. return 1;
  48. }
  49. if (pids[i][1] == 0) {
  50. close(pipes[1]);
  51. close(pipes[3]);
  52. close(pipes[4]);
  53. char byte;
  54. if (read(pipes[0], &byte, 1) != 1) {
  55. perror("read error");
  56. return 1;
  57. }
  58. struct timeval timevals[2];
  59. gettimeofday(&timevals[0], NULL);
  60. benchmark_rpc(pids[i][0], NTRIES, &byte, 1);
  61. gettimeofday(&timevals[1], NULL);
  62. close(pipes[0]);
  63. if (write(pipes[5], timevals, sizeof(struct timeval) * 2)
  64. != sizeof(struct timeval) * 2) {
  65. perror("write error");
  66. return 1;
  67. }
  68. close(pipes[5]);
  69. if (read(pipes[2], &byte, 1) != 1) {
  70. perror("read error");
  71. return 1;
  72. }
  73. close(pipes[2]);
  74. exit(0);
  75. }
  76. }
  77. close(pipes[0]);
  78. close(pipes[2]);
  79. close(pipes[5]);
  80. sleep(1);
  81. char bytes[times * 2];
  82. if (write(pipes[1], bytes, times) != times) {
  83. perror("write error");
  84. return 1;
  85. }
  86. close(pipes[1]);
  87. unsigned long long start_time = 0;
  88. unsigned long long end_time = 0;
  89. struct timeval timevals[2];
  90. for (int i = 0; i < times; i++) {
  91. if (read(pipes[4], timevals, sizeof(struct timeval) * 2) != sizeof(struct timeval) * 2) {
  92. perror("read error");
  93. return 1;
  94. }
  95. unsigned long s = timevals[0].tv_sec * 1000000ULL + timevals[0].tv_usec;
  96. unsigned long e = timevals[1].tv_sec * 1000000ULL + timevals[1].tv_usec;
  97. if (!start_time || s < start_time)
  98. start_time = s;
  99. if (!end_time || e > end_time)
  100. end_time = e;
  101. }
  102. close(pipes[4]);
  103. if (write(pipes[3], bytes, times * 2) != times * 2) {
  104. perror("write error");
  105. return 1;
  106. }
  107. close(pipes[3]);
  108. for (i = 0; i < times; i++) {
  109. waitpid(pids[i][0], NULL, 0);
  110. waitpid(pids[i][1], NULL, 0);
  111. }
  112. printf("throughput for %d processes to send %d message: %lf bytes/second\n", times, NTRIES,
  113. 1.0 * NTRIES * 2 * times * 1000000 / (end_time - start_time));
  114. return 0;
  115. }