stop_watch.h 1.4 KB
Newer Older
1 2 3
#ifndef STORAGE_LEVELDB_UTIL_STOP_WATCH_H_
#define STORAGE_LEVELDB_UTIL_STOP_WATCH_H_

4 5
#include "rocksdb/env.h"
#include "rocksdb/statistics.h"
6

7 8 9
namespace leveldb {
// Auto-scoped.
// Records the statistic into the corresponding histogram.
10 11
class StopWatch {
 public:
12 13
  StopWatch(
    Env * const env,
14 15
    std::shared_ptr<Statistics> statistics = nullptr,
    const Histograms histogram_name = DB_GET) :
16 17 18 19
      env_(env),
      start_time_(env->NowMicros()),
      statistics_(statistics),
      histogram_name_(histogram_name) {}
20 21 22



23
  uint64_t ElapsedMicros() {
24 25 26
    return env_->NowMicros() - start_time_;
  }

27 28 29 30
  ~StopWatch() {
    if (statistics_) {
      statistics_->measureTime(histogram_name_, ElapsedMicros());
    }
31 32 33 34 35
  }

 private:
  Env* const env_;
  const uint64_t start_time_;
A
Abhishek Kona 已提交
36
  std::shared_ptr<Statistics> statistics_;
37 38 39
  const Histograms histogram_name_;

};
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66

// a nano second precision stopwatch
class StopWatchNano {
 public:
  StopWatchNano(Env* const env, bool auto_start = false)
      : env_(env), start_(0) {
    if (auto_start) {
      Start();
    }
  }

  void Start() { start_ = env_->NowNanos(); }

  uint64_t ElapsedNanos(bool reset = false) {
    auto now = env_->NowNanos();
    auto elapsed = now - start_;
    if (reset) {
      start_ = now;
    }
    return elapsed;
  }

 private:
  Env* const env_;
  uint64_t start_;
};

67 68
} // namespace leveldb
#endif // STORAGE_LEVELDB_UTIL_STOP_WATCH_H_