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
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/concurrent_marker.h"
23 #include "ecmascript/mem/verification.h"
24
25 using namespace panda::ecmascript;
26
27 namespace panda::test {
28 class ConcurrentMarkingTest : 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 auto heap = const_cast<Heap *>(thread->GetEcmaVM()->GetHeap());
49 heap->GetConcurrentMarker()->EnableConcurrentMarking(EnableConcurrentMarkType::ENABLE);
50 }
51
TearDown()52 void TearDown() override
53 {
54 TestHelper::DestroyEcmaVMWithScope(instance, scope);
55 }
56
CreateTaggedArray(uint32_t length,JSTaggedValue initVal,MemSpaceType spaceType)57 JSHandle<TaggedArray> CreateTaggedArray(uint32_t length, JSTaggedValue initVal, MemSpaceType spaceType)
58 {
59 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
60 return factory->NewTaggedArray(length, initVal, spaceType);
61 }
62
63 EcmaVM *instance {nullptr};
64 ecmascript::EcmaHandleScope *scope {nullptr};
65 JSThread *thread {nullptr};
66 };
67
HWTEST_F_L0(ConcurrentMarkingTest,PerformanceWithConcurrentMarking)68 HWTEST_F_L0(ConcurrentMarkingTest, PerformanceWithConcurrentMarking)
69 {
70 uint32_t length = 1_KB;
71 JSHandle<TaggedArray> rootArray =
72 CreateTaggedArray(length, JSTaggedValue::Undefined(), MemSpaceType::OLD_SPACE);
73 for (uint32_t i = 0; i < length; i++) {
74 auto array = CreateTaggedArray(length, JSTaggedValue::Undefined(), MemSpaceType::OLD_SPACE);
75 rootArray->Set(thread, i, array);
76 }
77 auto heap = const_cast<Heap *>(thread->GetEcmaVM()->GetHeap());
78 heap->TriggerConcurrentMarking(); // concurrent mark
79 for (uint32_t i = 0; i < length; i++) {
80 auto array = CreateTaggedArray(length, JSTaggedValue::Undefined(), MemSpaceType::OLD_SPACE);
81 rootArray->Set(thread, i, array);
82 }
83 heap->CollectGarbage(TriggerGCType::OLD_GC);
84 }
85
HWTEST_F_L0(ConcurrentMarkingTest,PerformanceWithoutConcurrentMarking)86 HWTEST_F_L0(ConcurrentMarkingTest, PerformanceWithoutConcurrentMarking)
87 {
88 uint32_t length = 1_KB;
89 JSHandle<TaggedArray> rootArray =
90 CreateTaggedArray(length, JSTaggedValue::Undefined(), MemSpaceType::OLD_SPACE);
91 for (uint32_t i = 0; i < length; i++) {
92 auto array = CreateTaggedArray(length, JSTaggedValue::Undefined(), MemSpaceType::OLD_SPACE);
93 rootArray->Set(thread, i, array);
94 }
95 auto heap = const_cast<Heap *>(thread->GetEcmaVM()->GetHeap());
96 for (uint32_t i = 0; i < length; i++) {
97 auto array = CreateTaggedArray(length, JSTaggedValue::Undefined(), MemSpaceType::OLD_SPACE);
98 rootArray->Set(thread, i, array);
99 }
100 heap->CollectGarbage(TriggerGCType::OLD_GC);
101 }
102 } // namespace panda::test
103