1 /*
2 * CoBench by Davide Libenzi ( Portable Coroutine Library bench tester )
3 * Copyright (C) 2003 Davide Libenzi
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 * Davide Libenzi <davidel@xmailserver.org>
20 *
21 */
22
23 #include <stdio.h>
24 #include <stdarg.h>
25 #include <string.h>
26 #include <sys/time.h>
27 #include <unistd.h>
28 #include <pcl.h>
29
30
31 #define MIN_MEASURE_TIME 2000000ULL
32 #define CO_STACK_SIZE (8 * 1024)
33
34
35
36 static volatile unsigned long sw_counter;
37
38
39
getustime(void)40 static unsigned long long getustime(void) {
41 struct timeval tm;
42
43 gettimeofday(&tm, NULL);
44 return (unsigned long long) tm.tv_sec * 1000000ULL + (unsigned long long) tm.tv_usec;
45 }
46
47
switch_bench(void * data)48 static void switch_bench(void *data) {
49
50 for (;;) {
51 sw_counter--;
52 co_resume();
53 }
54 }
55
56
main(int argc,char * argv[])57 int main(int argc, char *argv[]) {
58 int i, ntimes;
59 coroutine_t coro;
60 unsigned long nswitches;
61 unsigned long long ts, te;
62
63 fprintf(stdout, "measuring co_create+co_delete performance ... ");
64 fflush(stdout);
65
66 ntimes = 10000;
67 do {
68 ts = getustime();
69 for (i = 0; i < ntimes; i++) {
70 if ((coro = co_create(switch_bench, NULL, NULL,
71 CO_STACK_SIZE)) != NULL)
72 co_delete(coro);
73 }
74 te = getustime();
75 ntimes *= 4;
76 } while ((te - ts) < MIN_MEASURE_TIME);
77
78 fprintf(stdout, "%g usec\n",
79 (double) (te - ts) / (double) ntimes);
80
81 if ((coro = co_create(switch_bench, NULL, NULL, CO_STACK_SIZE)) != NULL) {
82 fprintf(stdout, "measuring switch performance ... ");
83 fflush(stdout);
84
85 sw_counter = nswitches = 10000;
86 do {
87 ts = getustime();
88 while (sw_counter)
89 co_call(coro);
90 te = getustime();
91 sw_counter = (nswitches *= 4);
92 } while ((te - ts) < MIN_MEASURE_TIME);
93
94 fprintf(stdout, "%g usec\n",
95 (double) (te - ts) / (double) (2 * nswitches));
96
97 co_delete(coro);
98 }
99
100 return 0;
101 }
102
103