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