Merge pull request #1844 from jrw972/monterey
[ACE_TAO.git] / ACE / tests / Cached_Allocator_Test.cpp
blob4d4b7f8dbf17c56892d7e2e262af6b0900cf33ce
2 //=============================================================================
3 /**
4 * @file Cached_Allocator_Test.cpp
6 * Simple test of ACE_Dynamic_Cached_Allocator and ACE_Cached_Allocator.
8 * @author Jaroslaw Nozderko <jareknz@polbox.com>
9 */
10 //=============================================================================
13 #include "test_config.h"
14 #include "ace/OS_NS_string.h"
15 #include "ace/Malloc_T.h"
16 #include "ace/High_Res_Timer.h"
20 #include "ace/Synch_Traits.h"
21 #include "ace/Null_Mutex.h"
23 typedef ACE_Dynamic_Cached_Allocator<ACE_SYNCH_NULL_MUTEX> DYNAMIC_ALLOCATOR;
25 static int
26 speed_test (ACE_UINT32 loops)
28 double tt = 0.0,
29 ut = 0.0,
30 utus = 0.0,
31 speed = 0.0;
33 ACE_Time_Value tc;
34 void *ptr = 0;
35 ACE_UINT32 i = loops;
36 size_t n_chunks = 10;
37 size_t chunk_size = 8;
39 ACE_DEBUG ((LM_INFO,
40 ACE_TEXT (" (%t) ACE_Dynamic_Cached_Allocator ")
41 ACE_TEXT ("speed test...\n")));
43 DYNAMIC_ALLOCATOR allocator (n_chunks, chunk_size);
45 ACE_High_Res_Timer timer;
46 timer.reset ();
48 timer.start ();
50 while (i--)
52 ptr = allocator.malloc (chunk_size);
53 allocator.free (ptr);
56 timer.stop ();
58 timer.elapsed_time (tc);
60 ACE_DEBUG ((LM_INFO, ACE_TEXT (" (%t) Iterations : %d\n"), loops));
61 tt = tc.sec () + tc.usec ()*1.0e-6;
62 ut = tt/loops;
63 utus = ut*1.0e6;
64 speed = loops/tt;
66 ACE_DEBUG ((LM_INFO, ACE_TEXT (" (%t) Total time : %.6g [s]\n"), tt));
67 ACE_DEBUG ((LM_INFO, ACE_TEXT (" (%t) Unit time : %.6g [us]\n"), utus));
68 ACE_DEBUG ((LM_INFO, ACE_TEXT (" (%t) Speed : %.6g [1/s]\n"), speed));
70 return 0;
73 typedef char MEMBLOCK[8];
74 typedef ACE_Cached_Allocator<MEMBLOCK, ACE_SYNCH_NULL_MUTEX> STATIC_ALLOCATOR;
76 static int
77 stdspeed_test (ACE_UINT32 loops)
80 double tt = 0.0,
81 ut = 0.0,
82 utus = 0.0,
83 speed = 0.0;
85 ACE_Time_Value tc;
86 void *ptr = 0;
87 ACE_UINT32 i = loops;
88 size_t n_chunks = 10,
89 chunk_size = 8;
91 ACE_DEBUG ((LM_INFO, ACE_TEXT (" (%t) ACE_Cached_Allocator ")
92 ACE_TEXT ("speed test...\n")));
94 STATIC_ALLOCATOR allocator (n_chunks);
96 ACE_High_Res_Timer timer;
97 timer.reset ();
99 timer.start ();
100 while (i--)
102 ptr = allocator.malloc (chunk_size);
103 allocator.free (ptr);
105 timer.stop ();
107 timer.elapsed_time (tc);
109 ACE_DEBUG ((LM_INFO, ACE_TEXT (" (%t) Iterations : %d\n"), loops));
110 tt = tc.sec () + tc.usec ()*1.0e-6;
111 ut = tt/loops;
112 utus = ut*1.0e6;
113 speed = loops/tt;
115 ACE_DEBUG ((LM_INFO, ACE_TEXT (" (%t) Total time : %.6g [s]\n"), tt));
116 ACE_DEBUG ((LM_INFO, ACE_TEXT (" (%t) Unit time : %.6g [us]\n"), utus));
117 ACE_DEBUG ((LM_INFO, ACE_TEXT (" (%t) Speed : %.6g [1/s]\n"), speed));
119 return 0;
123 run_main (int argc, ACE_TCHAR *argv[])
125 ACE_START_TEST (ACE_TEXT ("Cached_Allocator_Test"));
127 size_t chunk_size = 0;
128 size_t n_chunks = 0;
129 size_t requested_size = 0;
130 size_t depth = 0;
131 char *ptr1 = 0;
132 char *ptr2 = 0;
133 char *ptr3 = 0;
134 char *ptr4 = 0;
135 ACE_UINT32 loops = 0;
137 const char *str1 = "12345678";
138 const char *str3 = "ABCDEFGH";
140 if (argc < 2)
141 loops = 10000000;
142 else
143 loops = ACE_OS::atoi (argv[1]);
145 n_chunks = 2;
147 for (chunk_size = 1; chunk_size <= 9; ++chunk_size) // 9 is strlen(str1 or str2) + 1
149 ACE_DEBUG ((LM_INFO,
150 ACE_TEXT (" (%t) Creating allocator: ")
151 ACE_TEXT ("%B chunks, %B bytes each\n"),
152 n_chunks,
153 chunk_size));
155 DYNAMIC_ALLOCATOR allocator (n_chunks, chunk_size);
157 if ((depth = allocator.pool_depth ()) != n_chunks)
158 ACE_ERROR ((LM_ERROR,
159 ACE_TEXT ("Expected pool depth %B but reported %B\n"),
160 n_chunks, depth));
161 requested_size = chunk_size;
162 ACE_DEBUG ((LM_INFO,
163 ACE_TEXT (" (%t) Allocating chunk 1: %B bytes, should succeed...\n"),
164 requested_size));
166 ptr1 = (char *) allocator.malloc (requested_size);
167 if (!ptr1)
168 ACE_ERROR_RETURN ((LM_ERROR, ACE_TEXT (" (%t) Failed, exiting.\n")), -1);
170 ACE_DEBUG ((LM_INFO, ACE_TEXT (" (%t) OK, succeeded.\n")));
171 if ((depth = allocator.pool_depth ()) != (n_chunks - 1))
172 ACE_ERROR ((LM_ERROR,
173 ACE_TEXT ("Expected pool depth %B but reported %B\n"),
174 n_chunks - 1, depth));
176 requested_size = chunk_size + 1;
177 ACE_DEBUG ((LM_INFO,
178 ACE_TEXT (" (%t) Allocating chunk 2: %B bytes, too big, should fail...\n"),
179 requested_size));
181 ptr2 = (char *) allocator.malloc (requested_size);
182 if (!ptr2)
183 ACE_DEBUG ((LM_INFO, ACE_TEXT (" (%t) OK, failed.\n")));
184 else
185 ACE_ERROR_RETURN ((LM_ERROR, ACE_TEXT (" (%t) Something is wrong...\n")), -1);
187 requested_size = chunk_size - 1;
188 ACE_DEBUG ((LM_INFO,
189 ACE_TEXT (" (%t) Allocating chunk 3: %B bytes, ")
190 ACE_TEXT ("should succeed...\n"),
191 requested_size));
192 ptr3 = (char *) allocator.malloc (requested_size);
193 if (!ptr3)
194 ACE_ERROR_RETURN ((LM_ERROR, ACE_TEXT (" (%t) Failed, exiting.\n")), -1);
196 ACE_DEBUG ((LM_INFO, ACE_TEXT (" (%t) OK, succeeded.\n")));
198 // One chunk too far...
199 if ((depth = allocator.pool_depth ()) != 0)
200 ACE_ERROR ((LM_ERROR,
201 ACE_TEXT ("Expected pool depth 0 but reported %B\n"),
202 depth));
203 requested_size = chunk_size;
204 ACE_DEBUG ((LM_INFO,
205 ACE_TEXT (" (%t) Allocating chunk 4: %B bytes, no free chunks,")
206 ACE_TEXT (" should fail...\n"),
207 requested_size));
209 ptr4 = (char *) allocator.malloc (requested_size);
210 if (!ptr4)
211 ACE_DEBUG ((LM_INFO, ACE_TEXT (" (%t) OK, failed.\n")));
212 else
213 ACE_ERROR_RETURN ((LM_ERROR, ACE_TEXT (" (%t) Something is wrong\n")), -1);
215 ACE_DEBUG ((LM_INFO, ACE_TEXT (" (%t) Writing to chunk 1: %C\n"), str1));
216 ACE_OS::memcpy (ptr1, str1, chunk_size);
217 ptr1[chunk_size - 1] = '\0';
218 ACE_DEBUG ((LM_INFO, ACE_TEXT (" (%t) Reading from chunk 1: %C\n"), ptr1));
220 ACE_DEBUG ((LM_INFO, ACE_TEXT (" (%t) Writing to chunk 3: %C\n"), str3));
221 ACE_OS::memcpy (ptr3, str3, chunk_size);
222 ptr3[chunk_size - 1] = '\0';
223 ACE_DEBUG ((LM_INFO, ACE_TEXT (" (%t) Reading from chunk 3: %C\n"), ptr3));
225 ACE_DEBUG ((LM_INFO, ACE_TEXT (" (%t) Deallocating chunk 1\n")));
226 allocator.free (ptr1);
228 requested_size = chunk_size;
229 ACE_DEBUG ((LM_INFO, ACE_TEXT (" (%t) Allocating chunk: %B bytes, ")
230 ACE_TEXT ("should succeed...\n"),
231 requested_size));
232 ptr1 = (char *) allocator.malloc (requested_size);
233 if (!ptr1)
234 ACE_ERROR_RETURN ((LM_ERROR, ACE_TEXT (" (%t) Failed, exiting.\n")), -1);
236 ACE_DEBUG ((LM_INFO, ACE_TEXT (" (%t) OK, succeeded.\n")));
238 ACE_DEBUG ((LM_INFO, ACE_TEXT (" (%t) Deallocating chunk 1\n")));
239 allocator.free (ptr1);
240 ACE_DEBUG ((LM_INFO, ACE_TEXT (" (%t) Deallocating chunk 3\n")));
241 allocator.free (ptr3);
244 speed_test (loops);
245 stdspeed_test (loops);
247 ACE_END_TEST;
248 return 0;