Robin Lee | 7e05cc9 | 2016-09-21 16:31:33 +0900 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
Mike Yu | e7e332f | 2019-03-13 17:15:48 +0800 | [diff] [blame^] | 17 | #ifndef NETDUTILS_STOPWATCH_H |
| 18 | #define NETDUTILS_STOPWATCH_H |
Robin Lee | 7e05cc9 | 2016-09-21 16:31:33 +0900 | [diff] [blame] | 19 | |
| 20 | #include <chrono> |
| 21 | |
Mike Yu | e7e332f | 2019-03-13 17:15:48 +0800 | [diff] [blame^] | 22 | namespace android { |
| 23 | namespace netdutils { |
| 24 | |
Robin Lee | 7e05cc9 | 2016-09-21 16:31:33 +0900 | [diff] [blame] | 25 | class Stopwatch { |
Mike Yu | e7e332f | 2019-03-13 17:15:48 +0800 | [diff] [blame^] | 26 | private: |
| 27 | using clock = std::chrono::steady_clock; |
| 28 | using time_point = std::chrono::time_point<clock>; |
| 29 | |
lifr | 9a729d7 | 2018-11-21 22:28:40 +0800 | [diff] [blame] | 30 | public: |
Lorenzo Colitti | 19ee8a8 | 2017-02-02 12:59:05 +0900 | [diff] [blame] | 31 | Stopwatch() : mStart(clock::now()) {} |
| 32 | |
Mike Yu | e7e332f | 2019-03-13 17:15:48 +0800 | [diff] [blame^] | 33 | virtual ~Stopwatch() = default; |
Robin Lee | 7e05cc9 | 2016-09-21 16:31:33 +0900 | [diff] [blame] | 34 | |
lifr | 9a729d7 | 2018-11-21 22:28:40 +0800 | [diff] [blame] | 35 | float timeTaken() const { return getElapsed(clock::now()); } |
| 36 | |
| 37 | int64_t timeTakenUs() const { return getElapsedUs(clock::now()); } |
Lorenzo Colitti | 19ee8a8 | 2017-02-02 12:59:05 +0900 | [diff] [blame] | 38 | |
| 39 | float getTimeAndReset() { |
| 40 | const auto& now = clock::now(); |
| 41 | float elapsed = getElapsed(now); |
| 42 | mStart = now; |
| 43 | return elapsed; |
Robin Lee | 7e05cc9 | 2016-09-21 16:31:33 +0900 | [diff] [blame] | 44 | } |
lifr | 9a729d7 | 2018-11-21 22:28:40 +0800 | [diff] [blame] | 45 | float getTimeAndResetUs() { |
| 46 | const auto& now = clock::now(); |
| 47 | float elapsed = getElapsedUs(now); |
| 48 | mStart = now; |
| 49 | return elapsed; |
| 50 | } |
Robin Lee | 7e05cc9 | 2016-09-21 16:31:33 +0900 | [diff] [blame] | 51 | |
lifr | 9a729d7 | 2018-11-21 22:28:40 +0800 | [diff] [blame] | 52 | private: |
Lorenzo Colitti | 19ee8a8 | 2017-02-02 12:59:05 +0900 | [diff] [blame] | 53 | time_point mStart; |
| 54 | |
| 55 | float getElapsed(const time_point& now) const { |
| 56 | using ms = std::chrono::duration<float, std::ratio<1, 1000>>; |
| 57 | return (std::chrono::duration_cast<ms>(now - mStart)).count(); |
| 58 | } |
lifr | 9a729d7 | 2018-11-21 22:28:40 +0800 | [diff] [blame] | 59 | int64_t getElapsedUs(const time_point& now) const { |
| 60 | return (std::chrono::duration_cast<std::chrono::microseconds>(now - mStart)).count(); |
| 61 | } |
Robin Lee | 7e05cc9 | 2016-09-21 16:31:33 +0900 | [diff] [blame] | 62 | }; |
| 63 | |
Mike Yu | e7e332f | 2019-03-13 17:15:48 +0800 | [diff] [blame^] | 64 | } // namespace netdutils |
| 65 | } // namespace android |
| 66 | |
| 67 | #endif // NETDUTILS_STOPWATCH_H |