summaryrefslogtreecommitdiff
path: root/test/network_tests/payload_tests/stopwatch.hpp
blob: 5917e12b286361dd63e509f9c77fb3d81770f975 (plain)
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
// Copyright (C) 2015-2017 Bayerische Motoren Werke Aktiengesellschaft (BMW AG)
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.

#ifndef STOP_WATCH_H_
#define STOP_WATCH_H_

#include <cstdint>


class stop_watch
{
public:
    typedef uint64_t usec_t;

    stop_watch() :
                    started_(false),
                    start_time_point_(0),
                    total_elapsed_(0)
    {
    }

    inline void reset()
    {
        started_ = false;
        total_elapsed_ = 0;
    }

    inline void start()
    {
        start_time_point_ = now();
        started_ = true;
    }

    inline void stop()
    {
        total_elapsed_ += get_elapsed();
        started_ = false;
    }

    usec_t get_total_elapsed_microseconds() const;
    usec_t get_total_elapsed_seconds() const;

private:
    inline usec_t get_elapsed() const
    {
        return now() - start_time_point_;
    }

    static usec_t now();

    bool started_;
    usec_t start_time_point_;
    usec_t total_elapsed_;
};

#endif // STOP_WATCH_H_