1
/* $Id: sleep.c 3553 2011-05-05 06:14:19Z nanang $ */
3
* Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com)
4
* Copyright (C) 2003-2008 Benny Prijono <benny@prijono.org>
6
* This program is free software; you can redistribute it and/or modify
7
* it under the terms of the GNU General Public License as published by
8
* the Free Software Foundation; either version 2 of the License, or
9
* (at your option) any later version.
11
* This program is distributed in the hope that it will be useful,
12
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
* GNU General Public License for more details.
16
* You should have received a copy of the GNU General Public License
17
* along with this program; if not, write to the Free Software
18
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23
* \page page_pjlib_sleep_test Test: Sleep, Time, and Timestamp
25
* This file provides implementation of \b sleep_test().
27
* \section sleep_test_sec Scope of the Test
30
* - whether pj_thread_sleep() works.
31
* - whether pj_gettimeofday() works.
32
* - whether pj_get_timestamp() and friends works.
39
* - pj_get_timestamp()
40
* - pj_get_timestamp_freq() (implicitly)
45
* This file is <b>pjlib-test/sleep.c</b>
47
* \include pjlib-test/sleep.c
50
#if INCLUDE_SLEEP_TEST
54
#define THIS_FILE "sleep_test"
56
static int simple_sleep_test(void)
62
PJ_LOG(3,(THIS_FILE, "..will write messages every 1 second:"));
64
for (i=0; i<COUNT; ++i) {
68
rc = pj_thread_sleep(1000);
69
if (rc != PJ_SUCCESS) {
70
app_perror("...error: pj_thread_sleep()", rc);
74
rc = pj_gettimeofday(&tv);
75
if (rc != PJ_SUCCESS) {
76
app_perror("...error: pj_gettimeofday()", rc);
80
pj_time_decode(&tv, &pt);
83
"...%04d-%02d-%02d %02d:%02d:%02d.%03d",
84
pt.year, pt.mon, pt.day,
85
pt.hour, pt.min, pt.sec, pt.msec));
92
static int sleep_duration_test(void)
95
unsigned duration[] = { 2000, 1000, 500, 200, 100 };
99
PJ_LOG(3,(THIS_FILE, "..running sleep duration test"));
101
/* Test pj_thread_sleep() and pj_gettimeofday() */
102
for (i=0; i<PJ_ARRAY_SIZE(duration); ++i) {
103
pj_time_val start, stop;
106
/* Mark start of test. */
107
rc = pj_gettimeofday(&start);
108
if (rc != PJ_SUCCESS) {
109
app_perror("...error: pj_gettimeofday()", rc);
114
rc = pj_thread_sleep(duration[i]);
115
if (rc != PJ_SUCCESS) {
116
app_perror("...error: pj_thread_sleep()", rc);
120
/* Mark end of test. */
121
rc = pj_gettimeofday(&stop);
123
/* Calculate duration (store in stop). */
124
PJ_TIME_VAL_SUB(stop, start);
126
/* Convert to msec. */
127
msec = PJ_TIME_VAL_MSEC(stop);
129
/* Check if it's within range. */
130
if (msec < duration[i] * (100-MIS)/100 ||
131
msec > duration[i] * (100+MIS)/100)
134
"...error: slept for %d ms instead of %d ms "
135
"(outside %d%% err window)",
136
msec, duration[i], MIS));
142
/* Test pj_thread_sleep() and pj_get_timestamp() and friends */
143
for (i=0; i<PJ_ARRAY_SIZE(duration); ++i) {
145
pj_timestamp start, stop;
150
/* Mark start of test. */
151
rc = pj_get_timestamp(&start);
152
if (rc != PJ_SUCCESS) {
153
app_perror("...error: pj_get_timestamp()", rc);
157
/* ..also with gettimeofday() */
158
pj_gettimeofday(&t1);
161
rc = pj_thread_sleep(duration[i]);
162
if (rc != PJ_SUCCESS) {
163
app_perror("...error: pj_thread_sleep()", rc);
167
/* Mark end of test. */
168
pj_get_timestamp(&stop);
170
/* ..also with gettimeofday() */
171
pj_gettimeofday(&t2);
173
/* Compare t1 and t2. */
174
if (PJ_TIME_VAL_LT(t2, t1)) {
175
PJ_LOG(3,(THIS_FILE, "...error: t2 is less than t1!!"));
179
/* Get elapsed time in msec */
180
msec = pj_elapsed_msec(&start, &stop);
182
/* Check if it's within range. */
183
if (msec < duration[i] * (100-MIS)/100 ||
184
msec > duration[i] * (100+MIS)/100)
187
"...error: slept for %d ms instead of %d ms "
188
"(outside %d%% err window)",
189
msec, duration[i], MIS));
190
PJ_TIME_VAL_SUB(t2, t1);
192
"...info: gettimeofday() reported duration is "
194
PJ_TIME_VAL_MSEC(t2)));
208
rc = simple_sleep_test();
209
if (rc != PJ_SUCCESS)
212
rc = sleep_duration_test();
213
if (rc != PJ_SUCCESS)
220
/* To prevent warning about "translation unit is empty"
221
* when this test is disabled.
223
int dummy_sleep_test;
224
#endif /* INCLUDE_SLEEP_TEST */