Debian Codegraph @

Xpjproject/pjlib/src/pjlib-test/sleep.c

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