blob: 9521aa9e84f6a2cc0ce657422d8c59f33985e8f8 [file] [log] [blame]
Alexandre Lision94f06ba2013-12-09 16:28:33 -05001/* $Id$ */
Tristan Matthews0a329cc2013-07-17 13:20:14 -04002/*
3 * Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com)
4 * Copyright (C) 2003-2008 Benny Prijono <benny@prijono.org>
5 *
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.
10 *
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.
15 *
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
19 */
20#include "test.h"
21
22/**
23 * \page page_pjlib_timer_test Test: Timer
24 *
25 * This file provides implementation of \b timer_test(). It tests the
26 * functionality of the timer heap.
27 *
28 *
29 * This file is <b>pjlib-test/timer.c</b>
30 *
31 * \include pjlib-test/timer.c
32 */
33
34
35#if INCLUDE_TIMER_TEST
36
37#include <pjlib.h>
38
39#define LOOP 16
40#define MIN_COUNT 250
41#define MAX_COUNT (LOOP * MIN_COUNT)
42#define MIN_DELAY 2
43#define D (MAX_COUNT / 32000)
44#define DELAY (D < MIN_DELAY ? MIN_DELAY : D)
45#define THIS_FILE "timer_test"
46
47
48static void timer_callback(pj_timer_heap_t *ht, pj_timer_entry *e)
49{
50 PJ_UNUSED_ARG(ht);
51 PJ_UNUSED_ARG(e);
52}
53
54static int test_timer_heap(void)
55{
56 int i, j;
57 pj_timer_entry *entry;
58 pj_pool_t *pool;
59 pj_timer_heap_t *timer;
60 pj_time_val delay;
61 pj_status_t rc; int err=0;
62 pj_size_t size;
63 unsigned count;
64
65 size = pj_timer_heap_mem_size(MAX_COUNT)+MAX_COUNT*sizeof(pj_timer_entry);
66 pool = pj_pool_create( mem, NULL, size, 4000, NULL);
67 if (!pool) {
68 PJ_LOG(3,("test", "...error: unable to create pool of %u bytes",
69 size));
70 return -10;
71 }
72
73 entry = (pj_timer_entry*)pj_pool_calloc(pool, MAX_COUNT, sizeof(*entry));
74 if (!entry)
75 return -20;
76
77 for (i=0; i<MAX_COUNT; ++i) {
78 entry[i].cb = &timer_callback;
79 }
80 rc = pj_timer_heap_create(pool, MAX_COUNT, &timer);
81 if (rc != PJ_SUCCESS) {
82 app_perror("...error: unable to create timer heap", rc);
83 return -30;
84 }
85
86 count = MIN_COUNT;
87 for (i=0; i<LOOP; ++i) {
88 int early = 0;
89 int done=0;
90 int cancelled=0;
91 int rc;
92 pj_timestamp t1, t2, t_sched, t_cancel, t_poll;
93 pj_time_val now, expire;
94
95 pj_gettimeofday(&now);
96 pj_srand(now.sec);
97 t_sched.u32.lo = t_cancel.u32.lo = t_poll.u32.lo = 0;
98
99 // Register timers
100 for (j=0; j<(int)count; ++j) {
101 delay.sec = pj_rand() % DELAY;
102 delay.msec = pj_rand() % 1000;
103
104 // Schedule timer
105 pj_get_timestamp(&t1);
106 rc = pj_timer_heap_schedule(timer, &entry[j], &delay);
107 if (rc != 0)
108 return -40;
109 pj_get_timestamp(&t2);
110
111 t_sched.u32.lo += (t2.u32.lo - t1.u32.lo);
112
113 // Poll timers.
114 pj_get_timestamp(&t1);
115 rc = pj_timer_heap_poll(timer, NULL);
116 pj_get_timestamp(&t2);
117 if (rc > 0) {
118 t_poll.u32.lo += (t2.u32.lo - t1.u32.lo);
119 early += rc;
120 }
121 }
122
123 // Set the time where all timers should finish
124 pj_gettimeofday(&expire);
125 delay.sec = DELAY;
126 delay.msec = 0;
127 PJ_TIME_VAL_ADD(expire, delay);
128
129 // Wait unfil all timers finish, cancel some of them.
130 do {
131 int index = pj_rand() % count;
132 pj_get_timestamp(&t1);
133 rc = pj_timer_heap_cancel(timer, &entry[index]);
134 pj_get_timestamp(&t2);
135 if (rc > 0) {
136 cancelled += rc;
137 t_cancel.u32.lo += (t2.u32.lo - t1.u32.lo);
138 }
139
140 pj_gettimeofday(&now);
141
142 pj_get_timestamp(&t1);
143#if defined(PJ_SYMBIAN) && PJ_SYMBIAN!=0
144 /* On Symbian, we must use OS poll (Active Scheduler poll) since
145 * timer is implemented using Active Object.
146 */
147 rc = 0;
148 while (pj_symbianos_poll(-1, 0))
149 ++rc;
150#else
151 rc = pj_timer_heap_poll(timer, NULL);
152#endif
153 pj_get_timestamp(&t2);
154 if (rc > 0) {
155 done += rc;
156 t_poll.u32.lo += (t2.u32.lo - t1.u32.lo);
157 }
158
159 } while (PJ_TIME_VAL_LTE(now, expire)&&pj_timer_heap_count(timer) > 0);
160
161 if (pj_timer_heap_count(timer)) {
162 PJ_LOG(3, (THIS_FILE, "ERROR: %d timers left",
163 pj_timer_heap_count(timer)));
164 ++err;
165 }
166 t_sched.u32.lo /= count;
167 t_cancel.u32.lo /= count;
168 t_poll.u32.lo /= count;
169 PJ_LOG(4, (THIS_FILE,
170 "...ok (count:%d, early:%d, cancelled:%d, "
171 "sched:%d, cancel:%d poll:%d)",
172 count, early, cancelled, t_sched.u32.lo, t_cancel.u32.lo,
173 t_poll.u32.lo));
174
175 count = count * 2;
176 if (count > MAX_COUNT)
177 break;
178 }
179
180 pj_pool_release(pool);
181 return err;
182}
183
184
185int timer_test()
186{
187 return test_timer_heap();
188}
189
190#else
191/* To prevent warning about "translation unit is empty"
192 * when this test is disabled.
193 */
194int dummy_timer_test;
195#endif /* INCLUDE_TIMER_TEST */
196
197