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
16 #include "ecmascript/tests/test_helper.h"
17
18 #include "ecmascript/ecma_vm.h"
19 #include "ecmascript/global_env.h"
20 #include "ecmascript/js_handle.h"
21 #include "ecmascript/mem/clock_scope.h"
22 #include "ecmascript/mem/incremental_marker.h"
23 #include "ecmascript/mem/verification.h"
24
25 using namespace panda::ecmascript;
26
27 namespace panda::test {
28 class IncrementalMarkingTest : public testing::Test {
29 public:
SetUpTestCase()30 static void SetUpTestCase()
31 {
32 GTEST_LOG_(INFO) << "SetUpTestCase";
33 }
34
TearDownTestCase()35 static void TearDownTestCase()
36 {
37 GTEST_LOG_(INFO) << "TearDownCase";
38 }
39
SetUp()40 void SetUp() override
41 {
42 JSRuntimeOptions options;
43 instance = JSNApi::CreateEcmaVM(options);
44 ASSERT_TRUE(instance != nullptr) << "Cannot create EcmaVM";
45 thread = instance->GetJSThread();
46 scope = new EcmaHandleScope(thread);
47 instance->SetEnableForceGC(false);
48 }
49
TearDown()50 void TearDown() override
51 {
52 TestHelper::DestroyEcmaVMWithScope(instance, scope);
53 }
54
CreateTaggedArray(uint32_t length,JSTaggedValue initVal,MemSpaceType spaceType)55 JSHandle<TaggedArray> CreateTaggedArray(uint32_t length, JSTaggedValue initVal, MemSpaceType spaceType)
56 {
57 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
58 return factory->NewTaggedArray(length, initVal, spaceType);
59 }
60
61 EcmaVM *instance {nullptr};
62 ecmascript::EcmaHandleScope *scope {nullptr};
63 JSThread *thread {nullptr};
64 };
65
HWTEST_F_L0(IncrementalMarkingTest,PerformanceWithIncrementalMarking)66 HWTEST_F_L0(IncrementalMarkingTest, PerformanceWithIncrementalMarking)
67 {
68 uint32_t length = 1_KB;
69 JSHandle<TaggedArray> rootArray =
70 CreateTaggedArray(length, JSTaggedValue::Undefined(), MemSpaceType::OLD_SPACE);
71 for (uint32_t i = 0; i < length; i++) {
72 auto array = CreateTaggedArray(length, JSTaggedValue::Undefined(), MemSpaceType::OLD_SPACE);
73 rootArray->Set(thread, i, array);
74 }
75 auto heap = const_cast<Heap *>(thread->GetEcmaVM()->GetHeap());
76 heap->EnableNotifyIdle();
77 heap->SetIdleTask(IdleTaskType::INCREMENTAL_MARK); // incremental mark
78 for (uint32_t i = 0; i < length; i++) {
79 auto array = CreateTaggedArray(length, JSTaggedValue::Undefined(), MemSpaceType::OLD_SPACE);
80 rootArray->Set(thread, i, array);
81 }
82 heap->CollectGarbage(TriggerGCType::OLD_GC);
83 }
84 } // namespace panda::test
85