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 BaseTestWithScope<false> {
29 public:
30     void SetUp() override
31     {
32         JSRuntimeOptions options;
33         instance = JSNApi::CreateEcmaVM(options);
34         ASSERT_TRUE(instance != nullptr) << "Cannot create EcmaVM";
35         thread = instance->GetJSThread();
36         thread->ManagedCodeBegin();
37         scope = new EcmaHandleScope(thread);
38         instance->SetEnableForceGC(false);
39     }
40 
CreateTaggedArray(uint32_t length, JSTaggedValue initVal, MemSpaceType spaceType)41     JSHandle<TaggedArray> CreateTaggedArray(uint32_t length, JSTaggedValue initVal, MemSpaceType spaceType)
42     {
43         ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
44         return factory->NewTaggedArray(length, initVal, spaceType);
45     }
46 };
47 
HWTEST_F_L0(IncrementalMarkingTest, PerformanceWithIncrementalMarking)48 HWTEST_F_L0(IncrementalMarkingTest, PerformanceWithIncrementalMarking)
49 {
50     uint32_t length = 1_KB;
51     JSHandle<TaggedArray> rootArray =
52         CreateTaggedArray(length, JSTaggedValue::Undefined(), MemSpaceType::OLD_SPACE);
53     for (uint32_t i = 0; i < length; i++) {
54         auto array = CreateTaggedArray(length, JSTaggedValue::Undefined(), MemSpaceType::OLD_SPACE);
55         rootArray->Set(thread, i, array);
56     }
57     auto heap = const_cast<Heap *>(thread->GetEcmaVM()->GetHeap());
58     heap->EnableNotifyIdle();
59     heap->SetIdleTask(IdleTaskType::INCREMENTAL_MARK);  // incremental mark
60     for (uint32_t i = 0; i < length; i++) {
61         auto array = CreateTaggedArray(length, JSTaggedValue::Undefined(), MemSpaceType::OLD_SPACE);
62         rootArray->Set(thread, i, array);
63     }
64     heap->CollectGarbage(TriggerGCType::OLD_GC);
65 }
66 }  // namespace panda::test
67