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 <benchmark/benchmark.h>
17 #include <algorithm>
18 #include <fstream>
19 #include <iostream>
20 #include <unistd.h>
21 #include <sys/mman.h>
22 #include "directory_ex.h"
23 #include "securec.h"
24 #include "parcel.h"
25 #include "refbase.h"
26 #include "ashmem.h"
27 #include "benchmark_log.h"
28 #include "benchmark_assert.h"
29 using namespace std;
30 
31 namespace OHOS {
32 namespace {
33 
34 const int MAX_PARCEL_SIZE = 1000;
35 char g_data[MAX_PARCEL_SIZE];
36 const int32_t MEMORY_SIZE = 1024;
37 const std::string MEMORY_CONTENT = "HelloWorld2020\0";
38 const std::string MEMORY_NAME = "Test SharedMemory\0";
39 
40 class BenchmarkAshmemTest : public benchmark::Fixture {
41 public:
BenchmarkAshmemTest()42     BenchmarkAshmemTest()
43     {
44         Iterations(iterations);
45         Repetitions(repetitions);
46         ReportAggregatesOnly(true);
47     }
48 
49     ~BenchmarkAshmemTest() override = default;
SetUp(const::benchmark::State & state)50     void SetUp(const ::benchmark::State& state) override
51     {
52     };
53 
TearDown(const::benchmark::State & state)54     void TearDown(const ::benchmark::State& state) override
55     {
56         for (int i = 0; i < MAX_PARCEL_SIZE; i++) {
57             g_data[i] = 0;
58         }
59     }
60 
61 protected:
62     const int32_t repetitions = 3;
63     const int32_t iterations = 1000;
64 };
65 
66 /**
67  * @tc.name: test_ashmem_WriteAndRead_001
68  * @tc.desc: create and map ashmem
69  * @tc.type: FUNC
70  */
BENCHMARK_F(BenchmarkAshmemTest,test_ashmem_WriteAndRead_001)71 BENCHMARK_F(BenchmarkAshmemTest, test_ashmem_WriteAndRead_001)(benchmark::State& state)
72 {
73     BENCHMARK_LOGD("AshmemTest test_ashmem_WriteAndRead_001 start.");
74     while (state.KeepRunning()) {
75         sptr<Ashmem> ashmem = Ashmem::CreateAshmem(MEMORY_NAME.c_str(), MEMORY_SIZE);
76         AssertUnequal(ashmem, nullptr, "ashmem != nullptr did not equal true as expected.", state);
77         AssertEqual(ashmem->GetAshmemSize(), MEMORY_SIZE,
78             "ashmem->GetAshmemSize() == MEMORY_SIZE did not equal true as expected.", state);
79 
80         bool ret = ashmem->MapAshmem(PROT_READ | PROT_WRITE);
81         AssertTrue(ret, "ret did not equal true as expected.", state);
82 
83         ashmem->UnmapAshmem();
84         ashmem->CloseAshmem();
85     }
86     BENCHMARK_LOGD("AshmemTest test_ashmem_WriteAndRead_001 end.");
87 }
88 
89 /**
90  * @tc.name: test_ashmem_WriteAndRead_002
91  * @tc.desc: write to and read from ashmem
92  * @tc.type: FUNC
93  */
BENCHMARK_F(BenchmarkAshmemTest,test_ashmem_WriteAndRead_002)94 BENCHMARK_F(BenchmarkAshmemTest, test_ashmem_WriteAndRead_002)(benchmark::State& state)
95 {
96     BENCHMARK_LOGD("AshmemTest test_ashmem_WriteAndRead_002 start.");
97     while (state.KeepRunning()) {
98         sptr<Ashmem> ashmem = Ashmem::CreateAshmem(MEMORY_NAME.c_str(), MEMORY_SIZE);
99         AssertUnequal(ashmem, nullptr, "ashmem != nullptr did not equal true as expected.", state);
100 
101         bool ret = ashmem->MapReadAndWriteAshmem();
102         AssertTrue(ret, "ret did not equal true as expected.", state);
103 
104         ret = ashmem->WriteToAshmem(MEMORY_CONTENT.c_str(), sizeof(MEMORY_CONTENT), 0);
105         AssertTrue(ret, "ret did not equal true as expected.", state);
106 
107         ret = ashmem->WriteToAshmem(MEMORY_CONTENT.c_str(), sizeof(MEMORY_CONTENT), sizeof(MEMORY_CONTENT));
108         AssertTrue(ret, "ret did not equal true as expected.", state);
109 
110         auto readData = ashmem->ReadFromAshmem(sizeof(MEMORY_CONTENT), 0);
111         AssertUnequal(readData, nullptr, "readData != nullptr did not equal true as expected.", state);
112 
113         const char *readContent = reinterpret_cast<const char *>(readData);
114         AssertEqual(memcmp(MEMORY_CONTENT.c_str(), readContent, sizeof(MEMORY_CONTENT)), 0,
115             "memcmp(MEMORY_CONTENT.c_str(), readContent, sizeof(MEMORY_CONTENT)) did not equal 0 as expected.", state);
116 
117         readData = ashmem->ReadFromAshmem(sizeof(MEMORY_CONTENT), sizeof(MEMORY_CONTENT));
118         AssertUnequal(readData, nullptr, "readData != nullptr did not equal true as expected.", state);
119 
120         readContent = reinterpret_cast<const char *>(readData);
121         AssertEqual(memcmp(MEMORY_CONTENT.c_str(), readContent, sizeof(MEMORY_CONTENT)), 0,
122             "memcmp(MEMORY_CONTENT.c_str(), readContent, sizeof(MEMORY_CONTENT)) did not equal 0 as expected.", state);
123 
124         ashmem->UnmapAshmem();
125         ashmem->CloseAshmem();
126     }
127     BENCHMARK_LOGD("AshmemTest test_ashmem_WriteAndRead_002 end.");
128 }
129 
130 /**
131  * @tc.name: test_ashmem_WriteAndRead_003
132  * @tc.desc: test read-only ashmem
133  * @tc.type: FUNC
134  */
BENCHMARK_F(BenchmarkAshmemTest,test_ashmem_WriteAndRead_003)135 BENCHMARK_F(BenchmarkAshmemTest, test_ashmem_WriteAndRead_003)(benchmark::State& state)
136 {
137     BENCHMARK_LOGD("AshmemTest test_ashmem_WriteAndRead_003 begin");
138     while (state.KeepRunning()) {
139         sptr<Ashmem> ashmem = Ashmem::CreateAshmem(MEMORY_NAME.c_str(), MEMORY_SIZE);
140         AssertUnequal(ashmem, nullptr, "ashmem != nullptr did not equal true as expected.", state);
141 
142         bool ret = ashmem->MapReadAndWriteAshmem();
143         AssertTrue(ret, "ret did not equal true as expected.", state);
144 
145         ret = ashmem->WriteToAshmem(MEMORY_CONTENT.c_str(), sizeof(MEMORY_CONTENT), 0);
146         AssertTrue(ret, "ret did not equal true as expected.", state);
147 
148         ashmem->UnmapAshmem();
149 
150         ret = ashmem->MapReadOnlyAshmem();
151         AssertTrue(ret, "ret did not equal true as expected.", state);
152 
153         ret = ashmem->WriteToAshmem(MEMORY_CONTENT.c_str(), sizeof(MEMORY_CONTENT), sizeof(MEMORY_CONTENT));
154         AssertFalse(ret, "ret did not equal false", state);
155 
156         auto readData = ashmem->ReadFromAshmem(sizeof(MEMORY_CONTENT), 0);
157         AssertUnequal(readData, nullptr, "readData != nullptr did not equal true as expected.", state);
158 
159         const char *readContent = reinterpret_cast<const char *>(readData);
160         AssertEqual(memcmp(MEMORY_CONTENT.c_str(), readContent, sizeof(MEMORY_CONTENT)), 0,
161             "memcmp(MEMORY_CONTENT.c_str(), readContent, sizeof(MEMORY_CONTENT)) did not equal 0 as expected.", state);
162 
163         ashmem->UnmapAshmem();
164         ashmem->CloseAshmem();
165     }
166     BENCHMARK_LOGD("AshmemTest test_ashmem_WriteAndRead_003 end.");
167 }
168 
169 /**
170  * @tc.name: test_ashmem_WriteAndRead_004
171  * @tc.desc: set read-only protection and map again
172  * @tc.type: FUNC
173  */
BENCHMARK_F(BenchmarkAshmemTest,test_ashmem_WriteAndRead_004)174 BENCHMARK_F(BenchmarkAshmemTest, test_ashmem_WriteAndRead_004)(benchmark::State& state)
175 {
176     BENCHMARK_LOGD("AshmemTest test_ashmem_WriteAndRead_004 start.");
177     while (state.KeepRunning()) {
178         sptr<Ashmem> ashmem = Ashmem::CreateAshmem(MEMORY_NAME.c_str(), MEMORY_SIZE);
179         AssertUnequal(ashmem, nullptr, "ashmem != nullptr did not equal true as expected.", state);
180 
181         bool ret = ashmem->MapReadAndWriteAshmem();
182         AssertTrue(ret, "ret did not equal true as expected.", state);
183 
184         ret = ashmem->WriteToAshmem(MEMORY_CONTENT.c_str(), sizeof(MEMORY_CONTENT), 0);
185         AssertTrue(ret, "ret did not equal true as expected.", state);
186 
187         ashmem->UnmapAshmem();
188 
189         ret = ashmem->SetProtection(PROT_READ);
190         AssertTrue(ret, "ret did not equal true as expected.", state);
191 
192         ret = ashmem->MapReadAndWriteAshmem();
193         AssertFalse(ret, "ret did not equal false", state);
194 
195         ret = ashmem->MapReadOnlyAshmem();
196         AssertTrue(ret, "ret did not equal true as expected.", state);
197 
198         auto readData = ashmem->ReadFromAshmem(sizeof(MEMORY_CONTENT), 0);
199         AssertUnequal(readData, nullptr, "readData != nullptr did not equal true as expected.", state);
200 
201         const char *readContent = reinterpret_cast<const char *>(readData);
202         AssertEqual(memcmp(MEMORY_CONTENT.c_str(), readContent, sizeof(MEMORY_CONTENT)), 0,
203             "memcmp(MEMORY_CONTENT.c_str(), readContent, sizeof(MEMORY_CONTENT)) did not equal 0 as expected.", state);
204 
205         ashmem->UnmapAshmem();
206         ashmem->CloseAshmem();
207     }
208     BENCHMARK_LOGD("AshmemTest test_ashmem_WriteAndRead_004 end.");
209 }
210 
211 /**
212  * @tc.name: test_ashmem_WriteAndRead_005
213  * @tc.desc: set read-only protection without mapping again
214  * @tc.type: FUNC
215  */
BENCHMARK_F(BenchmarkAshmemTest,test_ashmem_WriteAndRead_005)216 BENCHMARK_F(BenchmarkAshmemTest, test_ashmem_WriteAndRead_005)(benchmark::State& state)
217 {
218     BENCHMARK_LOGD("AshmemTest test_ashmem_WriteAndRead_005 start.");
219     while (state.KeepRunning()) {
220         sptr<Ashmem> ashmem = Ashmem::CreateAshmem(MEMORY_NAME.c_str(), MEMORY_SIZE);
221         AssertUnequal(ashmem, nullptr, "ashmem != nullptr did not equal true as expected.", state);
222 
223         bool ret = ashmem->MapReadAndWriteAshmem();
224         AssertTrue(ret, "ret did not equal true as expected.", state);
225 
226         ret = ashmem->WriteToAshmem(MEMORY_CONTENT.c_str(), sizeof(MEMORY_CONTENT), 0);
227         AssertTrue(ret, "ret did not equal true as expected.", state);
228 
229         ret = ashmem->SetProtection(PROT_READ);
230         AssertTrue(ret, "ret did not equal true as expected.", state);
231 
232         ret = ashmem->WriteToAshmem(MEMORY_CONTENT.c_str(), sizeof(MEMORY_CONTENT), 0);
233         AssertFalse(ret, "ret did not equal false", state);
234 
235         auto readData = ashmem->ReadFromAshmem(sizeof(MEMORY_CONTENT), 0);
236         AssertUnequal(readData, nullptr, "readData != nullptr did not equal true as expected.", state);
237 
238         const char *readContent = reinterpret_cast<const char *>(readData);
239         AssertEqual(memcmp(MEMORY_CONTENT.c_str(), readContent, sizeof(MEMORY_CONTENT)), 0,
240             "memcmp(MEMORY_CONTENT.c_str(), readContent, sizeof(MEMORY_CONTENT)) did not equal 0 as expected.", state);
241 
242         ashmem->UnmapAshmem();
243         ashmem->CloseAshmem();
244     }
245     BENCHMARK_LOGD("AshmemTest test_ashmem_WriteAndRead_005 end.");
246 }
247 
248 
249 /**
250  * @tc.name: test_ashmem_InvalidOperation_001
251  * @tc.desc: create invalid-size ashmem or set invalid protection type
252  * @tc.type: FUNC
253  */
BENCHMARK_F(BenchmarkAshmemTest,test_ashmem_InvalidOperation_001)254 BENCHMARK_F(BenchmarkAshmemTest, test_ashmem_InvalidOperation_001)(benchmark::State& state)
255 {
256     BENCHMARK_LOGD("AshmemTest test_ashmem_InvalidOperation_001 start.");
257     while (state.KeepRunning()) {
258         sptr<Ashmem> ashmem = Ashmem::CreateAshmem(MEMORY_NAME.c_str(), -1);
259         AssertEqual(ashmem, nullptr, "ashmem == nullptr did not equal true as expected.", state);
260 
261         ashmem = Ashmem::CreateAshmem(MEMORY_NAME.c_str(), MEMORY_SIZE);
262         AssertUnequal(ashmem, nullptr, "ashmem != nullptr did not equal true as expected.", state);
263 
264         bool ret = ashmem->SetProtection(-1);
265         AssertFalse(ret, "ret did not equal false", state);
266 
267         ashmem->CloseAshmem();
268     }
269     BENCHMARK_LOGD("AshmemTest test_ashmem_InvalidOperation_001 end.");
270 }
271 
272 /**
273  * @tc.name: test_ashmem_InvalidOperation_002
274  * @tc.desc: map after closing ashmem
275  * @tc.type: FUNC
276  */
BENCHMARK_F(BenchmarkAshmemTest,test_ashmem_InvalidOperation_002)277 BENCHMARK_F(BenchmarkAshmemTest, test_ashmem_InvalidOperation_002)(benchmark::State& state)
278 {
279     BENCHMARK_LOGD("AshmemTest test_ashmem_InvalidOperation_002 start.");
280     while (state.KeepRunning()) {
281         sptr<Ashmem> ashmem = Ashmem::CreateAshmem(MEMORY_NAME.c_str(), MEMORY_SIZE);
282         AssertUnequal(ashmem, nullptr, "ashmem != nullptr did not equal true as expected.", state);
283 
284         ashmem->CloseAshmem();
285 
286         bool ret = ashmem->MapReadAndWriteAshmem();
287         AssertFalse(ret, "ret did not equal false", state);
288     }
289     BENCHMARK_LOGD("AshmemTest test_ashmem_InvalidOperation_002 end.");
290 }
291 
292 /**
293  * @tc.name: test_ashmem_InvalidOperation_003
294  * @tc.desc: write or read after closing ashmem
295  * @tc.type: FUNC
296  */
BENCHMARK_F(BenchmarkAshmemTest,test_ashmem_InvalidOperation_003)297 BENCHMARK_F(BenchmarkAshmemTest, test_ashmem_InvalidOperation_003)(benchmark::State& state)
298 {
299     BENCHMARK_LOGD("AshmemTest test_ashmem_InvalidOperation_003 start.");
300     while (state.KeepRunning()) {
301         sptr<Ashmem> ashmem = Ashmem::CreateAshmem(MEMORY_NAME.c_str(), MEMORY_SIZE);
302         AssertUnequal(ashmem, nullptr, "ashmem != nullptr did not equal true as expected.", state);
303 
304         bool ret = ashmem->MapReadAndWriteAshmem();
305         AssertTrue(ret, "ret did not equal true as expected.", state);
306 
307         ashmem->CloseAshmem();
308 
309         ret = ashmem->WriteToAshmem(MEMORY_CONTENT.c_str(), sizeof(MEMORY_CONTENT), 0);
310         AssertFalse(ret, "ret did not equal false", state);
311 
312         auto readData = ashmem->ReadFromAshmem(sizeof(MEMORY_CONTENT), 0);
313         AssertEqual(readData, nullptr, "readData == nullptr did not equal true as expected.", state);
314     }
315     BENCHMARK_LOGD("AshmemTest test_ashmem_InvalidOperation_003 end.");
316 }
317 
318 /**
319  * @tc.name: test_ashmem_InvalidOperation_004
320  * @tc.desc: write or read after unmapping ashmem
321  * @tc.type: FUNC
322  */
BENCHMARK_F(BenchmarkAshmemTest,test_ashmem_InvalidOperation_004)323 BENCHMARK_F(BenchmarkAshmemTest, test_ashmem_InvalidOperation_004)(benchmark::State& state)
324 {
325     BENCHMARK_LOGD("AshmemTest test_ashmem_InvalidOperation_004 start.");
326     while (state.KeepRunning()) {
327         sptr<Ashmem> ashmem = Ashmem::CreateAshmem(MEMORY_NAME.c_str(), MEMORY_SIZE);
328         AssertUnequal(ashmem, nullptr, "ashmem != nullptr did not equal true as expected.", state);
329 
330         bool ret = ashmem->MapReadAndWriteAshmem();
331         AssertTrue(ret, "ret did not equal true as expected.", state);
332 
333         ashmem->UnmapAshmem();
334 
335         ret = ashmem->WriteToAshmem(MEMORY_CONTENT.c_str(), sizeof(MEMORY_CONTENT), 0);
336         AssertFalse(ret, "ret did not equal false", state);
337 
338         auto readData = ashmem->ReadFromAshmem(sizeof(MEMORY_CONTENT), 0);
339         AssertEqual(readData, nullptr, "readData == nullptr did not equal true as expected.", state);
340 
341         ashmem->CloseAshmem();
342     }
343     BENCHMARK_LOGD("AshmemTest test_ashmem_InvalidOperation_004 end.");
344 }
345 
346 /**
347  * @tc.name: test_ashmem_InvalidOperation_005
348  * @tc.desc: expand protection type
349  * @tc.type: FUNC
350  */
BENCHMARK_F(BenchmarkAshmemTest,test_ashmem_InvalidOperation_005)351 BENCHMARK_F(BenchmarkAshmemTest, test_ashmem_InvalidOperation_005)(benchmark::State& state)
352 {
353     BENCHMARK_LOGD("AshmemTest test_ashmem_InvalidOperation_005 start.");
354     while (state.KeepRunning()) {
355         sptr<Ashmem> ashmem = Ashmem::CreateAshmem(MEMORY_NAME.c_str(), MEMORY_SIZE);
356         AssertUnequal(ashmem, nullptr, "ashmem != nullptr did not equal true as expected.", state);
357 
358         bool ret = ashmem->SetProtection(PROT_WRITE);
359         AssertTrue(ret, "ret did not equal true as expected.", state);
360 
361         ret = ashmem->SetProtection(PROT_READ);
362         AssertFalse(ret, "ret did not equal false", state);
363 
364         ret = ashmem->SetProtection(PROT_READ | PROT_WRITE);
365         AssertFalse(ret, "ret did not equal false", state);
366 
367         ret = ashmem->SetProtection(PROT_NONE);
368         AssertTrue(ret, "ret did not equal true as expected.", state);
369 
370         ret = ashmem->SetProtection(PROT_READ);
371         AssertFalse(ret, "ret did not equal false", state);
372 
373         ashmem->CloseAshmem();
374     }
375     BENCHMARK_LOGD("AshmemTest test_ashmem_InvalidOperation_005 end.");
376 }
377 
378 /**
379  * @tc.name: test_ashmem_InvalidOperation_006
380  * @tc.desc: test invalid input or test invalid operation
381  * @tc.type: FUNC
382  */
BENCHMARK_F(BenchmarkAshmemTest,test_ashmem_InvalidOperation_006)383 BENCHMARK_F(BenchmarkAshmemTest, test_ashmem_InvalidOperation_006)(benchmark::State& state)
384 {
385     BENCHMARK_LOGD("AshmemTest test_ashmem_InvalidOperation_006 start.");
386     while (state.KeepRunning()) {
387         sptr<Ashmem> ashmem = Ashmem::CreateAshmem(MEMORY_NAME.c_str(), MEMORY_SIZE);
388         AssertUnequal(ashmem, nullptr, "ashmem != nullptr did not equal true as expected.", state);
389 
390         bool ret = ashmem->MapReadAndWriteAshmem();
391         AssertTrue(ret, "ret did not equal true as expected.", state);
392 
393         ret = ashmem->WriteToAshmem(nullptr, sizeof(MEMORY_CONTENT), 0);
394         AssertFalse(ret, "ret did not equal false", state);
395 
396         ret = ashmem->WriteToAshmem(MEMORY_CONTENT.c_str(), sizeof(MEMORY_CONTENT), MEMORY_SIZE+1);
397         AssertFalse(ret, "ret did not equal false", state);
398 
399         ret = ashmem->WriteToAshmem(MEMORY_CONTENT.c_str(), sizeof(MEMORY_CONTENT), -1);
400         AssertFalse(ret, "ret did not equal false", state);
401 
402         ret = ashmem->WriteToAshmem(MEMORY_CONTENT.c_str(), MEMORY_SIZE+1, 0);
403         AssertFalse(ret, "ret did not equal false", state);
404 
405         ret = ashmem->WriteToAshmem(MEMORY_CONTENT.c_str(), -1, 0);
406         AssertFalse(ret, "ret did not equal false", state);
407 
408         ret = ashmem->WriteToAshmem(MEMORY_CONTENT.c_str(), sizeof(MEMORY_CONTENT), MEMORY_SIZE);
409         AssertFalse(ret, "ret did not equal false", state);
410 
411         ashmem->UnmapAshmem();
412         ashmem->CloseAshmem();
413 
414         ashmem->GetAshmemSize();
415         AssertFalse(ret, "ret did not equal false", state);
416 
417         ashmem->GetProtection();
418         AssertFalse(ret, "ret did not equal false", state);
419 
420         ashmem->UnmapAshmem();
421         AssertFalse(ret, "ret did not equal false", state);
422 
423         ashmem->CloseAshmem();
424         AssertFalse(ret, "ret did not equal false", state);
425     }
426     BENCHMARK_LOGD("AshmemTest test_ashmem_InvalidOperation_006 end.");
427 }
428 
429 /**
430  * @tc.name: test_ashmem_constructor_001
431  * @tc.desc: test Ashmem constructor
432  * @tc.type: FUNC
433  */
BENCHMARK_F(BenchmarkAshmemTest,test_ashmem_constructor_001)434 BENCHMARK_F(BenchmarkAshmemTest, test_ashmem_constructor_001)(benchmark::State& state)
435 {
436     BENCHMARK_LOGD("AshmemTest test_ashmem_constructor_001 start.");
437     while (state.KeepRunning()) {
438         sptr<Ashmem> ashmem = Ashmem::CreateAshmem(MEMORY_NAME.c_str(), MEMORY_SIZE);
439         AssertUnequal(ashmem, nullptr, "Failed to create Ashmem object.", state);
440 
441         int fd = ashmem->GetAshmemFd();
442         int32_t size = ashmem->GetAshmemSize();
443         AssertFalse((fd <= 0 || size <= 0), "Invalid file descriptor or size obtained from Ashmem.", state);
444 
445         sptr<Ashmem> newAshmem = new Ashmem(fd, size);
446         AssertUnequal(newAshmem, nullptr, "Failed to create new Ashmem object with constructor.", state);
447 
448         int newFd = newAshmem->GetAshmemFd();
449         int32_t newSize = newAshmem->GetAshmemSize();
450         AssertFalse((newFd != fd || newSize != size),
451             "Mismatch in file descriptor or size in new Ashmem object.", state);
452 
453         ashmem->UnmapAshmem();
454         ashmem->CloseAshmem();
455         newAshmem->UnmapAshmem();
456         newAshmem->CloseAshmem();
457     }
458     BENCHMARK_LOGD("AshmemTest test_ashmem_constructor_001 end.");
459 }
460 
461 /**
462  * @tc.name: test_ashmem_ConstructorAndDestructor_001
463  * @tc.desc: create and delete ashmem
464  * @tc.type: FUNC
465  */
BENCHMARK_F(BenchmarkAshmemTest,test_ashmem_ConstructorAndDestructor_001)466 BENCHMARK_F(BenchmarkAshmemTest, test_ashmem_ConstructorAndDestructor_001)(benchmark::State& state)
467 {
468     BENCHMARK_LOGD("AshmemTest test_ashmem_ConstructorAndDestructor_001 start.");
469     while (state.KeepRunning()) {
470         sptr<Ashmem> ashmem = Ashmem::CreateAshmem(MEMORY_NAME.c_str(), MEMORY_SIZE);
471         AssertUnequal(ashmem, nullptr, "ashmem != nullptr did not equal true as expected.", state);
472         AssertEqual(ashmem->GetAshmemSize(), MEMORY_SIZE,
473             "ashmem->GetAshmemSize() == MEMORY_SIZE did not equal true as expected.", state);
474 
475         ashmem->UnmapAshmem();
476         ashmem->CloseAshmem();
477     }
478     BENCHMARK_LOGD("AshmemTest test_ashmem_ConstructorAndDestructor_001 end.");
479 }
480 
481 /**
482  * @tc.name: test_ashmem_GetAshmemFd_001
483  * @tc.desc: test getting file descriptor of ashmem
484  * @tc.type: FUNC
485  */
BENCHMARK_F(BenchmarkAshmemTest,test_ashmem_GetAshmemFd_001)486 BENCHMARK_F(BenchmarkAshmemTest, test_ashmem_GetAshmemFd_001)(benchmark::State& state)
487 {
488     BENCHMARK_LOGD("AshmemTest test_ashmem_GetAshmemFd_001 start.");
489     while (state.KeepRunning()) {
490         sptr<Ashmem> ashmem = Ashmem::CreateAshmem(MEMORY_NAME.c_str(), MEMORY_SIZE);
491         AssertUnequal(ashmem, nullptr, "Failed to create Ashmem object.", state);
492 
493         int fd = ashmem->GetAshmemFd();
494         AssertGreaterThan(fd, 0, "Invalid file descriptor obtained from Ashmem.", state);
495 
496         ashmem->UnmapAshmem();
497         ashmem->CloseAshmem();
498     }
499     BENCHMARK_LOGD("AshmemTest test_ashmem_GetAshmemFd_001 end.");
500 }
501 }  // namespace
502 }  // namespace OHOS
503 // Run the benchmark
504 BENCHMARK_MAIN();