• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include <benchmark/benchmark.h>
17 #include "datetime_ex.h"
18 #include <unistd.h>
19 #include <iostream>
20 #include "../log.h"
21 #include "../assert.h"
22 using namespace std;
23 
24 namespace OHOS {
25 namespace {
26 
27 class BenchmarkDateTimeTest : public benchmark::Fixture {
28 public:
BenchmarkDateTimeTest()29     BenchmarkDateTimeTest()
30     {
31         Iterations(iterations);
32         Repetitions(repetitions);
33         ReportAggregatesOnly();
34     }
35 
36     ~BenchmarkDateTimeTest() override = default;
SetUp(const::benchmark::State & state)37     void SetUp(const ::benchmark::State& state) override
38     {
39     }
40 
TearDown(const::benchmark::State & state)41     void TearDown(const ::benchmark::State& state) override
42     {
43     }
44 
45 protected:
46     const int32_t repetitions = 3;
47     const int32_t iterations = 1000;
48 };
49 
50 const int64_t SLEEP_DURATION_MICROSECONDS = 100;
51 
52 /*
53  * @tc.name: testTimecover001
54  * @tc.desc: convert all letters of str to uppercase
55  */
BENCHMARK_F(BenchmarkDateTimeTest,testTimecover001)56 BENCHMARK_F(BenchmarkDateTimeTest, testTimecover001)(benchmark::State& state)
57 {
58     BENCHMARK_LOGD("DateTimeTest testTimecover001 start.");
59     while (state.KeepRunning()) {
60         const int64_t second = 20;
61         const int64_t nanosecondsInASecond = 20000000000;
62         AssertEqual(SecToNanosec(second), nanosecondsInASecond,
63             "SecToNanosec(second) did not equal nanosecondsInASecond as expected.", state);
64         const int64_t millsec = 10;
65         const int64_t nanosecondsInAMillisecond = 10000000;
66         AssertEqual(MillisecToNanosec(millsec), nanosecondsInAMillisecond,
67             "MillisecToNanosec(millsec) did not equal nanosecondsInAMillisecond as expected.", state);
68         const int64_t microsec = 5;
69         const int64_t nanosecondsInAMicrosecond = 5000;
70         AssertEqual(MicrosecToNanosec(microsec), nanosecondsInAMicrosecond,
71             "MicrosecToNanosec(microsec) did not equal nanosecondsInAMicrosecond as expected.", state);
72 
73         const int64_t nanoces = 1000000000;
74         const int64_t secondsInANanosecond = 1;
75         const int64_t millisecondsInANanosecond = 1000;
76         const int64_t microsecondsInANanosecond = 1000000;
77         AssertEqual(NanosecToSec(nanoces), secondsInANanosecond,
78             "NanosecToSec(nanoces) did not equal secondsInANanosecond as expected.", state);
79         AssertEqual(NanosecToMillisec(nanoces), millisecondsInANanosecond,
80             "NanosecToMillisec(nanoces) did not equal millisecondsInANanosecond as expected.", state);
81         AssertEqual(NanosecToMicrosec(nanoces), microsecondsInANanosecond,
82             "NanosecToMicrosec(nanoces) did not equal microsecondsInANanosecond as expected.", state);
83     }
84     BENCHMARK_LOGD("DateTimeTest testTimecover001 end.");
85 }
86 
87 /*
88  * @tc.name: testTime001
89  * @tc.desc: datetime unit
90  */
BENCHMARK_F(BenchmarkDateTimeTest,testTime001)91 BENCHMARK_F(BenchmarkDateTimeTest, testTime001)(benchmark::State& state)
92 {
93     BENCHMARK_LOGD("DateTimeTest testTime001 start.");
94     while (state.KeepRunning()) {
95         int64_t second = GetSecondsSince1970ToNow();
96 
97         struct tm curTime = {0};
98         bool ret = GetSystemCurrentTime(&curTime);
99         AssertEqual(ret, true, "ret did not equal true as expected.", state);
100         ret = GetSystemCurrentTime(nullptr);
101         AssertEqual(ret, false, "ret did not equal false as expected.", state);
102 
103         int64_t second2 = GetSecondsSince1970ToPointTime(curTime);
104         AssertEqual(second, second2, "second did not equal second2 as expected.", state);
105         struct tm info;
106         info.tm_year = INT32_MIN;
107         info.tm_mon = 0;
108         info.tm_mday = 0;
109         info.tm_hour = 0;
110         info.tm_min = 0;
111         info.tm_sec = 0;
112         info.tm_isdst = 0;
113         const int64_t invalidReturnValue = -1;
114         second2 = GetSecondsSince1970ToPointTime(info);
115         AssertEqual(invalidReturnValue, second2, "invalidReturnValue did not equal second2 as expected.", state);
116         int64_t ret2 = GetSecondsBetween(curTime, info);
117         AssertTrue((ret2 = invalidReturnValue), "ret2 = invalidReturnValue did not equal true as expected.", state);
118     }
119     BENCHMARK_LOGD("DateTimeTest testTime001 end.");
120 }
121 
122 /*
123  * @tc.name: testTime002
124  * @tc.desc: datetime unit
125  */
BENCHMARK_F(BenchmarkDateTimeTest,testTime002)126 BENCHMARK_F(BenchmarkDateTimeTest, testTime002)(benchmark::State& state)
127 {
128     BENCHMARK_LOGD("DateTimeTest testTime002 start.");
129     while (state.KeepRunning()) {
130         int64_t days = GetDaysSince1970ToNow();
131         int64_t seconds = GetSecondsSince1970ToNow();
132         const int64_t secondsInAnHour = 3600;
133         const int64_t hoursInADay = 24;
134         int64_t resultdays = seconds / (secondsInAnHour * hoursInADay);
135         AssertEqual(days, resultdays, "days did not equal resultdays as expected.", state);
136     }
137     BENCHMARK_LOGD("DateTimeTest testTime002 end.");
138 }
139 
140 /*
141  * @tc.name: testTime003
142  * @tc.desc: datetime unit
143  */
BENCHMARK_F(BenchmarkDateTimeTest,testTime003)144 BENCHMARK_F(BenchmarkDateTimeTest, testTime003)(benchmark::State& state)
145 {
146     BENCHMARK_LOGD("DateTimeTest testTime003 start.");
147     while (state.KeepRunning()) {
148         struct tm curTime = { 0 };
149         bool ret = GetSystemCurrentTime(&curTime);
150         AssertEqual(ret, true, "ret did not equal true as expected.", state);
151 
152         struct tm curTime2 = { 0 };
153         ret = GetSystemCurrentTime(&curTime2);
154         AssertEqual(ret, true, "ret did not equal true as expected.", state);
155         int64_t betweensec = GetSecondsBetween(curTime, curTime2);
156         AssertGreaterThanOrEqual(betweensec, 0, "betweensec >= 0 did not equal true as expected.", state);
157     }
158     BENCHMARK_LOGD("DateTimeTest testTime003 end.");
159 }
160 
161 /*
162  * @tc.name: testTime004
163  * @tc.desc: datetime unit
164  */
BENCHMARK_F(BenchmarkDateTimeTest,testTime004)165 BENCHMARK_F(BenchmarkDateTimeTest, testTime004)(benchmark::State& state)
166 {
167     BENCHMARK_LOGD("DateTimeTest testTime004 start.");
168     while (state.KeepRunning()) {
169         int timezone = 0;
170         bool ret = GetLocalTimeZone(timezone);
171         AssertEqual(ret, true, "ret did not equal true as expected.", state);
172     }
173     BENCHMARK_LOGD("DateTimeTest testTime004 end.");
174 }
175 
176 /*
177  * @tc.name: testGetTickCount001
178  * @tc.desc: datetime unit
179  */
BENCHMARK_F(BenchmarkDateTimeTest,testGetTickCount001)180 BENCHMARK_F(BenchmarkDateTimeTest, testGetTickCount001)(benchmark::State& state)
181 {
182     BENCHMARK_LOGD("DateTimeTest testGetTickCount001 start.");
183     while (state.KeepRunning()) {
184         int64_t begin = GetTickCount();
185         usleep(SLEEP_DURATION_MICROSECONDS);
186         int64_t end = GetTickCount();
187 
188         AssertGreaterThanOrEqual(end - begin, 0, "end - begin >= 0 did not equal true as expected.", state);
189     }
190     BENCHMARK_LOGD("DateTimeTest testGetTickCount001 end.");
191 }
192 
193 /*
194  * @tc.name: testGetMicroTickCount001
195  * @tc.desc: datetime unit
196  */
BENCHMARK_F(BenchmarkDateTimeTest,testGetMicroTickCount001)197 BENCHMARK_F(BenchmarkDateTimeTest, testGetMicroTickCount001)(benchmark::State& state)
198 {
199     BENCHMARK_LOGD("DateTimeTest testGetMicroTickCount001 start.");
200     while (state.KeepRunning()) {
201         int64_t begin = GetMicroTickCount();
202         usleep(SLEEP_DURATION_MICROSECONDS);
203         int64_t end = GetMicroTickCount();
204 
205         AssertGreaterThanOrEqual(end - begin, 0, "end - begin >= 0 did not equal true as expected.", state);
206     }
207     BENCHMARK_LOGD("DateTimeTest testGetMicroTickCount001 end.");
208 }
209 }  // namespace
210 }  // namespace OHOS
211 // Run the benchmark
212 BENCHMARK_MAIN();