forked from scylladb/scylladb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlatency.hh
60 lines (50 loc) · 1.09 KB
/
latency.hh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
/*
* Copyright (C) 2015-present ScyllaDB
*/
/*
* SPDX-License-Identifier: LicenseRef-ScyllaDB-Source-Available-1.0
*/
#pragma once
#include <chrono>
/**
* A helper class to keep track of latencies
*/
namespace utils {
class latency_counter {
public:
using clock = std::chrono::steady_clock;
using time_point = clock::time_point;
using duration = clock::duration;
private:
time_point _start;
time_point _stop;
public:
void start() {
_start = now();
}
bool is_start() const {
// if start is not set it is still zero
return _start.time_since_epoch().count();
}
latency_counter& stop() {
_stop = now();
return *this;
}
bool is_stopped() const {
// if stop was not set, it is still zero
return _stop.time_since_epoch().count();
}
duration latency() const {
return _stop - _start;
}
latency_counter& check_and_stop() {
if (!is_stopped()) {
return stop();
}
return *this;
}
static time_point now() {
return clock::now();
}
};
}