upload http
[bottlenecks.git] / rubbos / app / httpd-2.0.64 / srclib / apr / test / testlockperf.c
1 /* Licensed to the Apache Software Foundation (ASF) under one or more
2  * contributor license agreements.  See the NOTICE file distributed with
3  * this work for additional information regarding copyright ownership.
4  * The ASF licenses this file to You under the Apache License, Version 2.0
5  * (the "License"); you may not use this file except in compliance with
6  * the License.  You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include "apr_thread_proc.h"
18 #include "apr_thread_mutex.h"
19 #include "apr_thread_rwlock.h"
20 #include "apr_file_io.h"
21 #include "apr_errno.h"
22 #include "apr_general.h"
23 #include "apr_getopt.h"
24 #include "errno.h"
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include "test_apr.h"
28
29 #if !APR_HAS_THREADS
30 int main(void)
31 {
32     printf("This program won't work on this platform because there is no "
33            "support for threads.\n");
34     return 0;
35 }
36 #else /* !APR_HAS_THREADS */
37
38 #define MAX_COUNTER 1000000
39 #define MAX_THREADS 6
40
41 static long mutex_counter;
42
43 static apr_thread_mutex_t *thread_lock;
44 void * APR_THREAD_FUNC thread_mutex_func(apr_thread_t *thd, void *data);
45 apr_status_t test_thread_mutex(int num_threads); /* apr_thread_mutex_t */
46
47 static apr_thread_rwlock_t *thread_rwlock;
48 void * APR_THREAD_FUNC thread_rwlock_func(apr_thread_t *thd, void *data);
49 apr_status_t test_thread_rwlock(int num_threads); /* apr_thread_rwlock_t */
50
51 int test_thread_mutex_nested(int num_threads);
52
53 apr_pool_t *pool;
54 int i = 0, x = 0;
55
56 void * APR_THREAD_FUNC thread_mutex_func(apr_thread_t *thd, void *data)
57 {
58     int i;
59
60     for (i = 0; i < MAX_COUNTER; i++) {
61         apr_thread_mutex_lock(thread_lock);
62         mutex_counter++;
63         apr_thread_mutex_unlock(thread_lock);
64     }
65     return NULL;
66 }
67
68 void * APR_THREAD_FUNC thread_rwlock_func(apr_thread_t *thd, void *data)
69 {
70     int i;
71
72     for (i = 0; i < MAX_COUNTER; i++) {
73         apr_thread_rwlock_wrlock(thread_rwlock);
74         mutex_counter++;
75         apr_thread_rwlock_unlock(thread_rwlock);
76     }
77     return NULL;
78 }
79
80 int test_thread_mutex(int num_threads)
81 {
82     apr_thread_t *t[MAX_THREADS];
83     apr_status_t s[MAX_THREADS];
84     apr_time_t time_start, time_stop;
85     int i;
86
87     mutex_counter = 0;
88
89     printf("apr_thread_mutex_t Tests\n");
90     printf("%-60s", "    Initializing the apr_thread_mutex_t (UNNESTED)");
91     s[0] = apr_thread_mutex_create(&thread_lock, APR_THREAD_MUTEX_UNNESTED, pool);
92     if (s[0] != APR_SUCCESS) {
93         printf("Failed!\n");
94         return s[0];
95     }
96     printf("OK\n");
97
98     apr_thread_mutex_lock(thread_lock);
99     /* set_concurrency(4)? -aaron */
100     printf("    Starting %d threads    ", num_threads); 
101     for (i = 0; i < num_threads; ++i) {
102         s[i] = apr_thread_create(&t[i], NULL, thread_mutex_func, NULL, pool);
103         if (s[i] != APR_SUCCESS) {
104             printf("Failed!\n");
105             return s[i];
106         }
107     }
108     printf("OK\n");
109
110     time_start = apr_time_now();
111     apr_thread_mutex_unlock(thread_lock);
112
113     /* printf("%-60s", "    Waiting for threads to exit"); */
114     for (i = 0; i < num_threads; ++i) {
115         apr_thread_join(&s[i], t[i]);
116     }
117     /* printf("OK\n"); */
118
119     time_stop = apr_time_now();
120     printf("microseconds: %" APR_INT64_T_FMT " usec\n",
121            (time_stop - time_start));
122     if (mutex_counter != MAX_COUNTER * num_threads)
123         printf("error: counter = %ld\n", mutex_counter);
124
125     return APR_SUCCESS;
126 }
127
128 int test_thread_mutex_nested(int num_threads)
129 {
130     apr_thread_t *t[MAX_THREADS];
131     apr_status_t s[MAX_THREADS];
132     apr_time_t time_start, time_stop;
133     int i;
134
135     mutex_counter = 0;
136
137     printf("apr_thread_mutex_t Tests\n");
138     printf("%-60s", "    Initializing the apr_thread_mutex_t (NESTED)");
139     s[0] = apr_thread_mutex_create(&thread_lock, APR_THREAD_MUTEX_NESTED, pool);
140     if (s[0] != APR_SUCCESS) {
141         printf("Failed!\n");
142         return s[0];
143     }
144     printf("OK\n");
145
146     apr_thread_mutex_lock(thread_lock);
147     /* set_concurrency(4)? -aaron */
148     printf("    Starting %d threads    ", num_threads); 
149     for (i = 0; i < num_threads; ++i) {
150         s[i] = apr_thread_create(&t[i], NULL, thread_mutex_func, NULL, pool);
151         if (s[i] != APR_SUCCESS) {
152             printf("Failed!\n");
153             return s[i];
154         }
155     }
156     printf("OK\n");
157
158     time_start = apr_time_now();
159     apr_thread_mutex_unlock(thread_lock);
160
161     /* printf("%-60s", "    Waiting for threads to exit"); */
162     for (i = 0; i < num_threads; ++i) {
163         apr_thread_join(&s[i], t[i]);
164     }
165     /* printf("OK\n"); */
166
167     time_stop = apr_time_now();
168     printf("microseconds: %" APR_INT64_T_FMT " usec\n",
169            (time_stop - time_start));
170     if (mutex_counter != MAX_COUNTER * num_threads)
171         printf("error: counter = %ld\n", mutex_counter);
172
173     return APR_SUCCESS;
174 }
175
176 int test_thread_rwlock(int num_threads)
177 {
178     apr_thread_t *t[MAX_THREADS];
179     apr_status_t s[MAX_THREADS];
180     apr_time_t time_start, time_stop;
181     int i;
182
183     mutex_counter = 0;
184
185     printf("apr_thread_rwlock_t Tests\n");
186     printf("%-60s", "    Initializing the apr_thread_rwlock_t");
187     s[0] = apr_thread_rwlock_create(&thread_rwlock, pool);
188     if (s[0] != APR_SUCCESS) {
189         printf("Failed!\n");
190         return s[0];
191     }
192     printf("OK\n");
193
194     apr_thread_rwlock_wrlock(thread_rwlock);
195     /* set_concurrency(4)? -aaron */
196     printf("    Starting %d threads    ", num_threads); 
197     for (i = 0; i < num_threads; ++i) {
198         s[i] = apr_thread_create(&t[i], NULL, thread_rwlock_func, NULL, pool);
199         if (s[i] != APR_SUCCESS) {
200             printf("Failed!\n");
201             return s[i];
202         }
203     }
204     printf("OK\n");
205
206     time_start = apr_time_now();
207     apr_thread_rwlock_unlock(thread_rwlock);
208
209     /* printf("%-60s", "    Waiting for threads to exit"); */
210     for (i = 0; i < num_threads; ++i) {
211         apr_thread_join(&s[i], t[i]);
212     }
213     /* printf("OK\n"); */
214
215     time_stop = apr_time_now();
216     printf("microseconds: %" APR_INT64_T_FMT " usec\n",
217            (time_stop - time_start));
218     if (mutex_counter != MAX_COUNTER * num_threads)
219         printf("error: counter = %ld\n", mutex_counter);
220
221     return APR_SUCCESS;
222 }
223
224 int main(int argc, const char * const *argv)
225 {
226     apr_status_t rv;
227     char errmsg[200];
228     const char *lockname = "multi.lock";
229     apr_getopt_t *opt;
230     char optchar;
231     const char *optarg;
232
233     printf("APR Lock Performance Test\n==============\n\n");
234         
235     apr_initialize();
236     atexit(apr_terminate);
237
238     if (apr_pool_create(&pool, NULL) != APR_SUCCESS)
239         exit(-1);
240
241     if ((rv = apr_getopt_init(&opt, pool, argc, argv)) != APR_SUCCESS) {
242         fprintf(stderr, "Could not set up to parse options: [%d] %s\n",
243                 rv, apr_strerror(rv, errmsg, sizeof errmsg));
244         exit(-1);
245     }
246         
247     while ((rv = apr_getopt(opt, "f:", &optchar, &optarg)) == APR_SUCCESS) {
248         if (optchar == 'f') {
249             lockname = optarg;
250         }
251     }
252
253     if (rv != APR_SUCCESS && rv != APR_EOF) {
254         fprintf(stderr, "Could not parse options: [%d] %s\n",
255                 rv, apr_strerror(rv, errmsg, sizeof errmsg));
256         exit(-1);
257     }
258
259     for (i = 1; i <= MAX_THREADS; ++i) {
260         if ((rv = test_thread_mutex(i)) != APR_SUCCESS) {
261             fprintf(stderr,"thread_mutex test failed : [%d] %s\n",
262                     rv, apr_strerror(rv, (char*)errmsg, 200));
263             exit(-3);
264         }
265
266         if ((rv = test_thread_mutex_nested(i)) != APR_SUCCESS) {
267             fprintf(stderr,"thread_mutex (NESTED) test failed : [%d] %s\n",
268                     rv, apr_strerror(rv, (char*)errmsg, 200));
269             exit(-4);
270         }
271
272         if ((rv = test_thread_rwlock(i)) != APR_SUCCESS) {
273             fprintf(stderr,"thread_rwlock test failed : [%d] %s\n",
274                     rv, apr_strerror(rv, (char*)errmsg, 200));
275             exit(-6);
276         }
277     }
278
279     return 0;
280 }
281
282 #endif /* !APR_HAS_THREADS */