1 #define ACE_BUILD_SVC_DLL
3 #include "Baseline_Test.h"
5 # if defined (ACE_HAS_THREADS)
7 #include "ace/OS_NS_unistd.h"
8 #include "ace/Service_Repository.h"
9 #include "ace/Get_Opt.h"
10 #include "ace/Thread_Manager.h"
12 #if !defined (__ACE_INLINE__)
13 #include "Baseline_Test.inl"
14 #endif /* __ACE_INLINE__ */
16 Baseline_Test_Options baseline_options
;
17 // Static Baseline Options holds the test configuration information
18 // and the test statistics.
20 Baseline_Test_Base::Baseline_Test_Base ()
21 : Benchmark_Base (Benchmark_Base::BASELINE
),
22 yield_method_ (Baseline_Test_Options::USE_SLEEP_ZERO
),
23 iteration_ (DEFAULT_ITERATIONS
),
29 Baseline_Test_Base::init (int argc
, ACE_TCHAR
*argv
[])
31 return this->parse_args (argc
, argv
);
35 Baseline_Test_Base::parse_args (int argc
, ACE_TCHAR
*argv
[])
37 ACE_Get_Opt
get_opt (argc
, argv
, ACE_TEXT("i:ylrw"), 0);
40 while ((c
= get_opt ()) != -1)
43 case 'i': // Total iterations
45 int tmp
= ACE_OS::atoi (get_opt
.opt_arg ());
47 ACE_ERROR_RETURN ((LM_ERROR
,
48 "%d is not a valid value for iteration\n",
51 this->iteration_
= static_cast<size_t> (tmp
);
55 case 'y': // Use thr_yield.
56 this->yield_method_
= Baseline_Test_Options::USE_THR_YIELD
;
60 this->what_
= TEST_LOCK
;
64 this->what_
= TEST_READLOCK
;
68 this->what_
= TEST_WRITELOCK
;
72 ACE_ERROR ((LM_ERROR
, "Invalid argument %c used\n", c
));
79 Baseline_Test_Base::yield ()
81 if (this->yield_method_
== Baseline_Test_Options::USE_SLEEP_ZERO
)
87 Baseline_Test_Options::Baseline_Test_Options ()
90 current_yield_method_ (0),
91 current_iteration_ (0),
92 total_iteration_ (DEFAULT_ITERATIONS
)
97 Baseline_Test_Options::parse_args (int argc
, ACE_TCHAR
*argv
[])
99 //FUZZ: disable check_for_lack_ACE_OS
100 ACE_Get_Opt
getopt (argc
, argv
, ACE_TEXT("tv"), 0);
103 while ((c
= getopt ()) != -1)
104 //FUZZ: enable check_for_lack_ACE_OS
108 this->test_try_lock_
= 1;
116 ACE_ERROR ((LM_ERROR
, "Invalid arguemnt %c used.\n", c
));
123 Baseline_Test_Options::reset_params (size_t iteration
,
126 this->current_iteration_
= 0;
127 this->timer
.reset ();
129 this->current_yield_method_
= yield
;
130 this->total_iteration_
= iteration
;
135 Baseline_Test_Options::print_result ()
140 this->timer
.elapsed_time_incr (tv
);
141 this->timer
.elapsed_time_incr (nsec
);
142 ACE_DEBUG ((LM_DEBUG
,
143 "Total Time: %d sec %d usec for a "
144 "total of %d iterations\n"
145 "Average time: %d nanoseconds.\n",
146 tv
.sec (), tv
.usec (),
147 this->current_iteration_
,
148 (int) (nsec
/ this->current_iteration_
)));
151 Baseline_Test::Baseline_Test ()
158 // Initialize and run the benchmarks tests.
161 Baseline_Test::init (int argc
, ACE_TCHAR
**argv
)
163 return baseline_options
.parse_args (argc
, argv
);
167 Baseline_Test::pre_run_test (Benchmark_Base
*bb
)
169 this->current_test_
= (Baseline_Test_Base
*) bb
;
170 baseline_options
.reset_params (this->current_test_
->iteration (),
171 this->current_test_
->yield_method ());
172 if (baseline_options
.test_try_lock ())
174 ACE_Thread_Manager::instance ()->spawn
175 (ACE_THR_FUNC (Baseline_Test::hold_lock
),
178 this->get_lock_
.wait ();
179 // Wait until the lock is held by the spawning thread.
186 Baseline_Test::run_test ()
188 if (baseline_options
.test_try_lock ())
189 return this->current_test_
->test_try_lock ();
191 return this->current_test_
->test_acquire_release ();
195 Baseline_Test::post_run_test ()
197 if (baseline_options
.test_try_lock ())
199 // Release the lock we hold.
200 this->let_go_lock_
.wait ();
202 ACE_Thread_Manager::instance ()->wait ();
205 baseline_options
.print_result ();
211 Baseline_Test::valid_test_object (Benchmark_Base
*bb
)
213 return (bb
->benchmark_type () == Benchmark_Base::BASELINE
);
217 Baseline_Test::hold_lock (void *arg
)
219 Baseline_Test
*this_test
= (Baseline_Test
*) arg
;
220 this_test
->current_test_
->acquire ();
221 this_test
->get_lock_
.wait ();
223 this_test
->let_go_lock_
.wait ();
227 ACE_SVC_FACTORY_DEFINE (Baseline_Test
)
229 #endif /* ACE_HAS_THREADS */