1 /* 2 * Copyright (c) 2023 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 <iostream> 16 #include "common.h" 17 #include "mem_profiler_collector.h" 18 #include "native_memory_profiler_sa_client_manager.h" 19 #include <thread> 20 #include <gtest/gtest.h> 21 22 using namespace testing::ext; 23 using namespace OHOS::HiviewDFX; 24 using namespace OHOS::HiviewDFX::UCollectUtil; 25 using namespace OHOS::HiviewDFX::UCollect; 26 using namespace OHOS::Developtools::NativeDaemon; 27 28 const static std::string NATIVE_DAEMON_NAME("native_daemon"); 29 int g_nativeDaemonPid = 0; 30 constexpr int WAIT_EXIT_MILLS = 50; 31 constexpr int FINAL_TIME = 1000; 32 constexpr int DURATION = 10; 33 constexpr int INTERVAL = 1; 34 35 class MemProfilerCollectorTest : public testing::Test { 36 public: SetUp()37 void SetUp() {}; TearDown()38 void TearDown() {}; SetUpTestCase()39 static void SetUpTestCase() {}; TearDownTestCase()40 static void TearDownTestCase() {}; 41 }; 42 43 /** 44 * @tc.name: MemProfilerCollectorTest001 45 * @tc.desc: used to test MemProfilerCollector.Start 46 * @tc.type: FUNC 47 */ 48 HWTEST_F(MemProfilerCollectorTest, MemProfilerCollectorTest001, TestSize.Level1) 49 { 50 std::shared_ptr<MemProfilerCollector> collector = MemProfilerCollector::Create(); 51 collector->Start(NativeMemoryProfilerSaClientManager::NativeMemProfilerType::MEM_PROFILER_LIBRARY, 52 0, DURATION, INTERVAL); 53 int time = 0; 54 while (!COMMON::IsProcessExist(NATIVE_DAEMON_NAME, g_nativeDaemonPid) && time < FINAL_TIME) { 55 std::this_thread::sleep_for(std::chrono::milliseconds(WAIT_EXIT_MILLS)); 56 time += WAIT_EXIT_MILLS; 57 } 58 ASSERT_TRUE(time < FINAL_TIME); 59 } 60 61 /** 62 * @tc.name: MemProfilerCollectorTest002 63 * @tc.desc: used to test MemProfilerCollector.Stop 64 * @tc.type: FUNC 65 */ 66 HWTEST_F(MemProfilerCollectorTest, MemProfilerCollectorTest002, TestSize.Level1) 67 { 68 std::shared_ptr<MemProfilerCollector> collector = MemProfilerCollector::Create(); 69 collector->Start(NativeMemoryProfilerSaClientManager::NativeMemProfilerType::MEM_PROFILER_LIBRARY, 70 0, DURATION, INTERVAL); 71 std::this_thread::sleep_for(std::chrono::milliseconds(FINAL_TIME)); 72 collector->Stop(0); 73 int time = 0; 74 while (COMMON::IsProcessExist(NATIVE_DAEMON_NAME, g_nativeDaemonPid) && time < FINAL_TIME) { 75 std::this_thread::sleep_for(std::chrono::milliseconds(WAIT_EXIT_MILLS)); 76 time += WAIT_EXIT_MILLS; 77 } 78 ASSERT_FALSE(time < FINAL_TIME); 79 }