snooze.cpp
Go to the documentation of this file.
1 
8 /*****************************************************************************
9 ** Includes
10 *****************************************************************************/
11 
12 #include <iostream>
13 #include <cmath>
14 #include <ecl/threads/priority.hpp>
16 #include <ecl/time/snooze.hpp>
17 #include <ecl/time/timestamp.hpp>
18 
19 /*****************************************************************************
20 ** Namespaces
21 *****************************************************************************/
22 
23 namespace ecl {
24 namespace benchmarks {
25 
26 /*****************************************************************************
27 ** Using
28 *****************************************************************************/
29 
30 using ecl::Duration;
31 using ecl::Snooze;
33 
34 /*****************************************************************************
35 ** Classes
36 *****************************************************************************/
37 
38 class LogSnooze : public Snooze {
39 
40 public:
41  LogSnooze(const long &ms) : Snooze(Duration(0,ms*1000000L)), count(0), avg(0.0), avg_noise(0.0), max(0), period_ms(ms) {};
42 
43  // Need to run this at least 10 times before the average is close
44  // to correct.
45  void log() {
46  ecl::epoch_time(time_actual);
47  latency = (time_actual.tv_nsec - time_value.tv_nsec)/1000000.0; // convert to ms
48  if ( count == 0 ) {
49  avg = latency;
50  } else {
51  avg = 0.9*avg + 0.1*latency;
52  avg_noise = 0.9*avg_noise + 0.1*fabs(latency-avg);
53  }
54  if ( latency > max ) { max = latency; }
55  ++count;
56  }
57 
58  void printLog() {
59  std::cout << "Loop period [ms]: " << period_ms << std::endl;
60  std::cout << "Average latency [ms]: " << avg << std::endl;
61  std::cout << "Maximum latency [ms]: " << max << std::endl;
62  }
63 
64 private:
65  /*********************
66  ** Logging Variables
67  **********************/
68  TimeStructure time_actual;
69  double latency;
70  int count;
71  double avg,avg_noise;
72  double max;
73  long period_ms;
74 };
75 
76 
77 } // namespace benchmarks
78 } // namespace ecl
79 
80 /*****************************************************************************
81 ** Using
82 *****************************************************************************/
83 
84 using namespace ecl::benchmarks;
85 
86 /*****************************************************************************
87 ** Main program
88 *****************************************************************************/
89 
90 int main() {
91 
92  try {
93  ecl::set_priority(ecl::RealTimePriority4);
94  } catch ( StandardException &e ) {
95  // dont worry about it.
96  }
97 
98  std::cout << std::endl;
99  std::cout << "***********************************************************" << std::endl;
100  std::cout << " Snooze Latency" << std::endl;
101  std::cout << "***********************************************************" << std::endl;
102  std::cout << "* " << std::endl;
103  std::cout << "* Latency is the absolute time behind where it should be." << std::endl;
104  std::cout << "* This is only a factor for the first snooze loop." << std::endl;
105  std::cout << "*" << std::endl;
106  std::cout << "* Latency noise is more important. After the first loop," << std::endl;
107  std::cout << "* with an ideal noise value of zero, the loop will time" << std::endl;
108  std::cout << "* exactly as you specify. Practically, this is not the case," << std::endl;
109  std::cout << "* but you should get good results from this without having" << std::endl;
110  std::cout << "* to use system timers. Use with loops which need to be" << std::endl;
111  std::cout << "* 5ms or over. Faster loops really require a RTOS." << std::endl;
112  std::cout << "* " << std::endl;
113  std::cout << "*****************************************************************" << std::endl;
114  std::cout << std::endl;
115  std::cout << "Looping commencing..." << std::endl;
116 
117  LogSnooze snooze(30);
118  snooze.initialise(); // Get it ready to start looping.
119  for(int i = 0; i < 100; ++i ) {
120  snooze();
121  snooze.log();
122  }
123  snooze.printLog();
124 
125  std::cout << std::endl;
126  std::cout << "***********************************************************" << std::endl;
127  std::cout << " Passed" << std::endl;
128  std::cout << "***********************************************************" << std::endl;
129  std::cout << std::endl;
130 
131  return 0;
132 }
133 
134 
TimeStructure time_actual
Definition: snooze.cpp:68
int main()
Definition: snooze.cpp:90
RealTimePriority4
LogSnooze(const long &ms)
Definition: snooze.cpp:41
TimeStamp Duration
Benchmark tests.
Definition: namespaces.dox:2


ecl_core_apps
Author(s): Daniel Stonier
autogenerated on Mon Feb 28 2022 22:19:02