aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/clickhouse/src/Common/ElapsedTimeProfileEventIncrement.h
blob: b30afd24a4c2a54b98fb2f1f43071d9a9f1a0b2e (plain) (blame)
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
#pragma once

#include <Common/ProfileEvents.h>
#include <Common/Stopwatch.h>

namespace DB
{

enum Time
{
    Nanoseconds,
    Microseconds,
    Milliseconds,
    Seconds,
};

template <Time time>
struct ProfileEventTimeIncrement
{
    explicit ProfileEventTimeIncrement<time>(ProfileEvents::Event event_)
        : event(event_), watch(CLOCK_MONOTONIC) {}

    UInt64 elapsed()
    {
        if constexpr (time == Time::Nanoseconds)
            return watch.elapsedNanoseconds();
        else if constexpr (time == Time::Microseconds)
            return watch.elapsedMicroseconds();
        else if constexpr (time == Time::Milliseconds)
            return watch.elapsedMilliseconds();
        else if constexpr (time == Time::Seconds)
            return watch.elapsedSeconds();
    }

    ~ProfileEventTimeIncrement()
    {
        watch.stop();
        ProfileEvents::increment(event, elapsed());
    }

    ProfileEvents::Event event;
    Stopwatch watch;
};

}