snooze.cpp
Go to the documentation of this file.
00001 
00008 /*****************************************************************************
00009 ** Includes
00010 *****************************************************************************/
00011 
00012 #include <iostream>
00013 #include <cmath>
00014 #include <ecl/threads/priority.hpp>
00015 #include <ecl/time_lite/functions.hpp>
00016 #include <ecl/time/snooze.hpp>
00017 #include <ecl/time/timestamp.hpp>
00018 
00019 /*****************************************************************************
00020 ** Namespaces
00021 *****************************************************************************/
00022 
00023 namespace ecl {
00024 namespace benchmarks {
00025 
00026 /*****************************************************************************
00027 ** Using
00028 *****************************************************************************/
00029 
00030 using ecl::Duration;
00031 using ecl::Snooze;
00032 using ecl::StandardException;
00033 
00034 /*****************************************************************************
00035 ** Classes
00036 *****************************************************************************/
00037 
00038 class LogSnooze : public Snooze {
00039 
00040 public:
00041         LogSnooze(const long &ms) : Snooze(Duration(0,ms*1000000L)), count(0), avg(0.0), avg_noise(0.0), max(0), period_ms(ms) {};
00042 
00043         // Need to run this at least 10 times before the average is close
00044         // to correct.
00045         void log() {
00046                 ecl::epoch_time(time_actual);
00047                 latency = (time_actual.tv_nsec - time_value.tv_nsec)/1000000.0; // convert to ms
00048                 if ( count == 0 ) {
00049                         avg = latency;
00050                 } else {
00051                         avg = 0.9*avg + 0.1*latency;
00052                         avg_noise = 0.9*avg_noise + 0.1*fabs(latency-avg);
00053                 }
00054                 if ( latency > max ) { max = latency; }
00055                 ++count;
00056         }
00057 
00058         void printLog() {
00059                 std::cout << "Loop period [ms]:     " << period_ms << std::endl;
00060                 std::cout << "Average latency [ms]: " << avg << std::endl;
00061                 std::cout << "Maximum latency [ms]: " << max << std::endl;
00062         }
00063 
00064 private:
00065         /*********************
00066         ** Logging Variables
00067         **********************/
00068         TimeStructure time_actual;
00069         double latency;
00070         int count;
00071         double avg,avg_noise;
00072         double max;
00073         long period_ms;
00074 };
00075 
00076 
00077 } // namespace benchmarks
00078 } // namespace ecl
00079 
00080 /*****************************************************************************
00081 ** Using
00082 *****************************************************************************/
00083 
00084 using namespace ecl::benchmarks;
00085 
00086 /*****************************************************************************
00087 ** Main program
00088 *****************************************************************************/
00089 
00090 int main() {
00091 
00092         try {
00093                 ecl::set_priority(ecl::RealTimePriority4);
00094         } catch ( StandardException &e ) {
00095                 // dont worry about it.
00096         }
00097 
00098     std::cout << std::endl;
00099     std::cout << "***********************************************************" << std::endl;
00100     std::cout << "                   Snooze Latency" << std::endl;
00101     std::cout << "***********************************************************" << std::endl;
00102     std::cout << "* " << std::endl;
00103     std::cout << "* Latency is the absolute time behind where it should be." << std::endl;
00104     std::cout << "* This is only a factor for the first snooze loop." << std::endl;
00105     std::cout << "*" << std::endl;
00106     std::cout << "* Latency noise is more important. After the first loop," << std::endl;
00107     std::cout << "* with an ideal noise value of zero, the loop will time" << std::endl;
00108     std::cout << "* exactly as you specify. Practically, this is not the case," << std::endl;
00109     std::cout << "* but you should get good results from this without having" << std::endl;
00110     std::cout << "* to use system timers. Use with loops which need to be" << std::endl;
00111     std::cout << "* 5ms or over. Faster loops really require a RTOS." << std::endl;
00112     std::cout << "* " << std::endl;
00113     std::cout << "*****************************************************************" << std::endl;
00114     std::cout << std::endl;
00115     std::cout << "Looping commencing..." << std::endl;
00116 
00117     LogSnooze snooze(30);
00118     snooze.initialise(); // Get it ready to start looping.
00119     for(int i = 0; i < 100; ++i ) {
00120         snooze();
00121         snooze.log();
00122     }
00123     snooze.printLog();
00124 
00125     std::cout << std::endl;
00126     std::cout << "***********************************************************" << std::endl;
00127     std::cout << "                      Passed" << std::endl;
00128     std::cout << "***********************************************************" << std::endl;
00129     std::cout << std::endl;
00130 
00131     return 0;
00132 }
00133 
00134 


ecl_core_apps
Author(s): Daniel Stonier
autogenerated on Mon Jul 3 2017 02:22:13