Compile fixes
[ACE_TAO.git] / ACE / tests / Message_Queue_Test_Ex.cpp
blob853eebb8504be0d536cdabc79f39e2cf7bafb85e
2 //=============================================================================
3 /**
4 * @file Message_Queue_Test_Ex.cpp
6 * This is:
7 * 1. A simple test of the ACE_Message_Queue_Ex that executes
8 * a performance measurement test for both single-threaded
9 * (null synch) and thread-safe ACE_Message_Queue_Ex
10 * instantiations.
11 * 2. An example of using a user-defined class to parameterize
12 * ACE_Message_Queue_Ex.
14 * @author Michael Vitlo <mvitalo@sprynet.com>
15 * @author copied the code from: Irfan Pyarali <irfan@cs.wustl.edu> and David L. Levine <levine@cs.wustl.edu>
17 //=============================================================================
20 #include "test_config.h"
21 #include "ace/Thread_Manager.h"
23 #include "ace/Auto_Ptr.h"
24 #include "ace/Message_Queue.h"
25 #include "ace/Synch_Traits.h"
26 #include "ace/Null_Mutex.h"
27 #include "ace/Null_Condition.h"
28 #include "ace/High_Res_Timer.h"
29 #include "ace/Message_Block.h"
30 #include "ace/OS_NS_sys_time.h"
31 #include "ace/Barrier.h"
32 #include "Message_Queue_Test_Ex.h" // Declares User_Class
34 const ACE_TCHAR usage[] =
35 ACE_TEXT ("usage: Message_Queue_Test_Ex <number of messages>\n");
37 using QUEUE = ACE_Message_Queue_Ex<User_Class, ACE_NULL_SYNCH>;
39 static const int MAX_MESSAGES = 10000;
40 static const char test_message[] = "ACE_Message_Queue_Ex Test Message";
42 static int max_messages = MAX_MESSAGES;
43 static int chain_limit = 4;
44 static ACE_Barrier tester_barrier (2);
46 // Dynamically allocate to avoid a static.
47 static ACE_High_Res_Timer *timer = 0;
49 // Helper printing function
50 static void
51 print_message (const ACE_TCHAR *message)
53 ACE_Time_Value tv;
54 timer->elapsed_time (tv);
55 ACE_DEBUG ((LM_INFO,
56 ACE_TEXT ("%s: %u messages took %u msec (%f msec/message)\n"),
57 message,
58 max_messages,
59 tv.msec (),
60 (double) tv.msec () / max_messages));
63 #if defined (ACE_HAS_THREADS)
64 using SYNCH_QUEUE = ACE_Message_Queue_Ex<User_Class, ACE_MT_SYNCH>;
66 /**
67 * Container for data passed to sender and receiver in
68 * performance test.
70 * For use in multithreaded performance test.
72 struct Queue_Wrapper
74 /// The message queue.
75 SYNCH_QUEUE *q_;
77 /// Pointer to messages blocks for sender to send to reciever.
78 User_Class **send_block_;
80 /// Default constructor.
81 Queue_Wrapper ()
82 : q_ (0), send_block_ (0)
87 struct MQ_Ex_N_Tester_Wrapper
89 // = TITLE
90 // Container for data passed to sender in the MQ_Ex_N_Tester
91 // performance test.
93 // = DESCRIPTION
94 // For use in multithreaded performance test.
95 MQ_Ex_N_Tester *tester_;
96 User_Class *head_send_block_;
99 #endif /* ACE_HAS_THREADS */
101 // Encapsulates the sent messages creation and destruction
102 struct Send_Messages
104 Send_Messages (int number_of_messages, int chain_limit):
105 send_block_ (0),
106 number_of_messages_ (number_of_messages),
107 chain_limit_ (chain_limit)
111 int create_messages (const char test_message[])
113 int limit = this->number_of_messages_ / this->chain_limit_;
114 ACE_NEW_RETURN (this->send_block_,
115 User_Class *[limit],
116 -1);
118 int i, j;
119 for (i = 0; i < limit; ++i)
121 User_Class *&temp1 = this->send_block_[i];
122 ACE_NEW_RETURN (temp1,
123 User_Class (test_message),
124 -1);
125 User_Class *tail = temp1;
126 for (j = 1; j < this->chain_limit_; ++j)
128 User_Class *temp2 = 0;
129 ACE_NEW_RETURN (temp2,
130 User_Class (test_message),
131 -1);
132 tail->next (temp2);
133 tail = temp2;
136 this->head_send_block_ = this->send_block_[0];
137 return 0;
140 ~Send_Messages ()
142 int j, i = 0;
143 int limit = this->number_of_messages_ / this->chain_limit_;
144 for (; i < limit; ++i)
146 User_Class *&temp1 = this->send_block_[i];
147 for (j = 0; j < this->chain_limit_; ++j)
149 User_Class *temp2 = temp1->next ();
150 delete temp1;
151 temp1 = temp2;
154 delete [] this->send_block_;
157 User_Class * head_send_block_;
158 User_Class ** send_block_;
159 int number_of_messages_;
160 int chain_limit_;
163 // Encapsulates the received messages creation and destruction
164 struct Receive_Messages
166 Receive_Messages (int number_of_messages) :
167 receive_block_ (0),
168 number_of_messages_ (number_of_messages)
172 int create ()
174 ACE_NEW_RETURN (this->receive_block_,
175 User_Class *[this->number_of_messages_],
176 -1);
177 return 0;
180 ~Receive_Messages ()
182 delete [] this->receive_block_;
185 User_Class **receive_block_;
186 int number_of_messages_;
189 static int
190 single_thread_performance_test ()
192 const char test_message[] =
193 "ACE_Message_Queue_Ex Test Message";
194 const ACE_TCHAR *message =
195 ACE_TEXT ("ACE_Message_Queue_Ex<ACE_NULL_SYNCH>, single thread");
197 // Create a message queue.
198 QUEUE *msgq = 0;
200 ACE_NEW_RETURN (msgq,
201 QUEUE,
202 -1);
204 // Create the messages. Allocate off the heap in case messages is
205 // large relative to the amount of stack space available.
206 User_Class **send_block = 0;
207 ACE_NEW_RETURN (send_block,
208 User_Class *[max_messages],
209 -1);
211 int i = 0;
213 for (i = 0; i < max_messages; ++i)
214 ACE_NEW_RETURN (send_block[i],
215 User_Class (test_message),
216 -1);
218 User_Class **receive_block_p = 0;
219 ACE_NEW_RETURN (receive_block_p,
220 User_Class *[max_messages],
221 -1);
223 timer->start ();
225 // Send/receive the messages.
226 for (i = 0; i < max_messages; ++i)
228 if (msgq->enqueue_tail (send_block[i]) == -1)
229 ACE_ERROR_RETURN ((LM_ERROR,
230 ACE_TEXT ("%p\n"),
231 ACE_TEXT ("enqueue")),
232 -1);
234 if (msgq->dequeue_head (receive_block_p[i]) == -1)
235 ACE_ERROR_RETURN ((LM_ERROR,
236 ACE_TEXT ("%p\n"),
237 ACE_TEXT ("dequeue_head")),
238 -1);
241 timer->stop ();
242 print_message (message);
243 timer->reset ();
245 delete [] receive_block_p;
247 for (i = 0; i < max_messages; ++i)
248 delete send_block[i];
249 delete [] send_block;
250 delete msgq;
252 return 0;
256 MQ_Ex_N_Tester::single_thread_performance_test ()
258 // Create the messages. Allocate off the heap in case messages is
259 // large relative to the amount of stack space available.
261 if ((0 != this->test_enqueue_tail ()) ||
262 (0 != this->test_enqueue_head ()) )
264 return -1;
267 return 0;
271 MQ_Ex_N_Tester::test_enqueue_tail ()
273 const ACE_TCHAR *message =
274 ACE_TEXT ("ACE_Message_Queue_Ex_N<ACE_NULL_SYNCH>, test_enqueue_tail");
276 // Send_Messages creates messages and deletes them when it gets out of scope
277 Send_Messages messages (max_messages, chain_limit);
278 if (-1 == messages.create_messages (test_message))
280 return -1;
282 Receive_Messages r_messages (max_messages);
283 if (-1 == r_messages.create ())
285 return -1;
288 // prepare
289 int limit = max_messages / chain_limit;
290 timer->start ();
291 // Send with just one call
292 for (int i = 0; i < limit; ++i)
294 if (-1 == this->st_queue_.enqueue_tail (messages.send_block_[i]))
296 ACE_ERROR_RETURN ((LM_ERROR,
297 ACE_TEXT ("%p\n"),
298 ACE_TEXT ("enqueue_tail_n")),
299 -1);
302 for (int j = 0, k = 0; j < chain_limit; ++j, ++k)
304 if (this->st_queue_.dequeue_head (r_messages.receive_block_[k]) == -1)
306 ACE_ERROR_RETURN ((LM_ERROR,
307 ACE_TEXT ("%p\n"),
308 ACE_TEXT ("dequeue_head")),
309 -1);
313 timer->stop ();
315 print_message (message);
317 timer->reset ();
319 return 0;
323 MQ_Ex_N_Tester::test_enqueue_head ()
325 const ACE_TCHAR *message =
326 ACE_TEXT ("ACE_Message_Queue_Ex_N<ACE_NULL_SYNCH>, test_enqueue_head");
328 // Send_Messages creates messages and deletes them when it gets out of scope
329 Send_Messages messages (max_messages, chain_limit);
330 if (-1 == messages.create_messages (test_message))
332 return -1;
334 Receive_Messages r_messages (max_messages);
335 if (-1 == r_messages.create ())
337 return -1;
340 // prepare
341 int i, j, k = 0;
343 int limit = max_messages / chain_limit;
344 timer->start ();
346 // Send/receive the messages.
347 // Send with just one call
348 for (i = 0; i < limit; ++i)
350 if (-1 == this->st_queue_.enqueue_head (messages.send_block_[i]))
352 ACE_ERROR_RETURN ((LM_ERROR,
353 ACE_TEXT ("%p\n"),
354 ACE_TEXT ("enqueue_tail_n")),
355 -1);
358 for (j = 0; j < chain_limit; ++j, ++k)
360 if (this->st_queue_.dequeue_head (r_messages.receive_block_[k]) == -1)
362 ACE_ERROR_RETURN ((LM_ERROR,
363 ACE_TEXT ("%p\n"),
364 ACE_TEXT ("dequeue_head")),
365 -1);
369 timer->stop ();
371 print_message (message);
373 timer->reset ();
375 return 0;
378 #if defined (ACE_HAS_THREADS)
380 static void *
381 receiver (void *arg)
383 Queue_Wrapper *queue_wrapper = reinterpret_cast<Queue_Wrapper *> (arg);
384 int i;
386 User_Class **receive_block_p = 0;
387 ACE_NEW_RETURN (receive_block_p,
388 User_Class *[max_messages],
389 (void *) -1);
391 for (i = 0; i < max_messages; ++i)
392 if (queue_wrapper->q_->dequeue_head (receive_block_p[i]) == -1)
393 ACE_ERROR_RETURN ((LM_ERROR,
394 ACE_TEXT ("%p\n"),
395 ACE_TEXT ("dequeue_head")),
397 timer->stop ();
399 delete [] receive_block_p;
401 return 0;
404 static void *
405 sender (void *arg)
407 Queue_Wrapper *queue_wrapper = reinterpret_cast<Queue_Wrapper *> (arg);
408 int i;
410 timer->start ();
412 // Send the messages.
413 for (i = 0; i < max_messages; ++i)
414 if (queue_wrapper->q_->
415 enqueue_tail (queue_wrapper->send_block_[i]) == -1)
416 ACE_ERROR_RETURN ((LM_ERROR,
417 ACE_TEXT ("%p\n"),
418 ACE_TEXT ("enqueue")),
420 return 0;
423 static int
424 performance_test ()
426 Queue_Wrapper queue_wrapper;
427 const ACE_TCHAR *message =
428 ACE_TEXT ("ACE_Message_Queue_Ex<ACE_SYNCH>");
429 int i = 0;
431 // Create the messages. Allocate off the heap in case messages is
432 // large relative to the amount of stack space available. Allocate
433 // it here instead of in the sender, so that we can delete it after
434 // the _receiver_ is done.
435 User_Class **send_block = 0;
436 ACE_NEW_RETURN (send_block,
437 User_Class *[max_messages],
438 -1);
440 for (i = 0; i < max_messages; ++i)
441 ACE_NEW_RETURN (send_block[i],
442 User_Class (test_message),
443 -1);
445 queue_wrapper.send_block_ = send_block;
447 ACE_NEW_RETURN (queue_wrapper.q_,
448 SYNCH_QUEUE,
449 -1);
451 if (ACE_Thread_Manager::instance ()->spawn ((ACE_THR_FUNC) sender,
452 &queue_wrapper,
453 THR_BOUND) == -1)
454 ACE_ERROR_RETURN ((LM_ERROR,
455 ACE_TEXT ("%p\n"),
456 ACE_TEXT ("spawning sender thread")),
457 -1);
459 if (ACE_Thread_Manager::instance ()->spawn ((ACE_THR_FUNC) receiver,
460 &queue_wrapper,
461 THR_BOUND) == -1)
462 ACE_ERROR_RETURN ((LM_ERROR,
463 ACE_TEXT ("%p\n"),
464 ACE_TEXT ("spawning receiver thread")),
465 -1);
467 ACE_Thread_Manager::instance ()->wait ();
468 print_message (message);
469 timer->reset ();
471 delete queue_wrapper.q_;
472 queue_wrapper.q_ = 0;
474 for (i = 0; i < max_messages; ++i)
475 delete send_block[i];
476 delete [] send_block;
478 return 0;
482 MQ_Ex_N_Tester::performance_test ()
484 const ACE_TCHAR *message =
485 ACE_TEXT ("ACE_Message_Queue_Ex_N<ACE_SYNCH>");
487 Send_Messages messages (max_messages, chain_limit);
488 if (-1 == messages.create_messages (test_message))
490 return -1;
493 MQ_Ex_N_Tester_Wrapper tester_wrapper;
494 tester_wrapper.head_send_block_ = messages.head_send_block_;
495 tester_wrapper.tester_ = this;
497 if (ACE_Thread_Manager::instance ()->spawn ((ACE_THR_FUNC) &MQ_Ex_N_Tester::sender,
498 &tester_wrapper,
499 THR_BOUND) == -1)
500 ACE_ERROR_RETURN ((LM_ERROR,
501 ACE_TEXT ("%p\n"),
502 ACE_TEXT ("spawning sender thread")),
503 -1);
505 if (ACE_Thread_Manager::instance ()->spawn ((ACE_THR_FUNC) &MQ_Ex_N_Tester::receiver,
506 this,
507 THR_BOUND) == -1)
508 ACE_ERROR_RETURN ((LM_ERROR,
509 ACE_TEXT ("%p\n"),
510 ACE_TEXT ("spawning receiver thread")),
511 -1);
513 ACE_Thread_Manager::instance ()->wait ();
515 print_message (message);
517 timer->reset ();
519 return 0;
522 ACE_THR_FUNC_RETURN
523 MQ_Ex_N_Tester::receiver (void *args)
525 MQ_Ex_N_Tester *tester = reinterpret_cast<MQ_Ex_N_Tester *> (args);
527 User_Class **receive_block_p = 0;
528 ACE_NEW_RETURN (receive_block_p,
529 User_Class *[max_messages],
530 (ACE_THR_FUNC_RETURN) -1);
532 int i;
533 tester_barrier.wait ();
534 for (i = 0; i < max_messages; ++i)
536 if (tester->mt_queue_.dequeue_head (receive_block_p[i]) == -1)
538 ACE_ERROR ((LM_ERROR,
539 ACE_TEXT ("%p\n"),
540 ACE_TEXT ("dequeue_head")));
541 return (ACE_THR_FUNC_RETURN) -1;
544 timer->stop ();
546 delete [] receive_block_p;
548 return 0;
551 ACE_THR_FUNC_RETURN
552 MQ_Ex_N_Tester::sender (void *args)
554 MQ_Ex_N_Tester_Wrapper *tester_wrapper =
555 reinterpret_cast<MQ_Ex_N_Tester_Wrapper *> (args);
556 MQ_Ex_N_Tester *tester = tester_wrapper->tester_;
558 Send_Messages messages (max_messages, chain_limit);
559 if (-1 == messages.create_messages (test_message))
561 return (ACE_THR_FUNC_RETURN) -1;
563 int limit = max_messages / chain_limit;
564 tester_barrier.wait ();
565 timer->start ();
566 // Send/receive the messages.
567 timer->start ();
568 // Send with just one call
569 for (int i = 0; i < limit; ++i)
571 if (-1 == tester->mt_queue_.enqueue_tail (messages.send_block_[i]))
573 ACE_ERROR ((LM_ERROR,
574 ACE_TEXT ("%p\n"),
575 ACE_TEXT ("enqueue_tail_n")));
576 return (ACE_THR_FUNC_RETURN) -1;
579 return 0;
582 #endif /* ACE_HAS_THREADS */
584 int basic_queue_test (ACE_Message_Queue_Ex<User_Class, ACE_SYNCH>& q)
586 int status = 0;
587 if (!q.is_empty ())
589 ACE_ERROR ((LM_ERROR, ACE_TEXT ("New queue is not empty!\n")));
590 status = 1;
592 else
594 User_Class *b;
595 ACE_Time_Value tv (ACE_OS::gettimeofday ()); // Now
596 if (q.dequeue_head (b, &tv) != -1)
598 ACE_ERROR ((LM_ERROR, ACE_TEXT ("Dequeued from empty queue!\n")));
599 status = 1;
601 else if (errno != EWOULDBLOCK)
603 ACE_ERROR ((LM_ERROR,
604 ACE_TEXT ("%p\n"),
605 ACE_TEXT ("Dequeue timeout should be EWOULDBLOCK, got")));
606 status = 1;
608 else
610 ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("Timed dequeue test: OK\n")));
611 status = 0; // All is well
615 return status;
618 int queue_priority_test (ACE_Message_Queue_Ex<User_Class, ACE_SYNCH>& q)
620 int status = 0;
621 if (!q.is_empty ())
622 ACE_ERROR_RETURN ((LM_ERROR, ACE_TEXT ("Prio test queue not empty\n")), 1);
624 // Set up a few objects with names for how they should come out of the queue.
625 ACE_Auto_Basic_Ptr<User_Class> b1, b2, b3, b4;
626 b1.reset (new User_Class ("first"));
627 b2.reset (new User_Class ("second"));
628 b3.reset (new User_Class ("third"));
629 b4.reset (new User_Class ("fourth"));
630 unsigned long prio =
631 ACE_Message_Queue_Ex<User_Class, ACE_SYNCH>::DEFAULT_PRIORITY;
633 prio += 1;
634 if (-1 == q.enqueue_prio (b2.get (), 0, prio))
635 ACE_ERROR_RETURN ((LM_ERROR, ACE_TEXT ("%p\n"), ACE_TEXT ("b2")), 1);
636 if (-1 == q.enqueue_prio (b3.get (), 0, prio))
637 ACE_ERROR_RETURN ((LM_ERROR, ACE_TEXT ("%p\n"), ACE_TEXT ("b3")), 1);
638 prio -= 1;
639 if (-1 == q.enqueue_prio (b4.get (), 0, prio))
640 ACE_ERROR_RETURN ((LM_ERROR, ACE_TEXT ("%p\n"), ACE_TEXT ("b4")), 1);
641 prio += 5;
642 if (-1 == q.enqueue_prio (b1.get (), 0, prio))
643 ACE_ERROR_RETURN ((LM_ERROR, ACE_TEXT ("%p\n"), ACE_TEXT ("b1")), 1);
645 User_Class *b = 0;
646 if (q.dequeue_head (b) == -1)
648 ACE_ERROR ((LM_ERROR, ACE_TEXT ("%p\n"), ACE_TEXT ("dequeue 1")));
649 ++status;
651 else
653 if (ACE_OS::strcmp (b->message (), "first") != 0)
655 ACE_ERROR ((LM_ERROR,
656 ACE_TEXT ("First dequeued was %C\n"),
657 b->message ()));
658 ++status;
661 if (q.dequeue_head (b) == -1)
663 ACE_ERROR ((LM_ERROR, ACE_TEXT ("%p\n"), ACE_TEXT ("dequeue 2")));
664 ++status;
666 else
668 if (ACE_OS::strcmp (b->message (), "second") != 0)
670 ACE_ERROR ((LM_ERROR,
671 ACE_TEXT ("Second dequeued was %C\n"),
672 b->message ()));
673 ++status;
676 if (q.dequeue_head (b) == -1)
678 ACE_ERROR ((LM_ERROR, ACE_TEXT ("%p\n"), ACE_TEXT ("dequeue 3")));
679 ++status;
681 else
683 if (ACE_OS::strcmp (b->message (), "third") != 0)
685 ACE_ERROR ((LM_ERROR,
686 ACE_TEXT ("Third dequeued was %C\n"),
687 b->message ()));
688 ++status;
691 if (q.dequeue_head (b) == -1)
693 ACE_ERROR ((LM_ERROR, ACE_TEXT ("%p\n"), ACE_TEXT ("dequeue 4")));
694 ++status;
696 else
698 if (ACE_OS::strcmp (b->message (), "fourth") != 0)
700 ACE_ERROR ((LM_ERROR,
701 ACE_TEXT ("Fourth dequeued was %C\n"),
702 b->message ()));
703 ++status;
707 if (status == 0)
708 ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("Priority queueing test: OK\n")));
709 return status;
713 run_main (int argc, ACE_TCHAR *argv[])
715 ACE_START_TEST (ACE_TEXT ("Message_Queue_Test_Ex"));
717 if (argc == 2)
719 if (! ACE_OS::strcmp (argv[1], ACE_TEXT ("-?")))
721 ACE_ERROR ((LM_ERROR,
722 ACE_TEXT ("%s/n"),
723 usage));
725 else
727 max_messages = ACE_OS::atoi (argv[1]);
731 int status = 0;
733 // Be sure that the a timed out get sets the error code properly.
734 ACE_Message_Queue_Ex<User_Class, ACE_SYNCH> q1;
735 ACE_Message_Queue_Ex_N<User_Class, ACE_SYNCH> q2;
736 if (0 != basic_queue_test (q1) ||
737 0 != basic_queue_test (q2))
739 ++status;
742 // Check priority operations.
743 if (0 != queue_priority_test (q1))
745 ++status;
748 ACE_NEW_RETURN (timer,
749 ACE_High_Res_Timer,
750 -1);
752 status += single_thread_performance_test ();
754 #if defined (ACE_HAS_THREADS)
755 status += performance_test ();
756 #endif /* ACE_HAS_THREADS */
759 MQ_Ex_N_Tester ex_n_tester;
760 status += ex_n_tester.single_thread_performance_test ();
761 #if defined (ACE_HAS_THREADS)
762 status += ex_n_tester.performance_test ();
763 #endif /* ACE_HAS_THREADS */
766 if (status != 0)
767 ACE_ERROR ((LM_ERROR,
768 ACE_TEXT ("%p\n"),
769 ACE_TEXT ("test failed")));
770 delete timer;
771 timer = 0;
773 ACE_END_TEST;
774 return status;