aboutsummaryrefslogtreecommitdiffstats
path: root/storage/src/vespa/storageframework/generic/clock/timer.h
blob: ee9853c97282355522699db0754efc9dab9a19f4 (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
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
/**
 * \class storage::framework::Timer
 * \ingroup clock
 *
 * \brief Class used to measure time differences.
 */

#pragma once

#include "clock.h"

namespace storage::framework {

class MilliSecTimer {
    const Clock* _clock;
    vespalib::steady_time _startTime;

public:
    explicit MilliSecTimer(const Clock& clock)
        : _clock(&clock), _startTime(_clock->getMonotonicTime()) {}

    // Copy construction makes the most sense when creating a timer that is
    // intended to inherit another timer's start time point, without incurring
    // the cost of an initial clock sampling.
    MilliSecTimer(const MilliSecTimer&) = default;
    MilliSecTimer& operator=(const MilliSecTimer&) = default;

    [[nodiscard]] vespalib::duration getElapsedTime() const {
        return _clock->getMonotonicTime() - _startTime;
    }

    [[nodiscard]] double getElapsedTimeAsDouble() const {
        using ToDuration = std::chrono::duration<double, std::milli>;
        return std::chrono::duration_cast<ToDuration>(getElapsedTime()).count();
    }
};

}