1 /* Copyright (c) 2003, 2018, Oracle and/or its affiliates. All rights reserved.
2 
3    This program is free software; you can redistribute it and/or modify
4    it under the terms of the GNU General Public License, version 2.0,
5    as published by the Free Software Foundation.
6 
7    This program is also distributed with certain software (including
8    but not limited to OpenSSL) that is licensed under separate terms,
9    as designated in a particular file or component or in included license
10    documentation.  The authors of MySQL hereby grant you an additional
11    permission to link the program and your derivative works with the
12    separately licensed software that they have included with MySQL.
13 
14    This program is distributed in the hope that it will be useful,
15    but WITHOUT ANY WARRANTY; without even the implied warranty of
16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17    GNU General Public License, version 2.0, for more details.
18 
19    You should have received a copy of the GNU General Public License
20    along with this program; if not, write to the Free Software
21    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA
22 */
23 
24 #ifndef NDBTIMER_H
25 #define NDBTIMER_H
26 
27 #include <NdbTick.h>
28 #include <NdbOut.hpp>
29 
30 //
31 // Class used for measuring time and printing the results
32 //
33 // Currently measures time in milliseconds
34 //
35 
36 class NdbTimer
37 {
38 public:
39 
40   NdbTimer();
~NdbTimer()41   ~NdbTimer() {}
42 
43   void doStart();
44   void doStop();
45   void doReset();
46   Uint64 elapsedTime() const;
47   void printTransactionStatistics(const char* text,
48 				  int numTransactions,
49 				  int numOperations);
50   void printTestTimer(int numLoops,
51 		      int numRecords);
52   void printTotalTime(void);
53 private:
54   NDB_TICKS startTicks;
55   NDB_TICKS stopTicks;
56 };
57 
NdbTimer()58 inline NdbTimer::NdbTimer(){
59   doReset();
60 }
61 
doReset(void)62 inline void NdbTimer::doReset(void){
63   NdbTick_Invalidate(&startTicks);
64   NdbTick_Invalidate(&stopTicks);
65 }
66 
doStart(void)67 inline void NdbTimer::doStart(void){
68   startTicks = NdbTick_getCurrentTicks();
69 }
70 
doStop(void)71 inline void NdbTimer::doStop(void){
72   stopTicks = NdbTick_getCurrentTicks();
73 }
74 
elapsedTime(void) const75 inline Uint64 NdbTimer::elapsedTime(void) const {
76   return NdbTick_Elapsed(startTicks,stopTicks).milliSec();
77 }
78 
printTransactionStatistics(const char * text,int numTransactions,int numOperations)79 inline void NdbTimer::printTransactionStatistics(const char* text,
80 						 int numTransactions,
81 						 int numOperations){
82 
83   // Convert to Uint32 in order to be able to print it to screen
84   Uint32 lapTime = (Uint32)elapsedTime();
85   ndbout_c("%i transactions, %i %s total time = %d ms\nAverage %f ms/transaction, %f ms/%s.\n%f transactions/second, %f %ss/second.\n",
86 	 numTransactions, numTransactions*numOperations, text, lapTime,
87          ((double)lapTime/numTransactions), ((double)lapTime/(numTransactions*numOperations)), text,
88          1000.0/((double)lapTime/numOperations), 1000.0/((double)lapTime/(numTransactions*numOperations)), text);
89 }
90 
91 
92 
printTestTimer(int numLoops,int numRecords)93 inline void NdbTimer::printTestTimer(int numLoops,
94 				     int numRecords){
95   // Convert to Uint32 in order to be able to print it to screen
96   Uint32 lapTime = (Uint32)elapsedTime();
97   ndbout_c("%i loop * %i records, total time = %d ms\nAverage %f ms/loop, %f ms/record.\n%f looop/second, %f records/second.\n",
98 	   numLoops, numRecords, lapTime,
99 	   ((double)lapTime/numLoops), ((double)lapTime/(numLoops*numRecords)),
100 	   1000.0/((double)lapTime/numLoops), 1000.0/((double)lapTime/(numLoops*numRecords)));
101 }
102 
103 
printTotalTime(void)104 inline void NdbTimer::printTotalTime(void){
105   // Convert to Uint32 in order to be able to print it to screen
106   Uint32 lapTime = (Uint32)elapsedTime();
107   Uint32 secTime = lapTime/1000;
108   ndbout_c("Total time : %d seconds (%d ms)\n", secTime, lapTime);
109 }
110 
111 
112 
113 
114 
115 
116 #endif
117