1 /*
2 * Copyright (c) 2021-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 <benchmark/benchmark.h>
17
18 #include "bundle_constants.h"
19 #include "bundle_installer_interface.h"
20 #include "bundle_mgr_interface.h"
21 #include "iservice_registry.h"
22 #include "system_ability_definition.h"
23 #include "status_receiver_host.h"
24
25 using namespace std::chrono_literals;
26 using namespace OHOS;
27 using namespace OHOS::AppExecFwk;
28 namespace {
29 const std::string THIRD_BUNDLE_PATH = "/data/test/benchmark/";
30
31 class InstallerProxyTest : public StatusReceiverHost {
32 public:
33 InstallerProxyTest();
34 virtual ~InstallerProxyTest() override;
35 virtual void OnStatusNotify(const int progress) override;
36 virtual void OnFinished(const int32_t resultCode, const std::string &resultMsg) override;
37 };
38
InstallerProxyTest()39 InstallerProxyTest::InstallerProxyTest()
40 {}
41
~InstallerProxyTest()42 InstallerProxyTest::~InstallerProxyTest()
43 {}
44
OnStatusNotify(const int progress)45 void InstallerProxyTest::OnStatusNotify(const int progress)
46 {}
47
OnFinished(const int32_t resultCode,const std::string & resultMsg)48 void InstallerProxyTest::OnFinished(const int32_t resultCode, const std::string &resultMsg)
49 {}
50
GetBundleMgrProxy()51 sptr<IBundleMgr> GetBundleMgrProxy()
52 {
53 sptr<ISystemAbilityManager> systemAbilityManager =
54 SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
55 if (!systemAbilityManager) {
56 return nullptr;
57 }
58
59 sptr<IRemoteObject> remoteObject = systemAbilityManager->GetSystemAbility(BUNDLE_MGR_SERVICE_SYS_ABILITY_ID);
60 if (!remoteObject) {
61 return nullptr;
62 }
63
64 return iface_cast<IBundleMgr>(remoteObject);
65 }
66
GetInstallerProxy()67 sptr<IBundleInstaller> GetInstallerProxy()
68 {
69 sptr<IBundleMgr> bundleMgrProxy = GetBundleMgrProxy();
70 if (!bundleMgrProxy) {
71 return nullptr;
72 }
73
74 sptr<IBundleInstaller> installerProxy = bundleMgrProxy->GetBundleInstaller();
75 if (!installerProxy) {
76 return nullptr;
77 }
78
79 return installerProxy;
80 }
81
82 /**
83 * @tc.name: BenchmarkTestInstallerProxyInfo
84 * @tc.desc: Testcase for testing Installs an application through the proxy object.
85 * @tc.type: FUNC
86 * @tc.require: Issue Number
87 */
88
BenchmarkTestInstallerProxyInfo(benchmark::State & state)89 static void BenchmarkTestInstallerProxyInfo(benchmark::State &state)
90 {
91 sptr<IBundleInstaller> installerProxy = GetInstallerProxy();
92 std::string bundleFilePath = THIRD_BUNDLE_PATH + "test.hap";
93 InstallParam installParam;
94 installParam.installFlag = InstallFlag::REPLACE_EXISTING;
95 sptr<InstallerProxyTest> statusReceiver(new (std::nothrow) InstallerProxyTest());
96 for (auto _ : state) {
97 /* @tc.steps: step1.call ReadFromParcel in loop */
98 installerProxy->Install(bundleFilePath, installParam, statusReceiver);
99 }
100 }
101
102 /**
103 * @tc.name: BenchmarkTestInstallerProxyInfo
104 * @tc.desc: Testcase for testing Installs multiple haps.
105 * @tc.type: FUNC
106 * @tc.require: Issue Number
107 */
108
BenchmarkTestMultipleInstallerProxyInfo(benchmark::State & state)109 static void BenchmarkTestMultipleInstallerProxyInfo(benchmark::State &state)
110 {
111 sptr<IBundleInstaller> installerProxy = GetInstallerProxy();
112 std::vector<std::string> bundleFilePaths;
113 std::string bundleFilePath = THIRD_BUNDLE_PATH + "test.hap";
114 bundleFilePaths.push_back(bundleFilePath);
115 InstallParam installParam;
116 installParam.installFlag = InstallFlag::REPLACE_EXISTING;
117 sptr<InstallerProxyTest> statusReceiver(new (std::nothrow) InstallerProxyTest());
118 for (auto _ : state) {
119 /* @tc.steps: step1.call ReadFromParcel in loop */
120 installerProxy->Install(bundleFilePaths, installParam, statusReceiver);
121 }
122 }
123
124 /**
125 * @tc.name: BenchmarkTestInstallerProxyInfo
126 * @tc.desc: Testcase for testing Uninstalls an application through the proxy object.
127 * @tc.type: FUNC
128 * @tc.require: Issue Number
129 */
130
BenchmarkTestUninstallerApplication(benchmark::State & state)131 static void BenchmarkTestUninstallerApplication(benchmark::State &state)
132 {
133 sptr<IBundleInstaller> installerProxy = GetInstallerProxy();
134 InstallParam installParam;
135 installParam.installFlag = InstallFlag::REPLACE_EXISTING;
136 installParam.userId = Constants::DEFAULT_USERID;
137 sptr<InstallerProxyTest> statusReceiver = (new (std::nothrow) InstallerProxyTest());
138 const std::string bundleName = "com.example.l3jsdemo";
139 std::string bundleFilePath = THIRD_BUNDLE_PATH + "test.hap";
140 installerProxy->Install(bundleFilePath, installParam, statusReceiver);
141 for (auto _ : state) {
142 /* @tc.steps: step1.call ReadFromParcel in loop */
143 installerProxy->Uninstall(bundleName, installParam, statusReceiver);
144 }
145 }
146
147 BENCHMARK(BenchmarkTestInstallerProxyInfo)->Iterations(1000);
148 BENCHMARK(BenchmarkTestMultipleInstallerProxyInfo)->Iterations(1000);
149 BENCHMARK(BenchmarkTestUninstallerApplication)->Iterations(1000);
150 } // namespace
151
152 BENCHMARK_MAIN();