MySQL 5.6.14 Source Code Document
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
NDBT_Stats.hpp
1 /*
2  Copyright (C) 2003-2006 MySQL AB, 2009 Sun Microsystems, Inc.
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 as published by
7  the Free Software Foundation; version 2 of the License.
8 
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  GNU General Public License for more details.
13 
14  You should have received a copy of the GNU General Public License
15  along with this program; if not, write to the Free Software
16  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17 */
18 
19 #ifndef NDBT_STATS_HPP
20 #define NDBT_STATS_HPP
21 
22 #include <ndb_global.h>
23 
24 class NDBT_Stats {
25 public:
26  NDBT_Stats() { reset(); }
27 
28  void reset() { sum = sum2 = 0.0; max = DBL_MIN; ; min = DBL_MAX; n = 0;}
29 
30  void addObservation(double t) {
31  sum+= t;
32  sum2 += (t*t);
33  n++;
34  if(min > t) min = t;
35  if(max < t) max = t;
36  }
37 
38  void addObservation(Uint64 t) { addObservation(double(t)); }
39 
40  double getMean() const { return sum/n;}
41  double getStddev() const { return sqrt(getVariance()); }
42  double getVariance() const { return (n*sum2 - (sum*sum))/(n*n);}
43  double getMin() const { return min;}
44  double getMax() const { return max;}
45  int getCount() const { return n;}
46 
47  NDBT_Stats & operator+=(const NDBT_Stats & c){
48  sum += c.sum;
49  sum2 += c.sum2;
50  n += c.n;
51  if(min > c.min) min = c.min;
52  if(max < c.max) max = c.max;
53  return * this;
54  }
55 private:
56  double sum;
57  double sum2;
58  int n;
59  double min, max;
60 };
61 
62 inline
63 double
64 NDB_SQRT(double x){
65  assert(x >= 0);
66 
67  double y = 0;
68  double s = 1;
69  double r = 0;
70  while(y <= x){
71  y += s;
72  s += 2;
73  r += 1;
74  }
75  return r - 1;
76 }
77 
78 #endif