1 /*************************************************************************
3 * Copyright 2016 Realm Inc.
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
17 **************************************************************************/
19 #ifndef REALM_METRIC_TIMER_HPP
20 #define REALM_METRIC_TIMER_HPP
22 #include <realm/util/features.h>
34 class MetricTimerResult
39 double get_elapsed_seconds() const;
40 virtual void report_seconds(double time);
42 double m_elapsed_seconds;
48 MetricTimer(std::shared_ptr<MetricTimerResult> destination = nullptr);
53 /// Returns elapsed time in seconds since last call to reset().
54 double get_elapsed_time() const;
55 /// Same as get_elapsed_time().
56 operator double() const;
58 /// Format the elapsed time on the form 0h00m, 00m00s, 00.00s, or
59 /// 000.0ms depending on magnitude.
60 static void format(double seconds, std::ostream&);
62 static std::string format(double seconds);
65 using clock_type = std::chrono::high_resolution_clock;
66 using time_point = std::chrono::time_point<clock_type>;
68 time_point m_paused_at;
69 std::shared_ptr<MetricTimerResult> m_dest;
71 time_point get_timer_ticks() const;
72 double calc_elapsed_seconds(time_point begin, time_point end) const;
76 inline void MetricTimer::reset()
78 m_start = get_timer_ticks();
81 inline double MetricTimer::get_elapsed_time() const
83 return calc_elapsed_seconds(m_start, get_timer_ticks());
86 inline MetricTimer::operator double() const
88 return get_elapsed_time();
91 inline std::ostream& operator<<(std::ostream& out, const MetricTimer& timer)
93 MetricTimer::format(timer, out);
98 } // namespace metrics
101 #endif // REALM_METRICS
103 #endif // REALM_METRIC_TIMER_HPP