libjmmcg  release_579_6_g8cffd
A C++ library containing an eclectic mix of useful, advanced components.
hp_timer.cpp
Go to the documentation of this file.
1 /******************************************************************************
2 ** Copyright © 2010 by J.M.McGuiness, coder@hussar.me.uk
3 **
4 ** This library is free software; you can redistribute it and/or
5 ** modify it under the terms of the GNU Lesser General Public
6 ** License as published by the Free Software Foundation; either
7 ** version 2.1 of the License, or (at your option) any later version.
8 **
9 ** This library is distributed in the hope that it will be useful,
10 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
11 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 ** Lesser General Public License for more details.
13 **
14 ** You should have received a copy of the GNU Lesser General Public
15 ** License along with this library; if not, write to the Free Software
16 ** Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 */
18 
19 #include "stdafx.h"
20 
21 #define BOOST_TEST_MODULE libjmmcg_tests
22 #include <boost/test/included/unit_test.hpp>
23 
24 #include <boost/mpl/list.hpp>
25 
26 #include "core/thread_api_traits.hpp"
27 
28 using namespace libjmmcg;
29 
31 
32 BOOST_AUTO_TEST_SUITE(hp_timers)
33 
34 BOOST_AUTO_TEST_CASE(check_cpu_frequency) {
35  BOOST_CHECK_GT(cpu_timer::private_::start_ticks, 0);
36  BOOST_CHECK_GT(cpu_timer::private_::start_UTC.time_of_day().total_milliseconds(), 0);
37  BOOST_CHECK_GT(cpu_timer::private_::ticks_per_microsec.min, 0);
38  BOOST_CHECK_GT(cpu_timer::private_::ticks_per_microsec.mean, cpu_timer::private_::ticks_per_microsec.min);
39  BOOST_CHECK_GT(cpu_timer::private_::ticks_per_microsec.max, cpu_timer::private_::ticks_per_microsec.mean);
40  BOOST_CHECK_GE(cpu_timer::private_::ticks_per_microsec.mean_average_dev, 0);
41  BOOST_CHECK_LT(cpu_timer::private_::ticks_per_microsec.mean_average_dev, 1);
42  BOOST_CHECK_GT(cpu_timer::out_of_order::now(), 0);
43  BOOST_CHECK_GT(cpu_timer::in_order::now(), 0);
44 }
45 
46 BOOST_AUTO_TEST_CASE_TEMPLATE(create_timer, Mdl, thread_types) {
47  typedef hp_timer<ppd::platform_api,Mdl> hp_timer_t;
48 
49  hp_timer_t t;
50  BOOST_CHECK_GT(t.start_up_count, 0);
51  BOOST_CHECK_GT(t.to_usec(t.start_up_time), 0);
52 }
53 
54 BOOST_AUTO_TEST_CASE_TEMPLATE(check_timer_counter_counts, Mdl, thread_types) {
55  typedef hp_timer<ppd::platform_api,Mdl> hp_timer_t;
56 
57  hp_timer_t t;
58  BOOST_CHECK_GE(t.current_count(), t.start_up_count);
59  BOOST_CHECK_GE(t.current_count(), t.start_up_count);
60  BOOST_CHECK_GE(t.current_count(), t.start_up_count);
61  BOOST_CHECK_GE(t.current_count(), t.start_up_count);
62  BOOST_CHECK_GE(t.current_count(), t.start_up_count);
63  BOOST_CHECK_GE(t.current_count(), t.start_up_count);
64  BOOST_CHECK_GE(t.current_count(), t.start_up_count);
65 }
66 
67 BOOST_AUTO_TEST_CASE_TEMPLATE(check_timer_usec_simply_increments, Mdl, thread_types) {
68  typedef hp_timer<ppd::platform_api,Mdl> hp_timer_t;
69 
70  hp_timer_t t;
71  BOOST_CHECK_GE(t.to_usec(t.current_time()), t.start_up_count);
72  BOOST_CHECK_GE(t.to_usec(t.current_time()), t.start_up_count);
73  BOOST_CHECK_GE(t.to_usec(t.current_time()), t.start_up_count);
74 }
75 
76 BOOST_AUTO_TEST_CASE_TEMPLATE(check_timer_usec_increments, Mdl, thread_types) {
77  typedef hp_timer<ppd::platform_api,Mdl> hp_timer_t;
78 
79  hp_timer_t t;
80  const typename hp_timer_t::value_type t1=t.to_usec(t.current_time());
81  const typename hp_timer_t::value_type t2=t.to_usec(t.current_time());
82  BOOST_CHECK_GE(t2, t1);
83  const typename hp_timer_t::value_type t3=t.to_usec(t.current_time());
84  BOOST_CHECK_GE(t3, t2);
85  BOOST_CHECK_GT(t3, t1);
86 }
87 
88 BOOST_AUTO_TEST_SUITE_END()
89 
90 BOOST_AUTO_TEST_SUITE(hp_intervals)
91 
92 BOOST_AUTO_TEST_CASE_TEMPLATE(create_interval, Mdl, thread_types) {
93  typedef hp_timer<ppd::platform_api,Mdl> hp_timer_t;
94  typedef hp_interval<hp_timer_t> hp_interval_t;
95 
96  hp_timer_t timer;
97  typename hp_timer_t::time_utc_t interval;
98  hp_interval_t measure(timer,interval);
99  BOOST_CHECK_GE(measure.timer.to_usec(measure.start), measure.timer.start_up_count);
100 }
101 
102 BOOST_AUTO_TEST_CASE(measure_one_sec_thread_interval) {
103  typedef ppd::api_threading_traits<ppd::platform_api,ppd::heavyweight_threading> thread_traits;
104  typedef hp_timer<ppd::platform_api,ppd::heavyweight_threading> hp_timer_t;
105  typedef hp_interval<hp_timer_t> hp_interval_t;
106 
107  const thread_traits::api_params_type::suspend_period_ms one_sec=1000;
108  hp_timer_t timer;
109  hp_timer_t::time_utc_t interval;
110  {
111  hp_interval_t measure(timer,interval);
112  thread_traits::sleep(one_sec);
113  }
114  BOOST_CHECK_GT(timer.to_usec(interval), static_cast<hp_timer_t::value_type>(one_sec*0.9*1000));
115  BOOST_CHECK_LT(timer.to_usec(interval), static_cast<hp_timer_t::value_type>(one_sec*2*1000));
116 }
117 
118 BOOST_AUTO_TEST_CASE(measure_one_sec_seq_interval) {
119  typedef ppd::api_threading_traits<ppd::platform_api,ppd::sequential_mode> thread_traits;
120  typedef hp_timer<ppd::platform_api,ppd::sequential_mode> hp_timer_t;
121  typedef hp_interval<hp_timer_t> hp_interval_t;
122 
123  const thread_traits::api_params_type::suspend_period_ms one_sec=1000;
124  hp_timer_t timer;
125  hp_timer_t::time_utc_t interval;
126  {
127  const hp_interval_t measure(timer, interval);
128  thread_traits::sleep(one_sec);
129  }
130  BOOST_CHECK_GE(timer.to_usec(interval), 0);
131 }
132 
133 BOOST_AUTO_TEST_CASE(measure_one_sec_pause) {
134  using hp_timer_t=hp_timer<ppd::platform_api,ppd::sequential_mode>;
135  using hp_interval_t=hp_interval<hp_timer_t>;
136 
137  const double one_sec=1000000; // In usec.
138  hp_timer_t timer;
139  hp_timer_t::time_utc_t interval;
140  {
141  const hp_interval_t measure(timer, interval);
142  const auto paused=cpu_timer::pause_for_usec(one_sec);
143  BOOST_CHECK_GE(paused, one_sec);
144  }
145  BOOST_CHECK_GE(timer.to_usec(interval), 0.9*one_sec);
146 }
147 
148 BOOST_AUTO_TEST_SUITE_END()