sleep.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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. /**
  21. * \page page_pjlib_sleep_test Test: Sleep, Time, and Timestamp
  22. *
  23. * This file provides implementation of \b sleep_test().
  24. *
  25. * \section sleep_test_sec Scope of the Test
  26. *
  27. * This tests:
  28. * - whether pj_thread_sleep() works.
  29. * - whether pj_gettimeofday() works.
  30. * - whether pj_get_timestamp() and friends works.
  31. *
  32. * API tested:
  33. * - pj_thread_sleep()
  34. * - pj_gettimeofday()
  35. * - PJ_TIME_VAL_SUB()
  36. * - PJ_TIME_VAL_LTE()
  37. * - pj_get_timestamp()
  38. * - pj_get_timestamp_freq() (implicitly)
  39. * - pj_elapsed_time()
  40. * - pj_elapsed_usec()
  41. *
  42. *
  43. * This file is <b>pjlib-test/sleep.c</b>
  44. *
  45. * \include pjlib-test/sleep.c
  46. */
  47. #if INCLUDE_SLEEP_TEST
  48. #include <pjlib.h>
  49. #define THIS_FILE "sleep_test"
  50. extern pj_bool_t param_ci_mode;
  51. static int simple_sleep_test(void)
  52. {
  53. enum { COUNT = 10 };
  54. int i;
  55. pj_status_t rc;
  56. PJ_LOG(3,(THIS_FILE, "..will write messages every 1 second:"));
  57. for (i=0; i<COUNT; ++i) {
  58. pj_time_val tv;
  59. pj_parsed_time pt;
  60. rc = pj_thread_sleep(1000);
  61. if (rc != PJ_SUCCESS) {
  62. app_perror("...error: pj_thread_sleep()", rc);
  63. return -10;
  64. }
  65. rc = pj_gettimeofday(&tv);
  66. if (rc != PJ_SUCCESS) {
  67. app_perror("...error: pj_gettimeofday()", rc);
  68. return -11;
  69. }
  70. pj_time_decode(&tv, &pt);
  71. PJ_LOG(3,(THIS_FILE,
  72. "...%04d-%02d-%02d %02d:%02d:%02d.%03d",
  73. pt.year, pt.mon, pt.day,
  74. pt.hour, pt.min, pt.sec, pt.msec));
  75. }
  76. return 0;
  77. }
  78. static int sleep_duration_test(void)
  79. {
  80. const unsigned MAX_SLIP = param_ci_mode? 200 : 20;
  81. long duration[] = { 2000, 1000, 500, 200, 100 };
  82. unsigned i;
  83. unsigned avg_diff, max_diff;
  84. pj_status_t rc;
  85. PJ_LOG(3,(THIS_FILE, "..running sleep duration test"));
  86. /* Test pj_thread_sleep() and pj_gettimeofday() */
  87. for (i=0, avg_diff=0, max_diff=0; i<PJ_ARRAY_SIZE(duration); ++i) {
  88. pj_time_val start, stop;
  89. long msec;
  90. unsigned diff;
  91. /* Mark start of test. */
  92. rc = pj_gettimeofday(&start);
  93. if (rc != PJ_SUCCESS) {
  94. app_perror("...error: pj_gettimeofday()", rc);
  95. return -10;
  96. }
  97. /* Sleep */
  98. rc = pj_thread_sleep(duration[i]);
  99. if (rc != PJ_SUCCESS) {
  100. app_perror("...error: pj_thread_sleep()", rc);
  101. return -20;
  102. }
  103. /* Mark end of test. */
  104. rc = pj_gettimeofday(&stop);
  105. if (rc != PJ_SUCCESS) {
  106. app_perror("...error: pj_gettimeofday()", rc);
  107. return -22;
  108. }
  109. /* Calculate duration (store in stop). */
  110. PJ_TIME_VAL_SUB(stop, start);
  111. /* Convert to msec. */
  112. msec = PJ_TIME_VAL_MSEC(stop);
  113. /* Check if it's within range. */
  114. diff = PJ_ABS(msec - duration[i]);
  115. avg_diff = ((avg_diff * i) + diff) / (i+1);
  116. max_diff = diff>max_diff ? diff : max_diff;
  117. if (diff > MAX_SLIP) {
  118. PJ_LOG(3,(THIS_FILE,
  119. "...error: slept for %ld ms instead of %ld ms "
  120. "(outside %d msec tolerance)",
  121. msec, duration[i], MAX_SLIP));
  122. }
  123. }
  124. PJ_LOG(3,(THIS_FILE, "...avg/max slippage: %d/%d ms",
  125. avg_diff, max_diff));
  126. if (max_diff > MAX_SLIP)
  127. return -30;
  128. /* Test pj_thread_sleep() and pj_get_timestamp() and friends */
  129. for (i=0, avg_diff=0, max_diff=0; i<PJ_ARRAY_SIZE(duration); ++i) {
  130. pj_time_val t1, t2;
  131. pj_timestamp start, stop;
  132. long msec;
  133. unsigned diff;
  134. pj_thread_sleep(0);
  135. /* Mark start of test. */
  136. rc = pj_get_timestamp(&start);
  137. if (rc != PJ_SUCCESS) {
  138. app_perror("...error: pj_get_timestamp()", rc);
  139. return -60;
  140. }
  141. /* ..also with gettimeofday() */
  142. pj_gettimeofday(&t1);
  143. /* Sleep */
  144. rc = pj_thread_sleep(duration[i]);
  145. if (rc != PJ_SUCCESS) {
  146. app_perror("...error: pj_thread_sleep()", rc);
  147. return -70;
  148. }
  149. /* Mark end of test. */
  150. pj_get_timestamp(&stop);
  151. /* ..also with gettimeofday() */
  152. pj_gettimeofday(&t2);
  153. /* Compare t1 and t2. */
  154. if (PJ_TIME_VAL_LT(t2, t1)) {
  155. PJ_LOG(3,(THIS_FILE, "...error: t2 is less than t1!!"));
  156. return -75;
  157. }
  158. /* Get elapsed time in msec */
  159. msec = pj_elapsed_msec(&start, &stop);
  160. /* Check if it's within range. */
  161. diff = PJ_ABS(msec - duration[i]);
  162. avg_diff = ((avg_diff * i) + diff) / (i+1);
  163. max_diff = diff>max_diff ? diff : max_diff;
  164. if (diff > MAX_SLIP) {
  165. PJ_LOG(3,(THIS_FILE,
  166. "...error: slept for %ld ms instead of %ld ms "
  167. "(outside %d msec tolerance)",
  168. msec, duration[i], MAX_SLIP));
  169. PJ_TIME_VAL_SUB(t2, t1);
  170. PJ_LOG(3,(THIS_FILE,
  171. "...info: gettimeofday() reported duration is "
  172. "%ld msec",
  173. PJ_TIME_VAL_MSEC(t2)));
  174. }
  175. }
  176. PJ_LOG(3,(THIS_FILE, "...avg/max slippage: %d/%d ms",
  177. avg_diff, max_diff));
  178. if (max_diff > MAX_SLIP)
  179. return -76;
  180. /* All done. */
  181. return 0;
  182. }
  183. int sleep_test()
  184. {
  185. int rc;
  186. rc = simple_sleep_test();
  187. if (rc != PJ_SUCCESS)
  188. return rc;
  189. rc = sleep_duration_test();
  190. if (rc != PJ_SUCCESS)
  191. return rc;
  192. return 0;
  193. }
  194. #else
  195. /* To prevent warning about "translation unit is empty"
  196. * when this test is disabled.
  197. */
  198. int dummy_sleep_test;
  199. #endif /* INCLUDE_SLEEP_TEST */