• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2025 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 FRONTEND_API_DEFINES_H
17 #define FRONTEND_API_DEFINES_H
18 
19 #include <initializer_list>
20 #include <string_view>
21 #include <map>
22 #include "nlohmann/json.hpp"
23 
24 namespace OHOS::perftest {
25     using namespace std;
26     enum ConnectionStat : uint8_t { UNINIT, CONNECTED, DISCONNECTED };
27     constexpr string_view PUBLISH_EVENT_PREFIX = "perftest.api.caller.publish#";
28     constexpr uint32_t WAIT_CONN_TIMEOUT_MS = 5000;
29     constexpr uint32_t PUBLISH_MAX_RETIES = 10;
30     constexpr uint32_t TEST_ITERATIONS = 5;
31     constexpr uint32_t EXECUTION_TIMEOUT = 10000;
32 
33     enum ErrCode : uint32_t {
34         /**No Error*/
35         NO_ERROR = 0,
36         /**Initialize failed.*/
37         ERR_INITIALIZE_FAILED = 32400001,
38         /**System error.*/
39         ERR_INTERNAL = 32400002,
40         /**Invalid input parameter.*/
41         ERR_INVALID_INPUT = 32400003,
42         /**Execute callback failed.*/
43         ERR_CALLBACK_FAILED = 32400004,
44         /**Collect metric data failed.*/
45         ERR_DATA_COLLECTION_FAILED = 32400005,
46         /**Get measure result failed.*/
47         ERR_GET_RESULT_FAILED = 32400006,
48         /**API does not allow calling concurrently.*/
49         ERR_API_USAGE = 32400007,
50     };
51 
52     const std::map<ErrCode, std::string> ErrDescMap = {
53         /**Correspondence between error codes and descriptions*/
54         {NO_ERROR, "No Error"},
55         {ERR_INITIALIZE_FAILED, "Initialization failed."},
56         {ERR_INTERNAL, "Internal error."},
57         {ERR_INVALID_INPUT, "Invalid input parameter."},
58     };
59 
60     /**API invocation error detail wrapper.*/
61     struct ApiCallErr {
62     public:
63         ErrCode code_;
64         std::string message_ = "";
65 
66         ApiCallErr() = delete;
67 
ApiCallErrApiCallErr68         explicit ApiCallErr(ErrCode ec)
69         {
70             code_ = ec;
71             message_ = ErrDescMap.find(ec)->second;
72         }
73 
ApiCallErrApiCallErr74         ApiCallErr(ErrCode ec, std::string_view msg)
75         {
76             code_ = ec;
77             message_ = std::string(msg);
78         }
79     };
80 
81     /**Structure wraps the api-call data.*/
82     struct ApiCallInfo {
83         std::string apiId_;
84         std::string callerObjRef_;
85         nlohmann::json paramList_ = nlohmann::json::array();
86     };
87 
88     /**Structure wraps the api-call reply.*/
89     struct ApiReplyInfo {
90         nlohmann::json resultValue_ = nullptr;
91         ApiCallErr exception_ = ApiCallErr(NO_ERROR);
92     };
93 
94     /** Specifications of a frontend enumerator value.*/
95     struct FrontendEnumValueDef {
96         std::string_view name_;
97         std::string_view valueJson_;
98     };
99 
100     /** Specifications of a frontend enumerator.*/
101     struct FrontendEnumeratorDef {
102         std::string_view name_;
103         const FrontendEnumValueDef *values_;
104         size_t valueCount_;
105     };
106 
107     /** Specifications of a frontend json data property.*/
108     struct FrontEndJsonPropDef {
109         std::string_view name_;
110         std::string_view type_;
111         bool required_;
112     };
113     /** Specifications of a frontend json object.*/
114     struct FrontEndJsonDef {
115         std::string_view name_;
116         const FrontEndJsonPropDef *props_;
117         size_t propCount_;
118     };
119 
120     /** Specifications of a frontend class method.*/
121     struct FrontendMethodDef {
122         std::string_view name_;
123         std::string_view signature_;
124         bool static_;
125         bool fast_;
126     };
127 
128     /** Specifications of a frontend class.*/
129     struct FrontEndClassDef {
130         std::string_view name_;
131         const FrontendMethodDef *methods_;
132         size_t methodCount_;
133         bool bindClassObject_;
134     };
135 
136     /** PerfMetric enumerator definition.*/
137     constexpr FrontendEnumValueDef PERF_METRIC_VALUES[] = {
138         {"DURATION", "0"},
139         {"CPU_LOAD", "1"},
140         {"CPU_USAGE", "2"},
141         {"MEMORY_RSS", "3"},
142         {"MEMORY_PSS", "4"},
143         {"APP_START_RESPONSE_TIME", "5"},
144         {"APP_START_COMPLETE_TIME", "6"},
145         {"PAGE_SWITCH_COMPLETE_TIME", "7"},
146         {"LIST_SWIPE_FPS", "8"},
147     };
148     constexpr FrontendEnumeratorDef PERF_METRIC_DEF = {
149         "PerfMetric",
150         PERF_METRIC_VALUES,
151         sizeof(PERF_METRIC_VALUES) / sizeof(FrontendEnumValueDef),
152     };
153 
154     /** PerfTestStrategy jsonObject definition.*/
155     constexpr FrontEndJsonPropDef PERF_TEST_STRATEGY_PROPERTIES[] = {
156         // Callback<<Callback<boolean>> saved in js, works as callbackRef in c++.
157         {"metrics", "[int]", true},
158         {"actionCode", "string", true},
159         {"resetCode", "string", false},
160         {"bundleName", "string", false},
161         {"iterations", "int", false},
162         {"timeout", "int", false},
163     };
164     constexpr FrontEndJsonDef PERF_TEST_STRATEGY_DEF = {
165         "PerfTestStrategy",
166         PERF_TEST_STRATEGY_PROPERTIES,
167         sizeof(PERF_TEST_STRATEGY_PROPERTIES) / sizeof(FrontEndJsonPropDef),
168     };
169 
170     /** PerfMeasureResult jsonObject definition.*/
171     constexpr FrontEndJsonPropDef PERF_MEASURE_RESULT_PROPERTIES[] = {
172         {"metric", "int", true},
173         {"roundValues", "[int]", true},
174         {"maximum", "int", true},
175         {"minimum", "int", true},
176         {"average", "int", true},
177     };
178     constexpr FrontEndJsonDef PERF_MEASURE_RESULT_DEF = {
179         "PerfMeasureResult",
180         PERF_MEASURE_RESULT_PROPERTIES,
181         sizeof(PERF_MEASURE_RESULT_PROPERTIES) / sizeof(FrontEndJsonPropDef),
182     };
183 
184     /** PerfTest class definition.*/
185     constexpr FrontendMethodDef PERF_TEST_METHODS[] = {
186         {"PerfTest.create", "(PerfTestStrategy):PerfTest", true, true},
187         {"PerfTest.run", "():void", false, false},
188         {"PerfTest.getMeasureResult", "(int):PerfMeasureResult", false, true},
189         {"PerfTest.destroy", "():void", false, true},
190     };
191     constexpr FrontEndClassDef PERF_TEST_DEF = {
192         "PerfTest",
193         PERF_TEST_METHODS,
194         sizeof(PERF_TEST_METHODS) / sizeof(FrontendMethodDef),
195     };
196 
197     /** List all the frontend data-type definitions.*/
198     const auto FRONTEND_CLASS_DEFS = {&PERF_TEST_DEF};
199     const auto FRONTEND_ENUMERATOR_DEFS = {&PERF_METRIC_DEF};
200     const auto FRONTEND_JSON_DEFS = {&PERF_TEST_STRATEGY_DEF, &PERF_MEASURE_RESULT_DEF};
201     /** The allowed in/out data type scope of frontend apis.*/
202     const std::initializer_list<std::string_view> DATA_TYPE_SCOPE = {
203         "int",
204         "float",
205         "bool",
206         "string",
207         PERF_TEST_DEF.name_,
208         PERF_METRIC_DEF.name_,
209         PERF_TEST_STRATEGY_DEF.name_,
210     };
211 } // namespace OHOS::perftest
212 #endif