• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 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 <perf.h>
17 #include "sleep_ex.h"
18 
19 #include <gtest/gtest.h>
20 
21 using namespace testing::ext;
22 using namespace OHOS::TestAW;
23 
24 static BaseLineManager m_baseline(PERF_BASELINE_CONFIG_PATH);
25 class SpentTimeTest : public testing::Test {
26 public:
SetUpTestCase(void)27     static void SetUpTestCase(void) {}
TearDownTestCase(void)28     static void TearDownTestCase(void) {}
SetUp()29     void SetUp() {}
TearDown()30     void TearDown() {}
31 };
32 
LoopMsleep(void * pMsec)33 static void LoopMsleep(void* pMsec)
34 {
35     if (pMsec == nullptr) {
36         return;
37     }
38 
39     int msec = *reinterpret_cast<int*> (pMsec);
40     for (int index = 0; index < msec; index++) {
41         Msleep(1);
42     }
43 }
44 
45 /**
46  * @tc.name: SpentTime001
47  * @tc.desc: Test spent time by ExpectSmaller.
48  * @tc.type: PERF
49  * @tc.require: AR000CQGNT
50  */
51 HWTEST_F(SpentTimeTest, SpentTime001, TestSize.Level3)
52 {
53     /**
54      * @tc.steps: step1. check result is smaller than or equal baseline.
55      * @tc.expected: step1. return true
56      */
57 
58     double spentTime = 0;
59     int millionsecond = 1000;
60 
61     GtestPerfTestCase ptc(&m_baseline, this, 1);
62     spentTime = ElapsedTime(LoopMsleep, &millionsecond) / 1000;
63     ptc.ExpectSmaller(spentTime);
64 }
65 
66 /**
67  * @tc.name: SpentTime002
68  * @tc.desc: Test spent time by ExpectLarger.
69  * @tc.type: PERF
70  * @tc.require: AR000CQGNT
71  */
72 HWTEST_F(SpentTimeTest, SpentTime002, TestSize.Level3)
73 {
74     /**
75      * @tc.steps: step1. check result is larger than or equal baseline
76      * @tc.expected: step1. return true
77      */
78 
79     double spentTime = 0;
80     int millionsecond = 1000;
81 
82     GtestPerfTestCase ptc(&m_baseline, this, 1); // case_version is 1
83     spentTime = ElapsedTime(LoopMsleep, &millionsecond) / 1000;
84     ptc.ExpectLarger(spentTime);
85 }
86 
87 /**
88  * @tc.name: SpentTime003
89  * @tc.desc: Test spent time by GetBaseLine.
90  * @tc.type: PERF
91  * @tc.require: AR000CQGNT
92  */
93 HWTEST_F(SpentTimeTest, SpentTime003, TestSize.Level3)
94 {
95     /**
96      * @tc.steps: step1. check result is smaller than or equal baseline.
97      * @tc.expected: step1. return true
98      */
99     double spentTime = 0;
100     int millionsecond = 100;
101 
102     GtestPerfTestCase ptc(&m_baseline, this, 1); //case_version is 1
103     spentTime = ElapsedTime(LoopMsleep, &millionsecond) / 1000;
104     ptc.SaveResult(spentTime);
105 
106     if (ptc.HasBaseLine()) {
107         EXPECT_LE(spentTime, ptc.GetBaseLine() * (1 + ptc.GetFloatRange()));
108     } else {
109         EXPECT_TRUE(ptc.HasBaseLine());
110     }
111 }
112 
113 /**
114  * @tc.name: SpentTime004
115  * @tc.desc: Test spent time by caseVersion.
116  * @tc.type: PERF
117  * @tc.require: AR000CQGNT
118  */
119 HWTEST_F(SpentTimeTest, SpentTime004, TestSize.Level3)
120 {
121     /**
122      * @tc.steps: step1. check result is smaller than or equal baseline.
123      * @tc.expected: step1. return true
124      */
125 
126     double spentTime = 0;
127     int millionsecond = 100;
128 
129     GtestPerfTestCase ptc(&m_baseline, this, 2, "", "ElapsedTime");
130     spentTime = ElapsedTime(LoopMsleep, &millionsecond) / 1000;
131     ptc.ExpectSmaller(spentTime);
132 }
133