1*8f0ba417SAndroid Build Coastguard Worker /*
2*8f0ba417SAndroid Build Coastguard Worker * Copyright (C) 2017 The Android Open Source Project
3*8f0ba417SAndroid Build Coastguard Worker *
4*8f0ba417SAndroid Build Coastguard Worker * Licensed under the Apache License, Version 2.0 (the "License");
5*8f0ba417SAndroid Build Coastguard Worker * you may not use this file except in compliance with the License.
6*8f0ba417SAndroid Build Coastguard Worker * You may obtain a copy of the License at
7*8f0ba417SAndroid Build Coastguard Worker *
8*8f0ba417SAndroid Build Coastguard Worker * http://www.apache.org/licenses/LICENSE-2.0
9*8f0ba417SAndroid Build Coastguard Worker *
10*8f0ba417SAndroid Build Coastguard Worker * Unless required by applicable law or agreed to in writing, software
11*8f0ba417SAndroid Build Coastguard Worker * distributed under the License is distributed on an "AS IS" BASIS,
12*8f0ba417SAndroid Build Coastguard Worker * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*8f0ba417SAndroid Build Coastguard Worker * See the License for the specific language governing permissions and
14*8f0ba417SAndroid Build Coastguard Worker * limitations under the License.
15*8f0ba417SAndroid Build Coastguard Worker */
16*8f0ba417SAndroid Build Coastguard Worker
17*8f0ba417SAndroid Build Coastguard Worker #include "android-base/chrono_utils.h"
18*8f0ba417SAndroid Build Coastguard Worker
19*8f0ba417SAndroid Build Coastguard Worker #include <time.h>
20*8f0ba417SAndroid Build Coastguard Worker
21*8f0ba417SAndroid Build Coastguard Worker #include <chrono>
22*8f0ba417SAndroid Build Coastguard Worker #include <sstream>
23*8f0ba417SAndroid Build Coastguard Worker #include <string>
24*8f0ba417SAndroid Build Coastguard Worker #include <thread>
25*8f0ba417SAndroid Build Coastguard Worker
26*8f0ba417SAndroid Build Coastguard Worker #include <gtest/gtest.h>
27*8f0ba417SAndroid Build Coastguard Worker
28*8f0ba417SAndroid Build Coastguard Worker namespace android {
29*8f0ba417SAndroid Build Coastguard Worker namespace base {
30*8f0ba417SAndroid Build Coastguard Worker
GetBootTimeSeconds()31*8f0ba417SAndroid Build Coastguard Worker std::chrono::seconds GetBootTimeSeconds() {
32*8f0ba417SAndroid Build Coastguard Worker struct timespec now;
33*8f0ba417SAndroid Build Coastguard Worker clock_gettime(CLOCK_BOOTTIME, &now);
34*8f0ba417SAndroid Build Coastguard Worker
35*8f0ba417SAndroid Build Coastguard Worker auto now_tp = boot_clock::time_point(std::chrono::seconds(now.tv_sec) +
36*8f0ba417SAndroid Build Coastguard Worker std::chrono::nanoseconds(now.tv_nsec));
37*8f0ba417SAndroid Build Coastguard Worker return std::chrono::duration_cast<std::chrono::seconds>(now_tp.time_since_epoch());
38*8f0ba417SAndroid Build Coastguard Worker }
39*8f0ba417SAndroid Build Coastguard Worker
40*8f0ba417SAndroid Build Coastguard Worker // Tests (at least) the seconds accuracy of the boot_clock::now() method.
TEST(ChronoUtilsTest,BootClockNowSeconds)41*8f0ba417SAndroid Build Coastguard Worker TEST(ChronoUtilsTest, BootClockNowSeconds) {
42*8f0ba417SAndroid Build Coastguard Worker auto now = GetBootTimeSeconds();
43*8f0ba417SAndroid Build Coastguard Worker auto boot_seconds =
44*8f0ba417SAndroid Build Coastguard Worker std::chrono::duration_cast<std::chrono::seconds>(boot_clock::now().time_since_epoch());
45*8f0ba417SAndroid Build Coastguard Worker EXPECT_EQ(now, boot_seconds);
46*8f0ba417SAndroid Build Coastguard Worker }
47*8f0ba417SAndroid Build Coastguard Worker
48*8f0ba417SAndroid Build Coastguard Worker template <typename T>
ExpectAboutEqual(T expected,T actual)49*8f0ba417SAndroid Build Coastguard Worker void ExpectAboutEqual(T expected, T actual) {
50*8f0ba417SAndroid Build Coastguard Worker auto expected_upper_bound = expected * 1.05f;
51*8f0ba417SAndroid Build Coastguard Worker auto expected_lower_bound = expected * .95;
52*8f0ba417SAndroid Build Coastguard Worker EXPECT_GT(expected_upper_bound, actual);
53*8f0ba417SAndroid Build Coastguard Worker EXPECT_LT(expected_lower_bound, actual);
54*8f0ba417SAndroid Build Coastguard Worker }
55*8f0ba417SAndroid Build Coastguard Worker
TEST(ChronoUtilsTest,TimerDurationIsSane)56*8f0ba417SAndroid Build Coastguard Worker TEST(ChronoUtilsTest, TimerDurationIsSane) {
57*8f0ba417SAndroid Build Coastguard Worker auto start = boot_clock::now();
58*8f0ba417SAndroid Build Coastguard Worker Timer t;
59*8f0ba417SAndroid Build Coastguard Worker std::this_thread::sleep_for(50ms);
60*8f0ba417SAndroid Build Coastguard Worker auto stop = boot_clock::now();
61*8f0ba417SAndroid Build Coastguard Worker auto stop_timer = t.duration();
62*8f0ba417SAndroid Build Coastguard Worker
63*8f0ba417SAndroid Build Coastguard Worker auto expected = std::chrono::duration_cast<std::chrono::milliseconds>(stop - start);
64*8f0ba417SAndroid Build Coastguard Worker ExpectAboutEqual(expected, stop_timer);
65*8f0ba417SAndroid Build Coastguard Worker }
66*8f0ba417SAndroid Build Coastguard Worker
TEST(ChronoUtilsTest,TimerOstream)67*8f0ba417SAndroid Build Coastguard Worker TEST(ChronoUtilsTest, TimerOstream) {
68*8f0ba417SAndroid Build Coastguard Worker Timer t;
69*8f0ba417SAndroid Build Coastguard Worker std::this_thread::sleep_for(50ms);
70*8f0ba417SAndroid Build Coastguard Worker auto stop_timer = t.duration().count();
71*8f0ba417SAndroid Build Coastguard Worker std::stringstream os;
72*8f0ba417SAndroid Build Coastguard Worker os << t;
73*8f0ba417SAndroid Build Coastguard Worker decltype(stop_timer) stop_timer_from_stream;
74*8f0ba417SAndroid Build Coastguard Worker os >> stop_timer_from_stream;
75*8f0ba417SAndroid Build Coastguard Worker EXPECT_NE(0, stop_timer);
76*8f0ba417SAndroid Build Coastguard Worker ExpectAboutEqual(stop_timer, stop_timer_from_stream);
77*8f0ba417SAndroid Build Coastguard Worker }
78*8f0ba417SAndroid Build Coastguard Worker
79*8f0ba417SAndroid Build Coastguard Worker } // namespace base
80*8f0ba417SAndroid Build Coastguard Worker } // namespace android
81