Coverage Report

Created: 2025-05-19 20:29

/root/doris/cloud/src/common/stopwatch.h
Line
Count
Source (jump to first uncovered line)
1
// Licensed to the Apache Software Foundation (ASF) under one
2
// or more contributor license agreements.  See the NOTICE file
3
// distributed with this work for additional information
4
// regarding copyright ownership.  The ASF licenses this file
5
// to you under the Apache License, Version 2.0 (the
6
// "License"); you may not use this file except in compliance
7
// with the License.  You may obtain a copy of the License at
8
//
9
//   http://www.apache.org/licenses/LICENSE-2.0
10
//
11
// Unless required by applicable law or agreed to in writing,
12
// software distributed under the License is distributed on an
13
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14
// KIND, either express or implied.  See the License for the
15
// specific language governing permissions and limitations
16
// under the License.
17
18
#pragma once
19
20
#include <chrono>
21
22
namespace doris::cloud {
23
24
class StopWatch {
25
public:
26
25.9k
    StopWatch() {
27
25.9k
        running_ = true;
28
25.9k
        start_ = std::chrono::steady_clock::now();
29
25.9k
    };
30
    ~StopWatch() = default;
31
32
1
    void start() {
33
1
        if (!running_) {
34
0
            start_ = std::chrono::steady_clock::now();
35
0
            running_ = true;
36
0
        }
37
1
    }
38
39
18
    void pause() {
40
18
        if (running_) {
41
18
            elapsed_ = elapsed_ + (std::chrono::steady_clock::now() - start_);
42
18
            running_ = false;
43
18
        }
44
18
    }
45
46
1
    void resume() {
47
1
        if (!running_) {
48
1
            start_ = std::chrono::steady_clock::now();
49
1
            running_ = true;
50
1
        }
51
1
    }
52
53
1
    void reset() {
54
1
        start_ = std::chrono::steady_clock::now();
55
1
        elapsed_ = std::chrono::steady_clock::duration {0};
56
1
        running_ = true;
57
1
    }
58
59
25.9k
    int64_t elapsed_us() const {
60
25.9k
        if (!running_) {
61
19
            return std::chrono::duration_cast<std::chrono::microseconds>(elapsed_).count();
62
19
        }
63
64
25.8k
        auto end = std::chrono::steady_clock::now();
65
25.8k
        return std::chrono::duration_cast<std::chrono::microseconds>(elapsed_ + (end - start_))
66
25.8k
                .count();
67
25.9k
    }
68
69
private:
70
    std::chrono::steady_clock::time_point start_;
71
    std::chrono::steady_clock::duration elapsed_ {0};
72
    bool running_ {false};
73
};
74
75
} // namespace doris::cloud