Page Speed Optimization Libraries  1.5.27.2
net/instaweb/util/public/statistics.h
Go to the documentation of this file.
00001 /*
00002  * Copyright 2010 Google Inc.
00003  *
00004  * Licensed under the Apache License, Version 2.0 (the "License");
00005  * you may not use this file except in compliance with the License.
00006  * You may obtain a copy of the License at
00007  *
00008  *      http:///www.apache.org/licenses/LICENSE-2.0
00009  *
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS,
00012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  * See the License for the specific language governing permissions and
00014  * limitations under the License.
00015  */
00016 
00018 
00019 #ifndef NET_INSTAWEB_UTIL_PUBLIC_STATISTICS_H_
00020 #define NET_INSTAWEB_UTIL_PUBLIC_STATISTICS_H_
00021 
00022 #include <map>
00023 #include <set>
00024 #include "base/logging.h"
00025 #include "net/instaweb/util/public/abstract_mutex.h"
00026 #include "net/instaweb/util/public/basictypes.h"
00027 #include "net/instaweb/util/public/null_mutex.h"
00028 #include "net/instaweb/util/public/string.h"
00029 #include "net/instaweb/util/public/string_util.h"
00030 
00031 namespace net_instaweb {
00032 
00033 class MessageHandler;
00034 class Writer;
00035 
00036 class Variable {
00037  public:
00038   virtual ~Variable();
00039 
00048   virtual int64 SetReturningPreviousValue(int64 value);
00049 
00050   virtual void Set(int64 value) = 0;
00051   virtual int64 Get() const = 0;
00054   virtual StringPiece GetName() const = 0;
00055 
00058   virtual int64 Add(int delta) {
00059     int64 value = Get() + delta;
00060     Set(value);
00061     return value;
00062   }
00063 
00064   void Clear() { Set(0); }
00065 };
00066 
00068 class ConsoleStatisticsLogger {
00069  public:
00070   virtual ~ConsoleStatisticsLogger();
00073   virtual void DumpJSON(const std::set<GoogleString>& var_titles,
00074                         int64 start_time, int64 end_time, int64 granularity_ms,
00075                         Writer* writer,
00076                         MessageHandler* message_handler) const = 0;
00077 };
00078 
00079 class Histogram {
00080  public:
00081   virtual ~Histogram();
00083   virtual void Add(double value) = 0;
00085   virtual void Clear() = 0;
00087   bool Empty() {
00088     ScopedMutex hold(lock());
00089     return CountInternal() == 0;
00090   }
00102   virtual void Render(int index, Writer* writer, MessageHandler* handler);
00103 
00105   virtual int NumBuckets() = 0;
00107   virtual void EnableNegativeBuckets() = 0;
00109   virtual void SetMinValue(double value) = 0;
00113   virtual void SetMaxValue(double value) = 0;
00114 
00117   virtual void SetSuggestedNumBuckets(int i) = 0;
00118 
00120   double Average() {
00121     ScopedMutex hold(lock());
00122     return AverageInternal();
00123   }
00127   double Percentile(const double perc) {
00128     ScopedMutex hold(lock());
00129     return PercentileInternal(perc);
00130   }
00131   double StandardDeviation() {
00132     ScopedMutex hold(lock());
00133     return StandardDeviationInternal();
00134   }
00135   double Count() {
00136     ScopedMutex hold(lock());
00137     return CountInternal();
00138   }
00139   double Maximum() {
00140     ScopedMutex hold(lock());
00141     return MaximumInternal();
00142   }
00143   double Minimum() {
00144     ScopedMutex hold(lock());
00145     return MinimumInternal();
00146   }
00147   double Median() {
00148     return Percentile(50);
00149   }
00150 
00157   GoogleString HtmlTableRow(const GoogleString& title, int index);
00158 
00162   virtual double BucketStart(int index) = 0;
00164   virtual double BucketLimit(int index) {
00165     return BucketStart(index + 1);
00166   }
00168   virtual double BucketCount(int index) = 0;
00169 
00170  protected:
00171   Histogram() {}
00172 
00174   virtual double AverageInternal() = 0;
00175   virtual double PercentileInternal(const double perc) = 0;
00176   virtual double StandardDeviationInternal() = 0;
00177   virtual double CountInternal() = 0;
00178   virtual double MaximumInternal() = 0;
00179   virtual double MinimumInternal() = 0;
00180 
00181   virtual AbstractMutex* lock() = 0;
00182 
00189   void WriteRawHistogramData(Writer* writer, MessageHandler* handler);
00190 
00191  private:
00192   DISALLOW_COPY_AND_ASSIGN(Histogram);
00193 };
00194 
00196 class CountHistogram : public Histogram {
00197  public:
00198   CountHistogram() : count_(0) {}
00199   virtual ~CountHistogram();
00200   virtual void Add(const double value) {
00201     ScopedMutex hold(lock());
00202     ++count_;
00203   }
00204   virtual void Clear() {
00205     ScopedMutex hold(lock());
00206     count_ = 0;
00207   }
00208   virtual int NumBuckets() { return 0; }
00209   virtual void EnableNegativeBuckets() { }
00210   virtual void SetMinValue(double value) { }
00211   virtual void SetMaxValue(double value) { }
00212   virtual void SetSuggestedNumBuckets(int i) { }
00213   virtual GoogleString GetName() const { return ""; }
00214 
00215  protected:
00216   virtual AbstractMutex* lock() { return &mutex_; }
00217   virtual double AverageInternal() { return 0.0; }
00218   virtual double PercentileInternal(const double perc) { return 0.0; }
00219   virtual double StandardDeviationInternal() { return 0.0; }
00220   virtual double CountInternal() { return count_; }
00221   virtual double MaximumInternal() { return 0.0; }
00222   virtual double MinimumInternal() { return 0.0; }
00223   virtual double BucketStart(int index) { return 0.0; }
00224   virtual double BucketCount(int index) { return 0.0; }
00225 
00226  private:
00227   NullMutex mutex_;
00228   int count_;
00229 
00230   DISALLOW_COPY_AND_ASSIGN(CountHistogram);
00231 };
00232 
00236 class TimedVariable {
00237  public:
00239   enum Levels { TENSEC, MINUTE, HOUR, START };
00240   virtual ~TimedVariable();
00242   virtual void IncBy(int64 delta) = 0;
00245   virtual int64 Get(int level) = 0;
00247   virtual void Clear() = 0;
00248 };
00249 
00251 class FakeTimedVariable : public TimedVariable {
00252  public:
00253   explicit FakeTimedVariable(Variable* var) : var_(var) {
00254   }
00255   virtual ~FakeTimedVariable();
00257   virtual void IncBy(int64 delta) {
00258     var_->Add(delta);
00259   }
00262   virtual int64 Get(int level) {
00266     if (level == START) {
00267       return var_->Get();
00268     }
00269     return 0;
00270   }
00272   virtual void Clear() {
00273     return var_->Clear();
00274   }
00275 
00276  protected:
00277   Variable* var_;
00278 };
00279 
00281 class Statistics {
00282  public:
00283   virtual ~Statistics();
00284 
00288   virtual Variable* AddVariable(const StringPiece& name) = 0;
00289 
00293   virtual Variable* AddGlobalVariable(const StringPiece& name);
00294 
00296   virtual Variable* FindVariable(const StringPiece& name) const = 0;
00297 
00299   Variable* GetVariable(const StringPiece& name) const {
00300     Variable* var = FindVariable(name);
00301     CHECK(var != NULL) << "Variable not found: " << name;
00302     return var;
00303   }
00304 
00308   virtual Histogram* AddHistogram(const StringPiece& name) = 0;
00310   virtual Histogram* FindHistogram(const StringPiece& name) const = 0;
00312   Histogram* GetHistogram(const StringPiece& name) const {
00313     Histogram* hist = FindHistogram(name);
00314     CHECK(hist != NULL) << "Histogram not found: " << name;
00315     return hist;
00316   }
00317 
00322   virtual TimedVariable* AddTimedVariable(
00323       const StringPiece& name, const StringPiece& group) = 0;
00325   virtual TimedVariable* FindTimedVariable(
00326       const StringPiece& name) const = 0;
00328   TimedVariable* GetTimedVariable(
00329       const StringPiece& name) const {
00330     TimedVariable* stat = FindTimedVariable(name);
00331     CHECK(stat != NULL) << "TimedVariable not found: " << name;
00332     return stat;
00333   }
00335   virtual const StringVector& HistogramNames() = 0;
00337   virtual const std::map<GoogleString, StringVector>& TimedVariableMap() = 0;
00339   virtual void Dump(Writer* writer, MessageHandler* handler) = 0;
00347   virtual void DumpConsoleVarsToWriter(
00348       int64 current_time_ms, Writer* writer, MessageHandler* message_handler) {}
00349   virtual void RenderTimedVariables(Writer* writer,
00350                                     MessageHandler* handler);
00352   virtual void RenderHistograms(Writer* writer, MessageHandler* handler);
00355   virtual void Clear() = 0;
00356 
00361   virtual ConsoleStatisticsLogger* console_logger() { return NULL; }
00362 
00363  protected:
00365   FakeTimedVariable* NewFakeTimedVariable(const StringPiece& name, int index);
00366 };
00367 
00368 }  
00369 
00370 #endif  ///< NET_INSTAWEB_UTIL_PUBLIC_STATISTICS_H_
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines