| 1 | |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | #ifndef NATFW__BENCHMARK_H |
|---|
| 8 | #define NATFW__BENCHMARK_H |
|---|
| 9 | |
|---|
| 10 | #include <iostream> |
|---|
| 11 | #include <string> |
|---|
| 12 | #include <ctime> |
|---|
| 13 | |
|---|
| 14 | |
|---|
| 15 | namespace natfw { |
|---|
| 16 | |
|---|
| 17 | #ifdef BENCHMARK |
|---|
| 18 | #define MP(mp_id) journal.add(mp_id) |
|---|
| 19 | #else |
|---|
| 20 | #define MP(mp_id) |
|---|
| 21 | #endif |
|---|
| 22 | |
|---|
| 23 | |
|---|
| 24 | |
|---|
| 25 | |
|---|
| 26 | |
|---|
| 27 | |
|---|
| 28 | |
|---|
| 29 | class benchmark { |
|---|
| 30 | public: |
|---|
| 31 | |
|---|
| 32 | |
|---|
| 33 | |
|---|
| 34 | |
|---|
| 35 | |
|---|
| 36 | |
|---|
| 37 | enum measuring_point_id_t { |
|---|
| 38 | INVALID_ID = 0, |
|---|
| 39 | PRE_PROCESSING = 1, |
|---|
| 40 | POST_PROCESSING = 2, |
|---|
| 41 | PRE_MAPPING = 3, |
|---|
| 42 | POST_MAPPING = 4, |
|---|
| 43 | PRE_PARSER = 5, |
|---|
| 44 | POST_PARSER = 6, |
|---|
| 45 | PRE_DISPATCHER = 7, |
|---|
| 46 | POST_DISPATCHER = 8, |
|---|
| 47 | PRE_SESSION = 9, |
|---|
| 48 | POST_SESSION = 10, |
|---|
| 49 | HIGHEST_VALID_ID = 10 |
|---|
| 50 | }; |
|---|
| 51 | |
|---|
| 52 | benchmark(int journal_size, const std::string &filename=""); |
|---|
| 53 | ~benchmark(); |
|---|
| 54 | |
|---|
| 55 | void add(measuring_point_id_t mp_id); |
|---|
| 56 | |
|---|
| 57 | void write_journal(const std::string &filename); |
|---|
| 58 | void write_journal(std::ostream &out); |
|---|
| 59 | |
|---|
| 60 | private: |
|---|
| 61 | struct measuring_point_t { |
|---|
| 62 | measuring_point_id_t point; |
|---|
| 63 | pthread_t thread_id; |
|---|
| 64 | struct timespec timestamp; |
|---|
| 65 | }; |
|---|
| 66 | |
|---|
| 67 | |
|---|
| 68 | |
|---|
| 69 | |
|---|
| 70 | |
|---|
| 71 | int journal_size; |
|---|
| 72 | int journal_pos; |
|---|
| 73 | measuring_point_t *journal; |
|---|
| 74 | |
|---|
| 75 | pthread_mutex_t mutex; |
|---|
| 76 | |
|---|
| 77 | std::string filename; |
|---|
| 78 | bool disable_journal; |
|---|
| 79 | |
|---|
| 80 | static const char *mp_names[HIGHEST_VALID_ID+1]; |
|---|
| 81 | |
|---|
| 82 | static void write_header(std::ostream &out); |
|---|
| 83 | }; |
|---|
| 84 | |
|---|
| 85 | |
|---|
| 86 | inline void benchmark::add(measuring_point_id_t mp_id) { |
|---|
| 87 | pthread_mutex_lock(&mutex); |
|---|
| 88 | |
|---|
| 89 | if ( journal_pos < journal_size ) { |
|---|
| 90 | struct timespec res; |
|---|
| 91 | clock_gettime(CLOCK_REALTIME, &res); |
|---|
| 92 | |
|---|
| 93 | struct measuring_point_t mp = { mp_id, pthread_self(), res }; |
|---|
| 94 | journal[journal_pos++] = mp; |
|---|
| 95 | } |
|---|
| 96 | else if ( disable_journal == false && journal_pos == journal_size ) { |
|---|
| 97 | std::cerr << "*** benchmark journal is full ***" << std::endl; |
|---|
| 98 | |
|---|
| 99 | if ( filename != "" ) { |
|---|
| 100 | std::cerr << "writing journal to file " |
|---|
| 101 | << filename << " ..." << std::endl; |
|---|
| 102 | write_journal(filename); |
|---|
| 103 | std::cerr << "done." << std::endl; |
|---|
| 104 | } |
|---|
| 105 | |
|---|
| 106 | disable_journal = true; |
|---|
| 107 | } |
|---|
| 108 | |
|---|
| 109 | pthread_mutex_unlock(&mutex); |
|---|
| 110 | } |
|---|
| 111 | |
|---|
| 112 | |
|---|
| 113 | } |
|---|
| 114 | |
|---|
| 115 | |
|---|
| 116 | #endif // NATFW__BENCHMARK_H |
|---|