1
/* $Id: timer.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_timer_test Test: Timer
25
* This file provides implementation of \b timer_test(). It tests the
26
* functionality of the timer heap.
29
* This file is <b>pjlib-test/timer.c</b>
31
* \include pjlib-test/timer.c
35
#if INCLUDE_TIMER_TEST
41
#define MAX_COUNT (LOOP * MIN_COUNT)
43
#define D (MAX_COUNT / 32000)
44
#define DELAY (D < MIN_DELAY ? MIN_DELAY : D)
45
#define THIS_FILE "timer_test"
48
static void timer_callback(pj_timer_heap_t *ht, pj_timer_entry *e)
54
static int test_timer_heap(void)
57
pj_timer_entry *entry;
59
pj_timer_heap_t *timer;
61
pj_status_t rc; int err=0;
64
size = pj_timer_heap_mem_size(MAX_COUNT)+MAX_COUNT*sizeof(pj_timer_entry);
65
pool = pj_pool_create( mem, NULL, size, 4000, NULL);
67
PJ_LOG(3,("test", "...error: unable to create pool of %u bytes",
72
entry = (pj_timer_entry*)pj_pool_calloc(pool, MAX_COUNT, sizeof(*entry));
76
for (i=0; i<MAX_COUNT; ++i) {
77
entry[i].cb = &timer_callback;
79
rc = pj_timer_heap_create(pool, MAX_COUNT, &timer);
80
if (rc != PJ_SUCCESS) {
81
app_perror("...error: unable to create timer heap", rc);
86
for (i=0; i<LOOP; ++i) {
91
pj_timestamp t1, t2, t_sched, t_cancel, t_poll;
92
pj_time_val now, expire;
94
pj_gettimeofday(&now);
96
t_sched.u32.lo = t_cancel.u32.lo = t_poll.u32.lo = 0;
99
for (j=0; j<(int)count; ++j) {
100
delay.sec = pj_rand() % DELAY;
101
delay.msec = pj_rand() % 1000;
104
pj_get_timestamp(&t1);
105
rc = pj_timer_heap_schedule(timer, &entry[j], &delay);
108
pj_get_timestamp(&t2);
110
t_sched.u32.lo += (t2.u32.lo - t1.u32.lo);
113
pj_get_timestamp(&t1);
114
rc = pj_timer_heap_poll(timer, NULL);
115
pj_get_timestamp(&t2);
117
t_poll.u32.lo += (t2.u32.lo - t1.u32.lo);
122
// Set the time where all timers should finish
123
pj_gettimeofday(&expire);
126
PJ_TIME_VAL_ADD(expire, delay);
128
// Wait unfil all timers finish, cancel some of them.
130
int index = pj_rand() % count;
131
pj_get_timestamp(&t1);
132
rc = pj_timer_heap_cancel(timer, &entry[index]);
133
pj_get_timestamp(&t2);
136
t_cancel.u32.lo += (t2.u32.lo - t1.u32.lo);
139
pj_gettimeofday(&now);
141
pj_get_timestamp(&t1);
142
#if defined(PJ_SYMBIAN) && PJ_SYMBIAN!=0
143
/* On Symbian, we must use OS poll (Active Scheduler poll) since
144
* timer is implemented using Active Object.
147
while (pj_symbianos_poll(-1, 0))
150
rc = pj_timer_heap_poll(timer, NULL);
152
pj_get_timestamp(&t2);
155
t_poll.u32.lo += (t2.u32.lo - t1.u32.lo);
158
} while (PJ_TIME_VAL_LTE(now, expire)&&pj_timer_heap_count(timer) > 0);
160
if (pj_timer_heap_count(timer)) {
161
PJ_LOG(3, (THIS_FILE, "ERROR: %d timers left",
162
pj_timer_heap_count(timer)));
165
t_sched.u32.lo /= count;
166
t_cancel.u32.lo /= count;
167
t_poll.u32.lo /= count;
168
PJ_LOG(4, (THIS_FILE,
169
"...ok (count:%d, early:%d, cancelled:%d, "
170
"sched:%d, cancel:%d poll:%d)",
171
count, early, cancelled, t_sched.u32.lo, t_cancel.u32.lo,
175
if (count > MAX_COUNT)
179
pj_pool_release(pool);
186
return test_timer_heap();
190
/* To prevent warning about "translation unit is empty"
191
* when this test is disabled.
193
int dummy_timer_test;
194
#endif /* INCLUDE_TIMER_TEST */