NSIS-ka
A free C++ implementation of NSIS protocols

root/natfw-nslp/trunk/include/benchmark.h @ 2252

Revision 2252, 2.3 KB (checked in by stud-matfried, 7 years ago)

Added more measuring points for benchmarking.
Renumbered the existing points.

  • Property svn:keywords set to Id HeadURL
Line 
1/*
2 * Tools for running benchmarks.
3 *
4 * $Id$
5 * $HeadURL$
6 */
7#ifndef NATFW__BENCHMARK_H
8#define NATFW__BENCHMARK_H
9
10#include <iostream>
11#include <string>
12#include <ctime>
13
14
15namespace 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 * A class for running benchmarks on the NATFW implementation.
25 *
26 * Only one instance of this class is needed. All threads share a common
27 * journal.
28 */
29class benchmark {
30  public:
31        /**
32         * Each measuring point has an ID on its own.
33         *
34         * Note: When adding or changing measuring points, please also adjust
35         *       the mp_names array in benchmark.cpp.
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         * The journal is an array of measuring_point entries. The next entry
69         * has to be written to journal[journal_pos].
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
86inline 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} // namespace natfw
114
115
116#endif // NATFW__BENCHMARK_H
Note: See TracBrowser for help on using the browser.