14514f5e3Sopenharmony_ci/* 24514f5e3Sopenharmony_ci * Copyright (c) 2023 Huawei Device Co., Ltd. 34514f5e3Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 44514f5e3Sopenharmony_ci * you may not use this file except in compliance with the License. 54514f5e3Sopenharmony_ci * You may obtain a copy of the License at 64514f5e3Sopenharmony_ci * 74514f5e3Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 84514f5e3Sopenharmony_ci * 94514f5e3Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software 104514f5e3Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 114514f5e3Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 124514f5e3Sopenharmony_ci * See the License for the specific language governing permissions and 134514f5e3Sopenharmony_ci * limitations under the License. 144514f5e3Sopenharmony_ci */ 154514f5e3Sopenharmony_ci 164514f5e3Sopenharmony_ci#include <cstdio> 174514f5e3Sopenharmony_ci 184514f5e3Sopenharmony_ci#include "ecmascript/dfx/hprof/heap_profiler_interface.h" 194514f5e3Sopenharmony_ci#include "ecmascript/dfx/hprof/heap_profiler.h" 204514f5e3Sopenharmony_ci#include "ecmascript/dfx/hprof/heap_sampling.h" 214514f5e3Sopenharmony_ci#include "ecmascript/dfx/stackinfo/js_stackgetter.h" 224514f5e3Sopenharmony_ci#include "ecmascript/tests/test_helper.h" 234514f5e3Sopenharmony_ci 244514f5e3Sopenharmony_ciusing namespace panda::ecmascript; 254514f5e3Sopenharmony_ci 264514f5e3Sopenharmony_cinamespace panda::test { 274514f5e3Sopenharmony_ciclass HeapSamplingTest : public testing::Test { 284514f5e3Sopenharmony_cipublic: 294514f5e3Sopenharmony_ci static void SetUpTestCase() 304514f5e3Sopenharmony_ci { 314514f5e3Sopenharmony_ci GTEST_LOG_(INFO) << "SetUpTestCase"; 324514f5e3Sopenharmony_ci } 334514f5e3Sopenharmony_ci 344514f5e3Sopenharmony_ci static void TearDownTestCase() 354514f5e3Sopenharmony_ci { 364514f5e3Sopenharmony_ci GTEST_LOG_(INFO) << "TearDownCase"; 374514f5e3Sopenharmony_ci } 384514f5e3Sopenharmony_ci 394514f5e3Sopenharmony_ci void SetUp() override 404514f5e3Sopenharmony_ci { 414514f5e3Sopenharmony_ci TestHelper::CreateEcmaVMWithScope(instance, thread, scope); 424514f5e3Sopenharmony_ci instance->SetEnableForceGC(false); 434514f5e3Sopenharmony_ci } 444514f5e3Sopenharmony_ci 454514f5e3Sopenharmony_ci void TearDown() override 464514f5e3Sopenharmony_ci { 474514f5e3Sopenharmony_ci TestHelper::DestroyEcmaVMWithScope(instance, scope); 484514f5e3Sopenharmony_ci } 494514f5e3Sopenharmony_ci 504514f5e3Sopenharmony_ci EcmaVM *instance {nullptr}; 514514f5e3Sopenharmony_ci EcmaHandleScope *scope {nullptr}; 524514f5e3Sopenharmony_ci JSThread *thread {nullptr}; 534514f5e3Sopenharmony_ci}; 544514f5e3Sopenharmony_ci 554514f5e3Sopenharmony_ciHWTEST_F_L0(HeapSamplingTest, StartHeapSampling) 564514f5e3Sopenharmony_ci{ 574514f5e3Sopenharmony_ci HeapProfilerInterface *heapProfile = HeapProfilerInterface::GetInstance(instance); 584514f5e3Sopenharmony_ci uint64_t samplingInterval = 1 << 15; // default interval 594514f5e3Sopenharmony_ci int stackDepth = 128; // default depth 604514f5e3Sopenharmony_ci bool result = heapProfile->StartHeapSampling(samplingInterval, stackDepth); 614514f5e3Sopenharmony_ci ASSERT_TRUE(result); 624514f5e3Sopenharmony_ci result = heapProfile->StartHeapSampling(samplingInterval, stackDepth); 634514f5e3Sopenharmony_ci ASSERT_FALSE(result); 644514f5e3Sopenharmony_ci} 654514f5e3Sopenharmony_ci 664514f5e3Sopenharmony_ciHWTEST_F_L0(HeapSamplingTest, StopHeapSampling) 674514f5e3Sopenharmony_ci{ 684514f5e3Sopenharmony_ci HeapProfilerInterface *heapProfile = HeapProfilerInterface::GetInstance(instance); 694514f5e3Sopenharmony_ci uint64_t samplingInterval = 1 << 15; // default interval 704514f5e3Sopenharmony_ci int stackDepth = 128; // default depth 714514f5e3Sopenharmony_ci bool result = heapProfile->StartHeapSampling(samplingInterval, stackDepth); 724514f5e3Sopenharmony_ci ASSERT_TRUE(result); 734514f5e3Sopenharmony_ci heapProfile->StopHeapSampling(); 744514f5e3Sopenharmony_ci result = heapProfile->StartHeapSampling(samplingInterval, stackDepth); 754514f5e3Sopenharmony_ci ASSERT_TRUE(result); 764514f5e3Sopenharmony_ci} 774514f5e3Sopenharmony_ci 784514f5e3Sopenharmony_ciHWTEST_F_L0(HeapSamplingTest, GetAllocationProfile) 794514f5e3Sopenharmony_ci{ 804514f5e3Sopenharmony_ci HeapProfilerInterface *heapProfile = HeapProfilerInterface::GetInstance(instance); 814514f5e3Sopenharmony_ci const SamplingInfo *result = heapProfile->GetAllocationProfile(); 824514f5e3Sopenharmony_ci ASSERT_TRUE(result == nullptr); 834514f5e3Sopenharmony_ci uint64_t samplingInterval = 1 << 15; // default interval 844514f5e3Sopenharmony_ci int stackDepth = 128; // default depth 854514f5e3Sopenharmony_ci heapProfile->StartHeapSampling(samplingInterval, stackDepth); 864514f5e3Sopenharmony_ci result = heapProfile->GetAllocationProfile(); 874514f5e3Sopenharmony_ci EXPECT_TRUE(result != nullptr); 884514f5e3Sopenharmony_ci EXPECT_TRUE(result->head_.callFrameInfo_.functionName_ == "(root)"); 894514f5e3Sopenharmony_ci} 904514f5e3Sopenharmony_ci 914514f5e3Sopenharmony_ciHWTEST_F_L0(HeapSamplingTest, ImplementSampling) 924514f5e3Sopenharmony_ci{ 934514f5e3Sopenharmony_ci uint64_t samplingInterval = 1 << 15; // default interval 944514f5e3Sopenharmony_ci int stackDepth = 128; // default depth 954514f5e3Sopenharmony_ci std::unique_ptr<HeapSampling> heapSampling = std::make_unique<HeapSampling>(instance, 964514f5e3Sopenharmony_ci const_cast<Heap *>(instance->GetHeap()), samplingInterval, stackDepth); 974514f5e3Sopenharmony_ci size_t size = 1U << 15U; // default size 984514f5e3Sopenharmony_ci Address addr = 0; 994514f5e3Sopenharmony_ci heapSampling->ImplementSampling(addr, size); 1004514f5e3Sopenharmony_ci const SamplingInfo *result = heapSampling->GetAllocationProfile(); 1014514f5e3Sopenharmony_ci EXPECT_TRUE(result != nullptr); 1024514f5e3Sopenharmony_ci EXPECT_TRUE(result->samples_[0].size_ == size); 1034514f5e3Sopenharmony_ci} 1044514f5e3Sopenharmony_ci} // namespace panda::test 105