rocksdb/util/stop_watch.h
Abhishek Kona e1174306c5 [RocksDB] Simplify StopWatch implementation
Summary:
Make stop watch a simple implementation, instead of subclass of a virtual class
Allocate stop watches off the stack instead of heap.
Code is more terse now.

Test Plan: make all check, db_bench with --statistics=1

Reviewers: haobo, dhruba

Reviewed By: haobo

CC: leveldb

Differential Revision: https://reviews.facebook.net/D10809
2013-05-17 10:55:34 -07:00

42 lines
918 B
C++

#ifndef STORAGE_LEVELDB_UTIL_STOP_WATCH_H_
#define STORAGE_LEVELDB_UTIL_STOP_WATCH_H_
#include "leveldb/env.h"
#include "leveldb/statistics.h"
namespace leveldb {
// Auto-scoped.
// Records the statistic into the corresponding histogram.
class StopWatch {
public:
StopWatch(
Env * const env,
std::shared_ptr<Statistics> statistics,
const Histograms histogram_name) :
env_(env),
start_time_(env->NowMicros()),
statistics_(statistics),
histogram_name_(histogram_name) {}
uint64_t ElapsedMicros() {
return env_->NowMicros() - start_time_;
}
~StopWatch() {
if (statistics_) {
statistics_->measureTime(histogram_name_, ElapsedMicros());
}
}
private:
Env* const env_;
const uint64_t start_time_;
std::shared_ptr<Statistics> statistics_;
const Histograms histogram_name_;
};
} // namespace leveldb
#endif // STORAGE_LEVELDB_UTIL_STOP_WATCH_H_