Alchemy  1.0
A framework to robustly process network messages and structured data
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
benchmark_util.h
Go to the documentation of this file.
1 /// @file benchmark_util.h
2 ///
3 /// Collection of utility functions to simplify benchmark tests.
4 ///
5 /// The MIT License(MIT)
6 /// @copyright 2014 Paul M Watt
7 // ****************************************************************************
8 #ifndef BENCHMARK_UTIL_H_INCLUDED
9 #define BENCHMARK_UTIL_H_INCLUDED
10 // Includes *******************************************************************
11 #include <algorithm>
12 
13 
14 #ifdef WIN32
15 
16 #include <windows.h>
17 
18 namespace alchemy
19 {
20 namespace benchmark
21 {
22 
23 inline
24 double get_time()
25 {
26  LARGE_INTEGER t, f;
27  QueryPerformanceCounter(&t);
28  QueryPerformanceFrequency(&f);
29  return (double)t.QuadPart/(double)f.QuadPart;
30 }
31 
32 } // benchmark
33 } // alchemy
34 
35 #else
36 
37 #include <sys/time.h>
38 #include <sys/resource.h>
39 
40 namespace alchemy
41 {
42 namespace benchmark
43 {
44 
45 inline
46 double get_time()
47 {
48  struct timeval t;
49  struct timezone tzp;
50  gettimeofday(&t, &tzp);
51  return t.tv_sec + t.tv_usec*1e-6;
52 }
53 
54 } // benchmark
55 } // alchemy
56 
57 #endif
58 
59 
60 namespace alchemy
61 {
62 namespace benchmark
63 {
64 
65 // ****************************************************************************
66 class DataBuffer
67 {
68 public:
69 
70  // **************************************************************************
71  DataBuffer()
72  : m_pBuffer(0)
73  , m_size(0)
74  , m_pCurrent(0)
75  { }
76 
77  // **************************************************************************
78  DataBuffer(size_t len)
79  : m_pBuffer(new char[len])
80  , m_size(len)
81  , m_pCurrent(m_pBuffer)
82  { }
83 
84  // **************************************************************************
85  void Init(size_t size)
86  {
87  double seed = get_time();
88  srand((unsigned int)seed);
89 
90  // Attempt to allocate the requested amount of memory.
91  Term();
92 
93  m_pBuffer = new char[size];
94  m_size = size;
95 
96  for (size_t index = 0; index < size; ++index)
97  {
98  char item = char(rand() % 256);
99  m_pBuffer[index] = item;
100  }
101  }
102 
103  // **************************************************************************
104  void Term()
105  {
106  delete[] m_pBuffer;
107  m_pBuffer = 0;
108  m_size = 0;
109  m_pCurrent = 0;
110  }
111 
112  // **************************************************************************
113  size_t Size() const
114  {
115  return m_size;
116  }
117 
118  // **************************************************************************
119  void* GetBytes(size_t count)
120  {
121  void* pRetVal = (void*)(m_pCurrent);
122  std::advance(m_pCurrent, count);
123 
124  return pRetVal;
125  }
126 
127  // **************************************************************************
128  void Reset()
129  {
130  if (!Size())
131  {
132  return;
133  }
134 
135  m_pCurrent = m_pBuffer;
136  }
137 
138  // **************************************************************************
139  void Restart()
140  {
141  m_pCurrent = m_pBuffer;
142  }
143 
144  // **************************************************************************
145  int Value()
146  {
147  size_t index = m_pBuffer[0];
148  return m_pBuffer[index];
149  }
150 
151 private:
152  size_t m_size;
153  char* m_pBuffer;
154  char* m_pCurrent;
155 
156 };
157 
158 } // benchmark
159 } // alchemy
160 
161 
162 
163 
164 #endif