sock_perf.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. /*
  2. * Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com)
  3. * Copyright (C) 2003-2008 Benny Prijono <benny@prijono.org>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. */
  19. #include "test.h"
  20. #include <pjlib.h>
  21. #include <pj/compat/high_precision.h>
  22. /**
  23. * \page page_pjlib_sock_perf_test Test: Socket Performance
  24. *
  25. * Test the performance of the socket communication. This will perform
  26. * simple producer-consumer type of test, where we calculate how long
  27. * does it take to send certain number of packets from producer to
  28. * consumer.
  29. *
  30. * This file is <b>pjlib-test/sock_perf.c</b>
  31. *
  32. * \include pjlib-test/sock_perf.c
  33. */
  34. #if INCLUDE_SOCK_PERF_TEST
  35. /*
  36. * sock_producer_consumer()
  37. *
  38. * Simple producer-consumer benchmarking. Send loop number of
  39. * buf_size size packets as fast as possible.
  40. */
  41. static int sock_producer_consumer(int sock_type,
  42. pj_size_t buf_size,
  43. unsigned loop,
  44. unsigned *p_bandwidth)
  45. {
  46. pj_sock_t consumer, producer;
  47. pj_pool_t *pool;
  48. char *outgoing_buffer, *incoming_buffer;
  49. pj_timestamp start, stop;
  50. unsigned i;
  51. pj_highprec_t elapsed, bandwidth;
  52. pj_highprec_t total_received;
  53. pj_status_t rc;
  54. /* Create pool. */
  55. pool = pj_pool_create(mem, NULL, 4096, 4096, NULL);
  56. if (!pool)
  57. return -10;
  58. /* Create producer-consumer pair. */
  59. rc = app_socketpair(pj_AF_INET(), sock_type, 0, &consumer, &producer);
  60. if (rc != PJ_SUCCESS) {
  61. app_perror("...error: create socket pair", rc);
  62. return -20;
  63. }
  64. /* Create buffers. */
  65. outgoing_buffer = (char*) pj_pool_alloc(pool, buf_size);
  66. incoming_buffer = (char*) pj_pool_alloc(pool, buf_size);
  67. /* Start loop. */
  68. pj_get_timestamp(&start);
  69. total_received = 0;
  70. for (i=0; i<loop; ++i) {
  71. pj_ssize_t sent, part_received, received;
  72. pj_time_val delay;
  73. sent = buf_size;
  74. rc = pj_sock_send(producer, outgoing_buffer, &sent, 0);
  75. if (rc != PJ_SUCCESS || sent != (pj_ssize_t)buf_size) {
  76. app_perror("...error: send()", rc);
  77. return -61;
  78. }
  79. /* Repeat recv() until all data is part_received.
  80. * This applies only for non-UDP of course, since for UDP
  81. * we would expect all data to be part_received in one packet.
  82. */
  83. received = 0;
  84. do {
  85. part_received = buf_size-received;
  86. rc = pj_sock_recv(consumer, incoming_buffer+received,
  87. &part_received, 0);
  88. if (rc != PJ_SUCCESS) {
  89. app_perror("...recv error", rc);
  90. return -70;
  91. }
  92. if (part_received <= 0) {
  93. PJ_LOG(3,("", "...error: socket has closed (part_received=%ld)!",
  94. part_received));
  95. return -73;
  96. }
  97. if ((pj_size_t)part_received != buf_size-received) {
  98. if (sock_type != pj_SOCK_STREAM()) {
  99. PJ_LOG(3,("", "...error: expecting %lu bytes, got %lu bytes",
  100. buf_size-received, part_received));
  101. return -76;
  102. }
  103. }
  104. received += part_received;
  105. } while ((pj_size_t)received < buf_size);
  106. total_received += received;
  107. /* Stop test if it's been runnign for more than 10 secs. */
  108. pj_get_timestamp(&stop);
  109. delay = pj_elapsed_time(&start, &stop);
  110. if (delay.sec > 10)
  111. break;
  112. }
  113. /* Stop timer. */
  114. pj_get_timestamp(&stop);
  115. elapsed = pj_elapsed_usec(&start, &stop);
  116. /* bandwidth = total_received * 1000 / elapsed */
  117. bandwidth = total_received;
  118. pj_highprec_mul(bandwidth, 1000);
  119. pj_highprec_div(bandwidth, elapsed);
  120. *p_bandwidth = (pj_uint32_t)bandwidth;
  121. /* Close sockets. */
  122. pj_sock_close(consumer);
  123. pj_sock_close(producer);
  124. /* Done */
  125. pj_pool_release(pool);
  126. return 0;
  127. }
  128. /*
  129. * sock_perf_test()
  130. *
  131. * Main test entry.
  132. */
  133. int sock_perf_test(void)
  134. {
  135. enum { LOOP = 64 * 1024 };
  136. int rc;
  137. unsigned bandwidth;
  138. PJ_LOG(3,("", "...benchmarking socket "
  139. "(2 sockets, packet=512, single threaded):"));
  140. /* Disable this test on Symbian since UDP connect()/send() failed
  141. * with S60 3rd edition (including MR2).
  142. * See https://github.com/pjsip/pjproject/issues/264
  143. */
  144. #if !defined(PJ_SYMBIAN) || PJ_SYMBIAN==0
  145. /* Benchmarking UDP */
  146. rc = sock_producer_consumer(pj_SOCK_DGRAM(), 512, LOOP, &bandwidth);
  147. if (rc != 0) return rc;
  148. PJ_LOG(3,("", "....bandwidth UDP = %d KB/s", bandwidth));
  149. #endif
  150. /* Benchmarking TCP */
  151. rc = sock_producer_consumer(pj_SOCK_STREAM(), 512, LOOP, &bandwidth);
  152. if (rc != 0) return rc;
  153. PJ_LOG(3,("", "....bandwidth TCP = %d KB/s", bandwidth));
  154. return rc;
  155. }
  156. #else
  157. /* To prevent warning about "translation unit is empty"
  158. * when this test is disabled.
  159. */
  160. int dummy_sock_perf_test;
  161. #endif /* INCLUDE_SOCK_PERF_TEST */