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 #include <thread>
16 #include <unistd.h>
17 #include <vector>
18
19 #include "hiperf_client.h"
20
21 using namespace OHOS::Developtools::HiPerf;
22
23 namespace HiperfClientDemo {
TestCodeThread(int id)24 void TestCodeThread(int id)
25 {
26 constexpr uint32_t k = 1024;
27 constexpr uint32_t k2 = 2 * k;
28 constexpr uint32_t k10 = 10 * k;
29 constexpr uint32_t two = 2;
30 constexpr uint64_t thousand = 1000;
31
32 std::vector<std::unique_ptr<char[]>> mems;
33 printf("TestCodeThread %d:++\n", id);
34 std::this_thread::sleep_for(std::chrono::milliseconds(thousand));
35
36 for (uint32_t i = 0; i < k10; i++) {
37 if (i % two == 0) {
38 mems.push_back(std::make_unique<char[]>(k));
39 } else {
40 mems.push_back(std::make_unique<char[]>(k2));
41 }
42 }
43
44 for (uint32_t i = 0; i < k10; i++) {
45 mems.pop_back();
46 }
47
48 std::this_thread::sleep_for(std::chrono::milliseconds(thousand));
49 printf("TestCodeThread %d:--\n", id);
50 }
51 } // namespace HiperfClientDemo
52
53 using namespace HiperfClientDemo;
54
main()55 int main()
56 {
57 const int waitTime = 2;
58 const int countTry = 3;
59
60 HiperfClient::Client myHiperf;
61 printf("GetOutputDir:'%s'\n", myHiperf.GetOutputDir().c_str());
62 printf("GetCommandPath:'%s'\n", myHiperf.GetCommandPath().c_str());
63 myHiperf.SetDebugMode();
64 printf("demo start\n");
65 HiperfClient::RecordOption opt;
66 const int timeout = 30;
67 opt.SetAppPackage("com.ohos.launcher");
68 opt.SetTimeStopSec(timeout);
69 if (myHiperf.Start(opt)) {
70 printf("demo start successfully\n");
71 }
72 std::thread workload(TestCodeThread, 0);
73 sleep(waitTime);
74 // try for each thread times
75 for (int i = 0; i < countTry; i++) {
76 printf("demo pause\n");
77 if (!myHiperf.Pause()) {
78 printf("demo pause failed\n");
79 }
80 sleep(1);
81 printf("demo resume \n");
82 if (!myHiperf.Resume()) {
83 printf("demo resume failed\n");
84 }
85 sleep(1);
86 }
87 printf("demo stop \n");
88 if (myHiperf.Stop()) {
89 printf("demo stop successfully\n");
90 }
91 workload.join();
92 return 0;
93 };
94