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