Update NEWS
[ACE_TAO.git] / ACE / performance-tests / Misc / test_guard.cpp
blobd56886b56d096477c18d323e46a9999da8f16ad0
1 // This test program illustrates the performance of ACE_GUARD
3 #include "ace/Log_Msg.h"
4 #include "ace/Get_Opt.h"
5 #include "ace/Profile_Timer.h"
6 #include "ace/Thread_Mutex.h"
7 #include "ace/Guard_T.h"
9 #if defined (ACE_HAS_THREADS)
11 static const int DEFAULT_ITERATIONS = 100000000;
13 enum
15 TEST_GUARD,
16 TEST_THR_GUARD,
17 TEST_END
20 ACE_Thread_Mutex lock_;
21 typedef void (*guard_func)(void);
22 int test_type = TEST_GUARD;
23 int dummy = 0;
25 void guard (void)
27 ACE_GUARD (ACE_Thread_Mutex, _ace_mon, lock_);
28 dummy++;
31 // FUZZ: disable check_for_ACE_Guard
32 const char *test_name[TEST_END] = { "ACE_Guard" };
33 // FUZZ: enable check_for_ACE_Guard
35 guard_func test_function=guard;
37 int
38 ACE_TMAIN(int argc, ACE_TCHAR *argv[])
40 ACE_Profile_Timer timer;
42 ACE_Get_Opt get_opt (argc, argv, ACE_TEXT("gn:"));
44 int iterations = DEFAULT_ITERATIONS;
45 int c, i;
47 while ((c = get_opt()) != -1)
48 switch (c)
50 case 'g':
51 test_type = TEST_GUARD;
52 test_function = guard;
53 break;
54 case 'n':
55 iterations = ACE_OS::atoi (get_opt.opt_arg ());
56 break;
57 default:
58 ACE_ERROR_RETURN ((LM_ERROR,
59 "Invalid option\n"), -1);
62 ACE_DEBUG ((LM_DEBUG, "%s for iterations = %d\n", test_name[test_type], iterations));
64 timer.start ();
66 // Test the thread mutex (which doesn't use inheritance or dynamic
67 // binding).
69 for (i = 0; i < iterations; i++)
70 test_function ();
72 timer.stop ();
74 ACE_Profile_Timer::ACE_Elapsed_Time et;
76 timer.elapsed_time (et);
78 ACE_DEBUG ((LM_DEBUG, "Thread_Mutex\n"));
79 ACE_DEBUG ((LM_DEBUG, "real time = %f secs, user time = %f secs, system time = %f secs\n",
80 et.real_time, et.user_time, et.system_time));
82 ACE_DEBUG ((LM_DEBUG, "time per call = %f usecs\n",
83 (et.real_time / double (iterations)) * 1000000));
85 return 0;
87 #else
88 int
89 ACE_TMAIN(int, ACE_TCHAR *[])
91 ACE_ERROR ((LM_ERROR, "threads not supported on this platform\n"));
92 return 0;
94 #endif /* ACE_HAS_THREADS */