2013-02-15 20:53:17 +01:00
|
|
|
#ifndef STORAGE_LEVELDB_UTIL_STOP_WATCH_H_
|
|
|
|
#define STORAGE_LEVELDB_UTIL_STOP_WATCH_H_
|
|
|
|
|
|
|
|
#include "leveldb/env.h"
|
|
|
|
#include "leveldb/statistics.h"
|
|
|
|
|
2013-05-17 19:55:34 +02:00
|
|
|
namespace leveldb {
|
|
|
|
// Auto-scoped.
|
|
|
|
// Records the statistic into the corresponding histogram.
|
2013-02-15 20:53:17 +01:00
|
|
|
class StopWatch {
|
|
|
|
public:
|
2013-05-17 19:55:34 +02:00
|
|
|
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) {}
|
2013-02-15 20:53:17 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
2013-05-17 19:55:34 +02:00
|
|
|
uint64_t ElapsedMicros() {
|
2013-02-15 20:53:17 +01:00
|
|
|
return env_->NowMicros() - start_time_;
|
|
|
|
}
|
|
|
|
|
2013-05-17 19:55:34 +02:00
|
|
|
~StopWatch() {
|
|
|
|
if (statistics_) {
|
|
|
|
statistics_->measureTime(histogram_name_, ElapsedMicros());
|
|
|
|
}
|
2013-02-15 20:53:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
Env* const env_;
|
|
|
|
const uint64_t start_time_;
|
2013-03-27 19:27:39 +01:00
|
|
|
std::shared_ptr<Statistics> statistics_;
|
2013-02-15 20:53:17 +01:00
|
|
|
const Histograms histogram_name_;
|
|
|
|
|
|
|
|
};
|
|
|
|
} // namespace leveldb
|
|
|
|
#endif // STORAGE_LEVELDB_UTIL_STOP_WATCH_H_
|