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 "benchmark_log.h"
21  #include "benchmark_assert.h"
22  using namespace std;
23  
24  namespace OHOS {
25  namespace {
26  
27  class BenchmarkDateTimeTest : public benchmark::Fixture {
28  public:
SetUp(const::benchmark::State & state)29      void SetUp(const ::benchmark::State& state) override
30      {
31      }
32  
TearDown(const::benchmark::State & state)33      void TearDown(const ::benchmark::State& state) override
34      {
35      }
36  
BenchmarkDateTimeTest()37      BenchmarkDateTimeTest()
38      {
39          Iterations(iterations);
40          Repetitions(repetitions);
41          ReportAggregatesOnly();
42      }
43  
44      ~BenchmarkDateTimeTest() override = default;
45  
46  protected:
47      const int32_t repetitions = 3;
48      const int32_t iterations = 1000;
49  };
50  
51  const int64_t SLEEP_DURATION_MICROSECONDS = 100;
52  
53  /*
54   * @tc.name: testTimecover001
55   * @tc.desc: convert all letters of str to uppercase
56   */
BENCHMARK_F(BenchmarkDateTimeTest,testTimecover001)57  BENCHMARK_F(BenchmarkDateTimeTest, testTimecover001)(benchmark::State& state)
58  {
59      BENCHMARK_LOGD("DateTimeTest testTimecover001 start.");
60      while (state.KeepRunning()) {
61          const int64_t second = 20;
62          const int64_t nanosecondsInASecond = 20000000000;
63          AssertEqual(SecToNanosec(second), nanosecondsInASecond,
64              "SecToNanosec(second) did not equal nanosecondsInASecond as expected.", state);
65          const int64_t millsec = 10;
66          const int64_t nanosecondsInAMillisecond = 10000000;
67          AssertEqual(MillisecToNanosec(millsec), nanosecondsInAMillisecond,
68              "MillisecToNanosec(millsec) did not equal nanosecondsInAMillisecond as expected.", state);
69          const int64_t microsec = 5;
70          const int64_t nanosecondsInAMicrosecond = 5000;
71          AssertEqual(MicrosecToNanosec(microsec), nanosecondsInAMicrosecond,
72              "MicrosecToNanosec(microsec) did not equal nanosecondsInAMicrosecond as expected.", state);
73  
74          const int64_t nanoces = 1000000000;
75          const int64_t secondsInANanosecond = 1;
76          const int64_t millisecondsInANanosecond = 1000;
77          const int64_t microsecondsInANanosecond = 1000000;
78          AssertEqual(NanosecToSec(nanoces), secondsInANanosecond,
79              "NanosecToSec(nanoces) did not equal secondsInANanosecond as expected.", state);
80          AssertEqual(NanosecToMillisec(nanoces), millisecondsInANanosecond,
81              "NanosecToMillisec(nanoces) did not equal millisecondsInANanosecond as expected.", state);
82          AssertEqual(NanosecToMicrosec(nanoces), microsecondsInANanosecond,
83              "NanosecToMicrosec(nanoces) did not equal microsecondsInANanosecond as expected.", state);
84      }
85      BENCHMARK_LOGD("DateTimeTest testTimecover001 end.");
86  }
87  
88  /*
89   * @tc.name: testTime001
90   * @tc.desc: datetime unit
91   */
BENCHMARK_F(BenchmarkDateTimeTest,testTime001)92  BENCHMARK_F(BenchmarkDateTimeTest, testTime001)(benchmark::State& state)
93  {
94      BENCHMARK_LOGD("DateTimeTest testTime001 start.");
95      while (state.KeepRunning()) {
96          int64_t second = GetSecondsSince1970ToNow();
97  
98          struct tm curTime = {0};
99          bool ret = GetSystemCurrentTime(&curTime);
100          AssertEqual(ret, true, "ret did not equal true as expected.", state);
101          ret = GetSystemCurrentTime(nullptr);
102          AssertEqual(ret, false, "ret did not equal false as expected.", state);
103  
104          int64_t second2 = GetSecondsSince1970ToPointTime(curTime);
105          AssertEqual(second, second2, "second did not equal second2 as expected.", state);
106          struct tm info;
107          info.tm_year = INT32_MIN;
108          info.tm_mon = 0;
109          info.tm_mday = 0;
110          info.tm_hour = 0;
111          info.tm_min = 0;
112          info.tm_sec = 0;
113          info.tm_isdst = 0;
114          const int64_t invalidReturnValue = -1;
115          second2 = GetSecondsSince1970ToPointTime(info);
116          AssertEqual(invalidReturnValue, second2, "invalidReturnValue did not equal second2 as expected.", state);
117          int64_t ret2 = GetSecondsBetween(curTime, info);
118          AssertTrue((ret2 = invalidReturnValue), "ret2 = invalidReturnValue did not equal true as expected.", state);
119      }
120      BENCHMARK_LOGD("DateTimeTest testTime001 end.");
121  }
122  
123  /*
124   * @tc.name: testTime002
125   * @tc.desc: datetime unit
126   */
BENCHMARK_F(BenchmarkDateTimeTest,testTime002)127  BENCHMARK_F(BenchmarkDateTimeTest, testTime002)(benchmark::State& state)
128  {
129      BENCHMARK_LOGD("DateTimeTest testTime002 start.");
130      while (state.KeepRunning()) {
131          int64_t days = GetDaysSince1970ToNow();
132          int64_t seconds = GetSecondsSince1970ToNow();
133          const int64_t secondsInAnHour = 3600;
134          const int64_t hoursInADay = 24;
135          int64_t resultdays = seconds / (secondsInAnHour * hoursInADay);
136          AssertEqual(days, resultdays, "days did not equal resultdays as expected.", state);
137      }
138      BENCHMARK_LOGD("DateTimeTest testTime002 end.");
139  }
140  
141  /*
142   * @tc.name: testTime003
143   * @tc.desc: datetime unit
144   */
BENCHMARK_F(BenchmarkDateTimeTest,testTime003)145  BENCHMARK_F(BenchmarkDateTimeTest, testTime003)(benchmark::State& state)
146  {
147      BENCHMARK_LOGD("DateTimeTest testTime003 start.");
148      while (state.KeepRunning()) {
149          struct tm curTime = { 0 };
150          bool ret = GetSystemCurrentTime(&curTime);
151          AssertEqual(ret, true, "ret did not equal true as expected.", state);
152  
153          struct tm curTime2 = { 0 };
154          ret = GetSystemCurrentTime(&curTime2);
155          AssertEqual(ret, true, "ret did not equal true as expected.", state);
156          int64_t betweensec = GetSecondsBetween(curTime, curTime2);
157          AssertGreaterThanOrEqual(betweensec, 0, "betweensec >= 0 did not equal true as expected.", state);
158      }
159      BENCHMARK_LOGD("DateTimeTest testTime003 end.");
160  }
161  
162  /*
163   * @tc.name: testTime004
164   * @tc.desc: datetime unit
165   */
BENCHMARK_F(BenchmarkDateTimeTest,testTime004)166  BENCHMARK_F(BenchmarkDateTimeTest, testTime004)(benchmark::State& state)
167  {
168      BENCHMARK_LOGD("DateTimeTest testTime004 start.");
169      while (state.KeepRunning()) {
170          int timezone = 0;
171          bool ret = GetLocalTimeZone(timezone);
172          AssertEqual(ret, true, "ret did not equal true as expected.", state);
173      }
174      BENCHMARK_LOGD("DateTimeTest testTime004 end.");
175  }
176  
177  /*
178   * @tc.name: testGetTickCount001
179   * @tc.desc: datetime unit
180   */
BENCHMARK_F(BenchmarkDateTimeTest,testGetTickCount001)181  BENCHMARK_F(BenchmarkDateTimeTest, testGetTickCount001)(benchmark::State& state)
182  {
183      BENCHMARK_LOGD("DateTimeTest testGetTickCount001 start.");
184      while (state.KeepRunning()) {
185          int64_t begin = GetTickCount();
186          usleep(SLEEP_DURATION_MICROSECONDS);
187          int64_t end = GetTickCount();
188  
189          AssertGreaterThanOrEqual(end - begin, 0, "end - begin >= 0 did not equal true as expected.", state);
190      }
191      BENCHMARK_LOGD("DateTimeTest testGetTickCount001 end.");
192  }
193  
194  /*
195   * @tc.name: testGetMicroTickCount001
196   * @tc.desc: datetime unit
197   */
BENCHMARK_F(BenchmarkDateTimeTest,testGetMicroTickCount001)198  BENCHMARK_F(BenchmarkDateTimeTest, testGetMicroTickCount001)(benchmark::State& state)
199  {
200      BENCHMARK_LOGD("DateTimeTest testGetMicroTickCount001 start.");
201      while (state.KeepRunning()) {
202          int64_t begin = GetMicroTickCount();
203          usleep(SLEEP_DURATION_MICROSECONDS);
204          int64_t end = GetMicroTickCount();
205  
206          AssertGreaterThanOrEqual(end - begin, 0, "end - begin >= 0 did not equal true as expected.", state);
207      }
208      BENCHMARK_LOGD("DateTimeTest testGetMicroTickCount001 end.");
209  }
210  }  // namespace
211  }  // namespace OHOS
212  // Run the benchmark
213  BENCHMARK_MAIN();