rpc_latency2.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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. pipe(&pipes[0]);
  21. pipe(&pipes[2]);
  22. pipe(&pipes[4]);
  23. for (i = 0; i < times; i++) {
  24. pids[i][0] = fork();
  25. if (pids[i][0] < 0) {
  26. printf("fork failed\n");
  27. return -1;
  28. }
  29. if (pids[i][0] == 0) {
  30. close(pipes[0]);
  31. close(pipes[1]);
  32. close(pipes[3]);
  33. close(pipes[4]);
  34. close(pipes[5]);
  35. char byte;
  36. for (int i = 0; i < NTRIES; i++) {
  37. pid_t pid;
  38. recv_rpc(&pid, &byte, 1);
  39. send_rpc(pid, &byte, 1);
  40. }
  41. read(pipes[2], &byte, 1);
  42. close(pipes[2]);
  43. exit(0);
  44. }
  45. pids[i][1] = fork();
  46. if (pids[i][1] < 0) {
  47. printf("fork failed\n");
  48. return -1;
  49. }
  50. if (pids[i][1] == 0) {
  51. close(pipes[1]);
  52. close(pipes[3]);
  53. close(pipes[4]);
  54. char byte;
  55. read(pipes[0], &byte, 1);
  56. struct timeval timevals[2];
  57. gettimeofday(&timevals[0], NULL);
  58. pid_t pid = pids[i][0];
  59. for (int i = 0; i < NTRIES; i++) {
  60. send_rpc(pid, &byte, 1);
  61. recv_rpc(NULL, &byte, 1);
  62. }
  63. gettimeofday(&timevals[1], NULL);
  64. close(pipes[0]);
  65. write(pipes[5], timevals, sizeof(struct timeval) * 2);
  66. close(pipes[5]);
  67. read(pipes[2], &byte, 1);
  68. close(pipes[2]);
  69. exit(0);
  70. }
  71. }
  72. close(pipes[0]);
  73. close(pipes[2]);
  74. close(pipes[5]);
  75. sleep(1);
  76. char bytes[times * 2];
  77. write(pipes[1], bytes, times);
  78. close(pipes[1]);
  79. unsigned long long start_time = 0;
  80. unsigned long long end_time = 0;
  81. struct timeval timevals[2];
  82. for (int i = 0; i < times; i++) {
  83. read(pipes[4], timevals, sizeof(struct timeval) * 2);
  84. unsigned long s = timevals[0].tv_sec * 1000000ULL + timevals[0].tv_usec;
  85. unsigned long e = timevals[1].tv_sec * 1000000ULL + timevals[1].tv_usec;
  86. if (!start_time || s < start_time)
  87. start_time = s;
  88. if (!end_time || e > end_time)
  89. end_time = e;
  90. }
  91. close(pipes[4]);
  92. write(pipes[3], bytes, times * 2);
  93. close(pipes[3]);
  94. for (i = 0; i < times; i++) {
  95. waitpid(pids[i][0], NULL, 0);
  96. waitpid(pids[i][1], NULL, 0);
  97. }
  98. printf("throughput for %d processes to send %d message: %lf bytes/second\n", times, NTRIES,
  99. 1.0 * NTRIES * 2 * times * 1000000 / (end_time - start_time));
  100. return 0;
  101. }