2 //=============================================================================
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>
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
;
26 speed_test (ACE_UINT32 loops
)
37 size_t chunk_size
= 8;
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
;
52 ptr
= allocator
.malloc (chunk_size
);
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;
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
));
73 typedef char MEMBLOCK
[8];
74 typedef ACE_Cached_Allocator
<MEMBLOCK
, ACE_SYNCH_NULL_MUTEX
> STATIC_ALLOCATOR
;
77 stdspeed_test (ACE_UINT32 loops
)
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
;
102 ptr
= allocator
.malloc (chunk_size
);
103 allocator
.free (ptr
);
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;
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
));
123 run_main (int argc
, ACE_TCHAR
*argv
[])
125 ACE_START_TEST (ACE_TEXT ("Cached_Allocator_Test"));
127 size_t chunk_size
= 0;
129 size_t requested_size
= 0;
135 ACE_UINT32 loops
= 0;
137 const char *str1
= "12345678";
138 const char *str3
= "ABCDEFGH";
143 loops
= ACE_OS::atoi (argv
[1]);
147 for (chunk_size
= 1; chunk_size
<= 9; ++chunk_size
) // 9 is strlen(str1 or str2) + 1
150 ACE_TEXT (" (%t) Creating allocator: ")
151 ACE_TEXT ("%B chunks, %B bytes each\n"),
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"),
161 requested_size
= chunk_size
;
163 ACE_TEXT (" (%t) Allocating chunk 1: %B bytes, should succeed...\n"),
166 ptr1
= (char *) allocator
.malloc (requested_size
);
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;
178 ACE_TEXT (" (%t) Allocating chunk 2: %B bytes, too big, should fail...\n"),
181 ptr2
= (char *) allocator
.malloc (requested_size
);
183 ACE_DEBUG ((LM_INFO
, ACE_TEXT (" (%t) OK, failed.\n")));
185 ACE_ERROR_RETURN ((LM_ERROR
, ACE_TEXT (" (%t) Something is wrong...\n")), -1);
187 requested_size
= chunk_size
- 1;
189 ACE_TEXT (" (%t) Allocating chunk 3: %B bytes, ")
190 ACE_TEXT ("should succeed...\n"),
192 ptr3
= (char *) allocator
.malloc (requested_size
);
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"),
203 requested_size
= chunk_size
;
205 ACE_TEXT (" (%t) Allocating chunk 4: %B bytes, no free chunks,")
206 ACE_TEXT (" should fail...\n"),
209 ptr4
= (char *) allocator
.malloc (requested_size
);
211 ACE_DEBUG ((LM_INFO
, ACE_TEXT (" (%t) OK, failed.\n")));
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"),
232 ptr1
= (char *) allocator
.malloc (requested_size
);
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
);
245 stdspeed_test (loops
);