Page Speed Optimization Libraries  1.3.25.1
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 Timer;
00035 class Writer;
00036 
00037 class Variable {
00038  public:
00039   virtual ~Variable();
00040 
00049   virtual int64 SetReturningPreviousValue(int64 value);
00050 
00051   virtual void Set(int64 value) = 0;
00052   virtual int64 Get() const = 0;
00055   virtual StringPiece GetName() const = 0;
00056 
00059   virtual int64 Add(int delta) {
00060     int64 value = Get() + delta;
00061     Set(value);
00062     return value;
00063   }
00064 
00065   void Clear() { Set(0); }
00066 };
00067 
00069 class ConsoleStatisticsLogger {
00070  public:
00071   virtual ~ConsoleStatisticsLogger();
00074   virtual void UpdateAndDumpIfRequired() = 0;
00077   virtual void DumpJSON(const std::set<GoogleString>& var_titles,
00078                         const std::set<GoogleString>& hist_titles,
00079                         int64 startTime, int64 endTime, int64 granularity_ms,
00080                         Writer* writer,
00081                         MessageHandler* message_handler) const = 0;
00082   virtual Timer* timer() = 0;
00083 };
00084 
00085 class Histogram {
00086  public:
00087   virtual ~Histogram();
00089   virtual void Add(double value) = 0;
00091   virtual void Clear() = 0;
00093   bool Empty() {
00094     ScopedMutex hold(lock());
00095     return CountInternal() == 0;
00096   }
00108   virtual void Render(int index, Writer* writer, MessageHandler* handler);
00109 
00111   virtual int NumBuckets() = 0;
00113   virtual void EnableNegativeBuckets() = 0;
00115   virtual void SetMinValue(double value) = 0;
00119   virtual void SetMaxValue(double value) = 0;
00120 
00123   virtual void SetSuggestedNumBuckets(int i) = 0;
00124 
00126   double Average() {
00127     ScopedMutex hold(lock());
00128     return AverageInternal();
00129   }
00133   double Percentile(const double perc) {
00134     ScopedMutex hold(lock());
00135     return PercentileInternal(perc);
00136   }
00137   double StandardDeviation() {
00138     ScopedMutex hold(lock());
00139     return StandardDeviationInternal();
00140   }
00141   double Count() {
00142     ScopedMutex hold(lock());
00143     return CountInternal();
00144   }
00145   double Maximum() {
00146     ScopedMutex hold(lock());
00147     return MaximumInternal();
00148   }
00149   double Minimum() {
00150     ScopedMutex hold(lock());
00151     return MinimumInternal();
00152   }
00153   double Median() {
00154     return Percentile(50);
00155   }
00156 
00163   GoogleString HtmlTableRow(const GoogleString& title, int index);
00164 
00168   virtual double BucketStart(int index) = 0;
00170   virtual double BucketLimit(int index) {
00171     return BucketStart(index + 1);
00172   }
00174   virtual double BucketCount(int index) = 0;
00175 
00176  protected:
00177   Histogram() {}
00178 
00180   virtual double AverageInternal() = 0;
00181   virtual double PercentileInternal(const double perc) = 0;
00182   virtual double StandardDeviationInternal() = 0;
00183   virtual double CountInternal() = 0;
00184   virtual double MaximumInternal() = 0;
00185   virtual double MinimumInternal() = 0;
00186 
00187   virtual AbstractMutex* lock() = 0;
00188 
00195   void WriteRawHistogramData(Writer* writer, MessageHandler* handler);
00196 
00197  private:
00198   DISALLOW_COPY_AND_ASSIGN(Histogram);
00199 };
00200 
00202 class CountHistogram : public Histogram {
00203  public:
00204   CountHistogram() : count_(0) {}
00205   virtual ~CountHistogram();
00206   virtual void Add(const double value) {
00207     ScopedMutex hold(lock());
00208     ++count_;
00209   }
00210   virtual void Clear() {
00211     ScopedMutex hold(lock());
00212     count_ = 0;
00213   }
00214   virtual int NumBuckets() { return 0; }
00215   virtual void EnableNegativeBuckets() { }
00216   virtual void SetMinValue(double value) { }
00217   virtual void SetMaxValue(double value) { }
00218   virtual void SetSuggestedNumBuckets(int i) { }
00219   virtual GoogleString GetName() const { return ""; }
00220 
00221  protected:
00222   virtual AbstractMutex* lock() { return &mutex_; }
00223   virtual double AverageInternal() { return 0.0; }
00224   virtual double PercentileInternal(const double perc) { return 0.0; }
00225   virtual double StandardDeviationInternal() { return 0.0; }
00226   virtual double CountInternal() { return count_; }
00227   virtual double MaximumInternal() { return 0.0; }
00228   virtual double MinimumInternal() { return 0.0; }
00229   virtual double BucketStart(int index) { return 0.0; }
00230   virtual double BucketCount(int index) { return 0.0; }
00231 
00232  private:
00233   NullMutex mutex_;
00234   int count_;
00235 
00236   DISALLOW_COPY_AND_ASSIGN(CountHistogram);
00237 };
00238 
00242 class TimedVariable {
00243  public:
00245   enum Levels { TENSEC, MINUTE, HOUR, START };
00246   virtual ~TimedVariable();
00248   virtual void IncBy(int64 delta) = 0;
00251   virtual int64 Get(int level) = 0;
00253   virtual void Clear() = 0;
00254 };
00255 
00257 class FakeTimedVariable : public TimedVariable {
00258  public:
00259   explicit FakeTimedVariable(Variable* var) : var_(var) {
00260   }
00261   virtual ~FakeTimedVariable();
00263   virtual void IncBy(int64 delta) {
00264     var_->Add(delta);
00265   }
00268   virtual int64 Get(int level) {
00272     if (level == START) {
00273       return var_->Get();
00274     }
00275     return 0;
00276   }
00278   virtual void Clear() {
00279     return var_->Clear();
00280   }
00281 
00282  protected:
00283   Variable* var_;
00284 };
00285 
00287 class Statistics {
00288  public:
00289   virtual ~Statistics();
00290 
00294   virtual Variable* AddVariable(const StringPiece& name) = 0;
00295 
00299   virtual Variable* AddGlobalVariable(const StringPiece& name);
00300 
00302   virtual Variable* FindVariable(const StringPiece& name) const = 0;
00303 
00305   Variable* GetVariable(const StringPiece& name) const {
00306     Variable* var = FindVariable(name);
00307     CHECK(var != NULL) << "Variable not found: " << name;
00308     return var;
00309   }
00310 
00314   virtual Histogram* AddHistogram(const StringPiece& name) = 0;
00316   virtual Histogram* FindHistogram(const StringPiece& name) const = 0;
00318   Histogram* GetHistogram(const StringPiece& name) const {
00319     Histogram* hist = FindHistogram(name);
00320     CHECK(hist != NULL) << "Histogram not found: " << name;
00321     return hist;
00322   }
00323 
00328   virtual TimedVariable* AddTimedVariable(
00329       const StringPiece& name, const StringPiece& group) = 0;
00331   virtual TimedVariable* FindTimedVariable(
00332       const StringPiece& name) const = 0;
00334   TimedVariable* GetTimedVariable(
00335       const StringPiece& name) const {
00336     TimedVariable* stat = FindTimedVariable(name);
00337     CHECK(stat != NULL) << "TimedVariable not found: " << name;
00338     return stat;
00339   }
00341   virtual const StringVector& HistogramNames() = 0;
00343   virtual const std::map<GoogleString, StringVector>& TimedVariableMap() = 0;
00345   virtual void Dump(Writer* writer, MessageHandler* handler) = 0;
00353   virtual void DumpConsoleVarsToWriter(
00354       int64 current_time_ms, Writer* writer, MessageHandler* message_handler) {}
00355   virtual void RenderTimedVariables(Writer* writer,
00356                                     MessageHandler* handler);
00358   virtual void RenderHistograms(Writer* writer, MessageHandler* handler);
00361   virtual void Clear() = 0;
00362 
00367   virtual ConsoleStatisticsLogger* console_logger() const { return NULL; }
00368 
00369  protected:
00371   FakeTimedVariable* NewFakeTimedVariable(const StringPiece& name, int index);
00372 };
00373 
00374 }  
00375 
00376 #endif  ///< NET_INSTAWEB_UTIL_PUBLIC_STATISTICS_H_
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines