1 /* 2 * Copyright (c) 2022 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 "distributed_bundle_info.h" 17 18 #include <benchmark/benchmark.h> 19 20 using namespace std; 21 using namespace OHOS; 22 using namespace OHOS::AppExecFwk; 23 24 namespace { 25 /** 26 * @tc.name: BenchmarkTestForReadFromParcel 27 * @tc.desc: Testcase for testing 'ReadFromParcel' function. 28 * @tc.type: FUNC 29 * @tc.require: Issue Number 30 */ BenchmarkTestForReadFromParcel(benchmark::State & state)31 static void BenchmarkTestForReadFromParcel(benchmark::State &state) 32 { 33 DistributedBundleInfo info; 34 info.bundleName = "com.ohos.contactsdataability"; 35 info.appId = "com.ohos.contactsdataability_BNtg4JBClbl92Rgc3jm"\ 36 "/RfcAdrHXaM8F0QOiwVEhnV5ebE5jNIYnAx+weFRT3QTyUjRNdhmc2aAzWyi+5t5CoBM="; 37 Parcel parcel; 38 info.Marshalling(parcel); 39 for (auto _ : state) { 40 /* @tc.steps: step1.call ReadFromParcel in loop */ 41 info.ReadFromParcel(parcel); 42 } 43 } 44 45 /** 46 * @tc.name: BenchmarkTestForMarshalling 47 * @tc.desc: Testcase for testing 'Marshalling' function. 48 * @tc.type: FUNC 49 * @tc.require: Issue Number 50 */ BenchmarkTestForMarshalling(benchmark::State & state)51 static void BenchmarkTestForMarshalling(benchmark::State &state) 52 { 53 DistributedBundleInfo info; 54 info.bundleName = "com.ohos.contactsdataability"; 55 info.appId = "com.ohos.contactsdataability_BNtg4JBClbl92Rgc3jm"\ 56 "/RfcAdrHXaM8F0QOiwVEhnV5ebE5jNIYnAx+weFRT3QTyUjRNdhmc2aAzWyi+5t5CoBM="; 57 Parcel parcel; 58 for (auto _ : state) { 59 /* @tc.steps: step1.call Marshalling in loop */ 60 info.Marshalling(parcel); 61 } 62 } 63 64 /** 65 * @tc.name: BenchmarkTestForUnmarshalling 66 * @tc.desc: Testcase for testing 'Unmarshalling' function. 67 * @tc.type: FUNC 68 * @tc.require: Issue Number 69 */ BenchmarkTestForUnmarshalling(benchmark::State & state)70 static void BenchmarkTestForUnmarshalling(benchmark::State &state) 71 { 72 DistributedBundleInfo info; 73 info.bundleName = "com.ohos.contactsdataability"; 74 info.appId = "com.ohos.contactsdataability_BNtg4JBClbl92Rgc3jm"\ 75 "/RfcAdrHXaM8F0QOiwVEhnV5ebE5jNIYnAx+weFRT3QTyUjRNdhmc2aAzWyi+5t5CoBM="; 76 Parcel parcel; 77 info.Marshalling(parcel); 78 for (auto _ : state) { 79 /* @tc.steps: step1.call Unmarshalling in loop */ 80 info.Unmarshalling(parcel); 81 } 82 } 83 84 /** 85 * @tc.name: BenchmarkTestForToString 86 * @tc.desc: Testcase for testing 'ToString' function. 87 * @tc.type: FUNC 88 * @tc.require: Issue Number 89 */ BenchmarkTestForToString(benchmark::State & state)90 static void BenchmarkTestForToString(benchmark::State &state) 91 { 92 DistributedBundleInfo info; 93 info.bundleName = "com.ohos.contactsdataability"; 94 info.appId = "com.ohos.contactsdataability_BNtg4JBClbl92Rgc3jm"\ 95 "/RfcAdrHXaM8F0QOiwVEhnV5ebE5jNIYnAx+weFRT3QTyUjRNdhmc2aAzWyi+5t5CoBM="; 96 for (auto _ : state) { 97 /* @tc.steps: step1.call ToString in loop */ 98 info.ToString(); 99 } 100 } 101 102 /** 103 * @tc.name: BenchmarkTestForFromJsonString 104 * @tc.desc: Testcase for testing 'FromJsonString' function. 105 * @tc.type: FUNC 106 * @tc.require: Issue Number 107 */ BenchmarkTestForFromJsonString(benchmark::State & state)108 static void BenchmarkTestForFromJsonString(benchmark::State &state) 109 { 110 DistributedBundleInfo info; 111 const std::string jsonString = R"({"appId": "ohos.global.systemres_BNtg4JBClbl92Rgc3jm/ 112 RfcAdrHXaM8F0QOiwVEhnV5ebE5jNIYnAx+weFRT3QTyUjRNdhmc2aAzWyi+5t5CoBM=", 113 "moduleInfos": [{ 114 "moduleName":"ohos.global.systemres", 115 "abilities": [{ 116 "abilityName":"", 117 "permissions":[], 118 "type":"page", 119 "enabled":true, 120 }] 121 }], 122 "compatibleVersionCode": 3, 123 "mainAbility": "", 124 "minCompatibleVersion": 1, 125 "bundleName": "ohos.global.systemres", 126 "targetVersionCode": 3, 127 "version": 1, 128 "versionCode": 1, 129 "versionName": "1.0.0.1"})"; 130 for (auto _ : state) { 131 /* @tc.steps: step1.call FromJsonString in loop */ 132 info.FromJsonString(jsonString); 133 } 134 } 135 136 BENCHMARK(BenchmarkTestForReadFromParcel)->Iterations(1000); 137 BENCHMARK(BenchmarkTestForMarshalling)->Iterations(1000); 138 BENCHMARK(BenchmarkTestForUnmarshalling)->Iterations(1000); 139 BENCHMARK(BenchmarkTestForToString)->Iterations(1000); 140 BENCHMARK(BenchmarkTestForFromJsonString)->Iterations(1000); 141 } 142 143 BENCHMARK_MAIN();