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#ifndef AW_CXX_HWEXT_PERF_H_ 17#define AW_CXX_HWEXT_PERF_H_ 18 19#include <iosfwd> 20 21#include <string> 22#include <list> 23#include <map> 24#include <ctime> 25#include <gtest/gtest.h> 26 27namespace OHOS { 28namespace TestAW { 29 30#define PERF_BASELINE_CONFIG_PATH "/data/test/baseline.xml" 31 32// Common Function 33struct BaseLineItem { 34 std::string testcasename; 35 double baseline; 36 double lastvalue; 37 double floatrange; 38 BaseLineItem() 39 { 40 testcasename = ""; 41 baseline = 0; 42 lastvalue = 0; 43 floatrange = 0; 44 } 45}; 46 47struct BaselineConfig { 48 std::string date; 49 std::string url; 50 std::list<std::map<std::string, std::string>> items; 51}; 52 53// BaseLineManager 54class BaseLineManager { 55public: 56 BaseLineManager(); 57 explicit BaseLineManager(const std::string path); 58 ~BaseLineManager(); 59 60 bool LoadConfig(const std::string path); 61 bool IsNoBaseline(); 62 bool GetExtraValueDouble(const std::string testcaseName, const std::string extra, double &value); 63 64private: 65 bool ReadXmlFile(std::string path); 66 double StrtoDouble(const std::string &str); 67 68private: 69 BaselineConfig m_bastCfg; 70 bool m_bNoBaseline; 71}; 72 73// should be defined in GTEST testcase function. 74class GtestPerfTestCase { 75private: 76 GtestPerfTestCase() = delete; 77public: 78 GtestPerfTestCase(BaseLineManager* pManager, testing::Test *tester, 79 int caseVersion, 80 std::string testClassName = "", 81 std::string testInterfaceName = ""); 82 83 ~GtestPerfTestCase() {} 84 85 // expect result is larger than or equal baseline*(1.0-float_range). 86 bool ExpectLarger(double testValue); 87 88 // expect result is smaller than or equal baseline*(1.0-float_range). 89 bool ExpectSmaller(double testValue); 90 91 // specific baseline name (test case name). 92 bool SetBaseLine(std::string testcaseName); 93 94 // ================== extra APIs for advanced usage ================== 95 // get test case name. 96 std::string GetCaseName() 97 { 98 return m_strCaseName; 99 }; 100 101 // get baseline value. 102 bool HasBaseLine() 103 { 104 return m_bHasBaseLine; 105 }; 106 double GetBaseLine() 107 { 108 return m_dbBaseLine; 109 }; 110 111 // get last test result from remote database 112 bool HasLastValue() 113 { 114 return m_bHasLastValue; 115 }; 116 double GetLastValue() 117 { 118 return m_dbLastValue; 119 }; 120 121 // get floating range: 0.0 ~ 1.0 122 bool HasFloatRange() 123 { 124 return m_bHasFloatRange; 125 }; 126 double GetFloatRange() 127 { 128 return m_dbFloatRange; 129 }; 130 131 // get test result, valid after ExpectXXX() is called. 132 bool GetTestResult(double *out = nullptr) 133 { 134 if (out != nullptr) { 135 *out = m_dbTestResult; 136 } 137 return m_bTestResult; 138 }; 139 140 // output result to stdout & xml. 141 bool SaveResult(double testValue); 142 143private: 144 bool Initialize(); 145 void ResetValues(); 146 bool UpdateBaseLineName(std::string testcaseName); 147 bool ExpectValue(double testValue, bool isLargerBetter); 148 149private: 150 BaseLineManager *m_pManager; 151 testing::Test *m_pTester; 152 std::string m_strCaseName; 153 std::string m_strTestClassName; 154 std::string m_strTestInterfaceName; 155 int m_dCaseVersion; 156 157 bool m_bHasBaseLine; 158 double m_dbBaseLine; 159 bool m_bHasLastValue; 160 double m_dbLastValue; 161 bool m_bHasFloatRange; 162 double m_dbFloatRange; // 0.0~1.0 163 164 bool m_bTestResult; 165 double m_dbTestResult; 166}; 167} // namespace TestAW 168} // namespace OHOS 169 170#endif // AW_CXX_HWEXT_PERF_H_ 171