From 49228cd1717d22e5b788b4b9619fc12ce57c7eb1 Mon Sep 17 00:00:00 2001 From: wujianlin Date: Tue, 19 Mar 2024 08:51:40 +0000 Subject: [PATCH] Rectify the repetition rate of c_utils. Issue:https://gitee.com/openharmony/commonlibrary_c_utils/issues/I99TG6?from=project-issue Signed-off-by: wujianlin --- .../parcel_benchmark_test.cpp | 53 ++++----- ...fe_block_queue_tracking_benchmark_test.cpp | 89 ++++++++------- .../safe_map_benchmark_test.cpp | 15 +-- .../safe_queue_benchmark_test.cpp | 15 +-- .../sorted_vector_benchmark_test.cpp | 39 +++---- .../thread_benchmark_test.cpp | 104 ++++++++---------- .../thread_pool_benchmark_test.cpp | 56 +++------- .../timer_benchmark_test.cpp | 15 +-- 8 files changed, 169 insertions(+), 217 deletions(-) diff --git a/base/test/benchmarktest/parcel_benchmark_test/parcel_benchmark_test.cpp b/base/test/benchmarktest/parcel_benchmark_test/parcel_benchmark_test.cpp index 4974c60..3e32903 100644 --- a/base/test/benchmarktest/parcel_benchmark_test/parcel_benchmark_test.cpp +++ b/base/test/benchmarktest/parcel_benchmark_test/parcel_benchmark_test.cpp @@ -1945,28 +1945,33 @@ BENCHMARK_F(BenchmarkParcelTest, test_SetMaxCapacity_002)(benchmark::State& stat BENCHMARK_LOGD("ParcelTest test_SetMaxCapacity_002 end."); } +static void ParcelWriteData(Parcel& parcel, u16string& str16Write, string& strWrite, benchmark::State& state) +{ + parcel.WriteBool(true); + bool result = parcel.WriteString(strWrite); + AssertEqual(result, true, "result did not equal true as expected.", state); + + RemoteObject obj1; + result = parcel.WriteRemoteObject(&obj1); + AssertEqual(result, true, "result did not equal true as expected.", state); + + parcel.WriteInt32(WRITE_AND_CMP_INT32_VALUE); + RemoteObject obj2; + result = parcel.WriteRemoteObject(&obj2); + AssertEqual(result, true, "result did not equal true as expected.", state); + + result = parcel.WriteString16(str16Write); + AssertEqual(result, true, "result did not equal true as expected.", state); +} + BENCHMARK_F(BenchmarkParcelTest, test_ValidateReadData_001)(benchmark::State& state) { BENCHMARK_LOGD("ParcelTest test_ValidateReadData_001 start."); while (state.KeepRunning()) { Parcel parcel(nullptr); - parcel.WriteBool(true); - string strWrite = "test"; - bool result = parcel.WriteString(strWrite); - AssertEqual(result, true, "result did not equal true as expected.", state); - - RemoteObject obj1; - result = parcel.WriteRemoteObject(&obj1); - AssertEqual(result, true, "result did not equal true as expected.", state); - - parcel.WriteInt32(WRITE_AND_CMP_INT32_VALUE); - RemoteObject obj2; - result = parcel.WriteRemoteObject(&obj2); - AssertEqual(result, true, "result did not equal true as expected.", state); - u16string str16Write = u"12345"; - result = parcel.WriteString16(str16Write); - AssertEqual(result, true, "result did not equal true as expected.", state); + string strWrite = "test"; + ParcelWriteData(parcel, str16Write, strWrite, state); bool readBool = parcel.ReadBool(); AssertEqual(readBool, true, "readBool did not equal true as expected.", state); @@ -1998,23 +2003,9 @@ BENCHMARK_F(BenchmarkParcelTest, test_ValidateReadData_002)(benchmark::State& st BENCHMARK_LOGD("ParcelTest test_ValidateReadData_002 start."); while (state.KeepRunning()) { Parcel parcel(nullptr); - parcel.WriteBool(true); string strWrite = "test"; - bool result = parcel.WriteString(strWrite); - AssertEqual(result, true, "result did not equal true as expected.", state); - - RemoteObject obj1; - result = parcel.WriteRemoteObject(&obj1); - AssertEqual(result, true, "result did not equal true as expected.", state); - - parcel.WriteInt32(WRITE_AND_CMP_INT32_VALUE); - RemoteObject obj2; - result = parcel.WriteRemoteObject(&obj2); - AssertEqual(result, true, "result did not equal true as expected.", state); - u16string str16Write = u"12345"; - result = parcel.WriteString16(str16Write); - AssertEqual(result, true, "result did not equal true as expected.", state); + ParcelWriteData(parcel, str16Write, strWrite, state); bool readBool = parcel.ReadBool(); AssertEqual(readBool, true, "readBool did not equal true as expected.", state); diff --git a/base/test/benchmarktest/safe_block_queue_tracking_benchmark_test/safe_block_queue_tracking_benchmark_test.cpp b/base/test/benchmarktest/safe_block_queue_tracking_benchmark_test/safe_block_queue_tracking_benchmark_test.cpp index a89f26d..848a669 100644 --- a/base/test/benchmarktest/safe_block_queue_tracking_benchmark_test/safe_block_queue_tracking_benchmark_test.cpp +++ b/base/test/benchmarktest/safe_block_queue_tracking_benchmark_test/safe_block_queue_tracking_benchmark_test.cpp @@ -29,22 +29,23 @@ namespace { class BenchmarkSafeBlockQueueTracking : public benchmark::Fixture { public: - BenchmarkSafeBlockQueueTracking() + void SetUp(const ::benchmark::State& state) override { - Iterations(iterations); - Repetitions(repetitions); - ReportAggregatesOnly(); } - ~BenchmarkSafeBlockQueueTracking() override = default; - void SetUp(const ::benchmark::State& state) override + void TearDown(const ::benchmark::State& state) override { } - void TearDown(const ::benchmark::State& state) override + BenchmarkSafeBlockQueueTracking() { + Iterations(iterations); + Repetitions(repetitions); + ReportAggregatesOnly(); } + ~BenchmarkSafeBlockQueueTracking() override = default; + protected: const int32_t repetitions = 3; const int32_t iterations = 50; @@ -172,6 +173,14 @@ void processSharedQueueTasks(DemoThreadData& data) } } +static auto GetTimeOfSleepHundredMillisecond() +{ + using std::chrono::system_clock; + auto timeT = std::chrono::high_resolution_clock::now(); + timeT += std::chrono::milliseconds(SLEEP_FOR_HUNDRED_MILLISECOND); + return timeT; +} + /* * @tc.name: testPut001 * @tc.desc: Single-threaded call put and get to determine that the normal scenario is working properly @@ -279,9 +288,7 @@ BENCHMARK_F(BenchmarkSafeBlockQueueTracking, PutAndBlockInblankqueue001)(benchma std::array demoDatas; while (state.KeepRunning()) { demoDatas.fill(DemoThreadData()); - using std::chrono::system_clock; - auto timeT = std::chrono::high_resolution_clock::now(); - timeT += std::chrono::milliseconds(SLEEP_FOR_HUNDRED_MILLISECOND); + auto timeT = GetTimeOfSleepHundredMillisecond(); AssertTrue((DemoThreadData::shareQueue.IsEmpty()), "DemoThreadData::shareQueue.IsEmpty() did not equal true as expected.", state); StartThreads(threads, PutHandleThreadDataTime, demoDatas, timeT); @@ -333,9 +340,7 @@ BENCHMARK_F(BenchmarkSafeBlockQueueTracking, testPutAndBlockInFullQueue001)(benc std::array demoDatas; while (state.KeepRunning()) { demoDatas.fill(DemoThreadData()); - using std::chrono::system_clock; - auto timeT = std::chrono::high_resolution_clock::now(); - timeT += std::chrono::milliseconds(SLEEP_FOR_HUNDRED_MILLISECOND); + auto timeT = GetTimeOfSleepHundredMillisecond(); AssertTrue((DemoThreadData::shareQueue.IsEmpty()), "DemoThreadData::shareQueue.IsEmpty() did not equal true as expected.", state); for (unsigned int i = 0; i < QUEUE_SLOTS; i++) { @@ -386,9 +391,7 @@ BENCHMARK_F(BenchmarkSafeBlockQueueTracking, GetAndBlockInblankqueue001)(benchma std::array demoDatas; while (state.KeepRunning()) { demoDatas.fill(DemoThreadData()); - using std::chrono::system_clock; - auto timeT = std::chrono::high_resolution_clock::now(); - timeT += std::chrono::milliseconds(SLEEP_FOR_HUNDRED_MILLISECOND); + auto timeT = GetTimeOfSleepHundredMillisecond(); AssertTrue((DemoThreadData::shareQueue.IsEmpty()), "DemoThreadData::shareQueue.IsEmpty() did not equal true as expected.", state); StartThreads(threads, GetAndOneTaskDoneHandleThreadDataTime, demoDatas, timeT); @@ -424,6 +427,22 @@ BENCHMARK_F(BenchmarkSafeBlockQueueTracking, GetAndBlockInblankqueue001)(benchma BENCHMARK_LOGD("SafeBlockQueueTracking GetAndBlockInblankqueue001 end."); } +static void QueuePushFullEquivalent(const int Equivalent, benchmark::State& state) +{ + for (unsigned int i = 0; i < QUEUE_SLOTS; i++) { + DemoThreadData::shareQueue.Push(Equivalent); + } + AssertTrue((DemoThreadData::shareQueue.IsFull()), "shareQueue.IsFull() did not equal true.", state); +} + +static void QueuePushFullNotEquivalent(const unsigned int remain) +{ + for (unsigned int i = 0; i < QUEUE_SLOTS - remain; i++) { + int t = i; + DemoThreadData::shareQueue.Push(t); + } +} + /* * @tc.name: GetAndBlockInfullqueue001 * @tc.desc: Multi-threaded get() on the full queue. When n threads are waiting to reach a certain @@ -436,15 +455,10 @@ BENCHMARK_F(BenchmarkSafeBlockQueueTracking, GetAndBlockInfullqueue001)(benchmar std::array demoDatas; while (state.KeepRunning()) { demoDatas.fill(DemoThreadData()); - using std::chrono::system_clock; - auto timeT = std::chrono::high_resolution_clock::now(); - timeT += std::chrono::milliseconds(SLEEP_FOR_HUNDRED_MILLISECOND); + auto timeT = GetTimeOfSleepHundredMillisecond(); AssertTrue((DemoThreadData::shareQueue.IsEmpty()), "shareQueue.IsEmpty() did not equal true.", state); int t = 1; - for (unsigned int i = 0; i < QUEUE_SLOTS; i++) { - DemoThreadData::shareQueue.Push(t); - } - AssertTrue((DemoThreadData::shareQueue.IsFull()), "shareQueue.IsFull() did not equal true.", state); + QueuePushFullEquivalent(t, state); demoDatas[0].joinStatus = false; DemoThreadData tmp = DemoThreadData(); std::thread joinThread = std::thread(&DemoThreadData::Join, tmp); @@ -492,16 +506,11 @@ BENCHMARK_F(BenchmarkSafeBlockQueueTracking, GetAndBlockInnotfullqueue001)(bench std::array demoDatas; while (state.KeepRunning()) { demoDatas.fill(DemoThreadData()); - using std::chrono::system_clock; - const unsigned int REMAIN_SLOTS = 3; - auto timeT = std::chrono::high_resolution_clock::now(); - timeT += std::chrono::milliseconds(SLEEP_FOR_HUNDRED_MILLISECOND); + auto timeT = GetTimeOfSleepHundredMillisecond(); AssertTrue((DemoThreadData::shareQueue.IsEmpty()), "DemoThreadData::shareQueue.IsEmpty() did not equal true as expected.", state); - for (unsigned int i = 0; i < QUEUE_SLOTS - REMAIN_SLOTS; i++) { - int t = i; - DemoThreadData::shareQueue.Push(t); - } + const unsigned int REMAIN_SLOTS = 3; + QueuePushFullNotEquivalent(REMAIN_SLOTS); demoDatas[0].joinStatus = false; DemoThreadData tmp = DemoThreadData(); std::thread joinThread = std::thread(&DemoThreadData::Join, tmp); @@ -548,16 +557,11 @@ BENCHMARK_F(BenchmarkSafeBlockQueueTracking, PutAndBlockInnotfullqueue001)(bench std::array demoDatas; while (state.KeepRunning()) { demoDatas.fill(DemoThreadData()); - using std::chrono::system_clock; + auto timeT = GetTimeOfSleepHundredMillisecond(); const unsigned int REMAIN_SLOTS = 3; - auto timeT = std::chrono::high_resolution_clock::now(); - timeT += std::chrono::milliseconds(SLEEP_FOR_HUNDRED_MILLISECOND); AssertTrue((DemoThreadData::shareQueue.IsEmpty()), "DemoThreadData::shareQueue.IsEmpty() did not equal true as expected.", state); - for (unsigned int i = 0; i < QUEUE_SLOTS - REMAIN_SLOTS; i++) { - int t = i; - DemoThreadData::shareQueue.Push(t); - } + QueuePushFullNotEquivalent(REMAIN_SLOTS); demoDatas[0].joinStatus = false; DemoThreadData tmp = DemoThreadData(); std::thread joinThread = std::thread(&DemoThreadData::Join, tmp); @@ -605,15 +609,10 @@ BENCHMARK_F(BenchmarkSafeBlockQueueTracking, testMutilthreadConcurrentGetAndPopI while (state.KeepRunning()) { demoDatas.fill(DemoThreadData()); std::thread threadsin[THREAD_NUM]; - using std::chrono::system_clock; - auto timeT = std::chrono::high_resolution_clock::now(); - timeT += std::chrono::milliseconds(SLEEP_FOR_HUNDRED_MILLISECOND); + auto timeT = GetTimeOfSleepHundredMillisecond(); AssertTrue((DemoThreadData::shareQueue.IsEmpty()), "shareQueue.IsEmpty() did not equal true.", state); int t = 1; - for (unsigned int i = 0; i < QUEUE_SLOTS; i++) { - DemoThreadData::shareQueue.Push(t); - } - AssertTrue((DemoThreadData::shareQueue.IsFull()), "shareQueue.IsFull() did not equal true.", state); + QueuePushFullEquivalent(t, state); demoDatas[0].joinStatus = false; DemoThreadData tmp = DemoThreadData(); std::thread joinThread = std::thread(&DemoThreadData::Join, tmp); diff --git a/base/test/benchmarktest/safe_map_benchmark_test/safe_map_benchmark_test.cpp b/base/test/benchmarktest/safe_map_benchmark_test/safe_map_benchmark_test.cpp index e035c1f..bb9214b 100644 --- a/base/test/benchmarktest/safe_map_benchmark_test/safe_map_benchmark_test.cpp +++ b/base/test/benchmarktest/safe_map_benchmark_test/safe_map_benchmark_test.cpp @@ -30,22 +30,23 @@ namespace { class BenchmarkSafeMap : public benchmark::Fixture { public: - BenchmarkSafeMap() + void SetUp(const ::benchmark::State& state) override { - Iterations(iterations); - Repetitions(repetitions); - ReportAggregatesOnly(); } - ~BenchmarkSafeMap() override = default; - void SetUp(const ::benchmark::State& state) override + void TearDown(const ::benchmark::State& state) override { } - void TearDown(const ::benchmark::State& state) override + BenchmarkSafeMap() { + Iterations(iterations); + Repetitions(repetitions); + ReportAggregatesOnly(); } + ~BenchmarkSafeMap() override = default; + protected: const int32_t repetitions = 3; const int32_t iterations = 50; diff --git a/base/test/benchmarktest/safe_queue_benchmark_test/safe_queue_benchmark_test.cpp b/base/test/benchmarktest/safe_queue_benchmark_test/safe_queue_benchmark_test.cpp index a5d39a9..3b73bae 100644 --- a/base/test/benchmarktest/safe_queue_benchmark_test/safe_queue_benchmark_test.cpp +++ b/base/test/benchmarktest/safe_queue_benchmark_test/safe_queue_benchmark_test.cpp @@ -36,22 +36,23 @@ const int SLEEP_FOR_FOUR_SECONDS = 4; class BenchmarkSafeQueue : public benchmark::Fixture { public: - BenchmarkSafeQueue() + void SetUp(const ::benchmark::State& state) override { - Iterations(iterations); - Repetitions(repetitions); - ReportAggregatesOnly(); } - ~BenchmarkSafeQueue() override = default; - void SetUp(const ::benchmark::State& state) override + void TearDown(const ::benchmark::State& state) override { } - void TearDown(const ::benchmark::State& state) override + BenchmarkSafeQueue() { + Iterations(iterations); + Repetitions(repetitions); + ReportAggregatesOnly(); } + ~BenchmarkSafeQueue() override = default; + protected: const int32_t repetitions = 3; const int32_t iterations = 50; diff --git a/base/test/benchmarktest/sorted_vector_benchmark_test/sorted_vector_benchmark_test.cpp b/base/test/benchmarktest/sorted_vector_benchmark_test/sorted_vector_benchmark_test.cpp index 9acb6d1..09ac17b 100644 --- a/base/test/benchmarktest/sorted_vector_benchmark_test/sorted_vector_benchmark_test.cpp +++ b/base/test/benchmarktest/sorted_vector_benchmark_test/sorted_vector_benchmark_test.cpp @@ -103,23 +103,28 @@ BENCHMARK_F(BenchmarkSortedVector, testConsFromSortedAllowDup)(benchmark::State& BENCHMARK_LOGD("SortedVector testConsFromSortedAllowDup end."); } +static void AddElement(SortedVector& svec, std::vector& vec) +{ + for (int i = 0; i < VECTOR_SIZE_TWENTY; i++) { + vec.push_back(i); + } + + for (int i = SVECTOR_END_POS_ASSIGN; i >= SVECTOR_START_POS_ASSIGN; i--) { + svec.Add(i); + } + + for (int i = 0; i < VECTOR_SIZE_TWENTY; i++) { + svec.Add(i); + } +} + BENCHMARK_F(BenchmarkSortedVector, testConsFromSortedNotAllowDuplicate)(benchmark::State& state) { BENCHMARK_LOGD("SortedVector testConsFromSortedNotAllowDuplicate start."); while (state.KeepRunning()) { SortedVector svec; std::vector vec; - for (int i = 0; i < VECTOR_SIZE_TWENTY; i++) { - vec.push_back(i); - } - - for (int i = SVECTOR_END_POS_ASSIGN; i >= SVECTOR_START_POS_ASSIGN; i--) { - svec.Add(i); - } - - for (int i = 0; i < VECTOR_SIZE_TWENTY; i++) { - svec.Add(i); - } + AddElement(svec, vec); AssertEqual(static_cast(EXPECTED_SIZE_AFTER_DUPLICATES), svec.Size(), "static_cast(EXPECTED_SIZE_AFTER_DUPLICATES) did not equal svec.Size() as expected.", state); @@ -202,17 +207,7 @@ BENCHMARK_F(BenchmarkSortedVector, testOperatorEqAllowToNotAllow)(benchmark::Sta while (state.KeepRunning()) { SortedVector svec; std::vector vec; - for (int i = 0; i < VECTOR_SIZE_TWENTY; i++) { - vec.push_back(i); - } - - for (int i = SVECTOR_END_POS_ASSIGN; i >= SVECTOR_START_POS_ASSIGN; i--) { - svec.Add(i); - } - - for (int i = 0; i < VECTOR_SIZE_TWENTY; i++) { - svec.Add(i); - } + AddElement(svec, vec); AssertEqual(static_cast(EXPECTED_SIZE_AFTER_DUPLICATES), svec.Size(), "static_cast(EXPECTED_SIZE_AFTER_DUPLICATES) did not equal svec.Size() as expected.", state); diff --git a/base/test/benchmarktest/thread_benchmark_test/thread_benchmark_test.cpp b/base/test/benchmarktest/thread_benchmark_test/thread_benchmark_test.cpp index 68a9a3f..2c4337d 100644 --- a/base/test/benchmarktest/thread_benchmark_test/thread_benchmark_test.cpp +++ b/base/test/benchmarktest/thread_benchmark_test/thread_benchmark_test.cpp @@ -215,6 +215,30 @@ BENCHMARK_F(BenchmarkThreadTest, testThread002)(benchmark::State& state) BENCHMARK_LOGD("ThreadTest testThread002 end."); } +static void ThreadTestProcedure(std::unique_ptr& test, const int expectedDataValue, benchmark::State& state) +{ + pthread_t thread = test->GetThread(); + + // pthread_equal return non-zero if equal + AssertEqual((pthread_equal(thread, INVALID_THREAD_ID) != 0), (test->IsRunning() ? false : true), + "pthread_equal(thread, INVALID_THREAD_ID) != 0 did not equal \ + (test->IsRunning() ? false : true) as expected.", state); + + // ReadyToWork return false, RUN will not be called! + AssertEqual(test->priority_, DEFAULT_PRIO, + "test->priority_ did not equal DEFAULT_PRIO as expected.", state); + AssertEqual(test->name_, DEFAULT_THREAD_NAME, + "test->name_ did not equal DEFAULT_THREAD_NAME as expected.", state); + AssertEqual(test->data_, expectedDataValue, "test->data_ did not equal expectedDataValue as expected.", state); + AssertEqual(g_times, INITIAL_TIMES, "g_times did not equal INITIAL_TIMES as expected.", state); + + test->NotifyExitSync(); + sleep(SLEEP_FOR_ONE_SECOND); + AssertEqual((pthread_equal(test->GetThread(), INVALID_THREAD_ID) != 0), (test->IsRunning() ? false : true), + "pthread_equal(test->GetThread(), INVALID_THREAD_ID) != 0 did not equal \ + (test->IsRunning() ? false : true) as expected.", state); +} + /* * @tc.name: testThread003 * @tc.desc: ThreadTest @@ -230,26 +254,7 @@ BENCHMARK_F(BenchmarkThreadTest, testThread003)(benchmark::State& state) AssertEqual((status == ThreadStatus::OK), true, "status == ThreadStatus::OK did not equal true as expected.", state); - pthread_t thread = test->GetThread(); - - // pthread_equal return non-zero if equal - AssertEqual((pthread_equal(thread, INVALID_THREAD_ID) != 0), (test->IsRunning() ? false : true), - "pthread_equal(thread, INVALID_THREAD_ID) != 0 did not equal \ - (test->IsRunning() ? false : true) as expected.", state); - - // ReadyToWork return false, RUN will not be called! - AssertEqual(test->priority_, DEFAULT_PRIO, - "test->priority_ did not equal DEFAULT_PRIO as expected.", state); - AssertEqual(test->name_, DEFAULT_THREAD_NAME, - "test->name_ did not equal DEFAULT_THREAD_NAME as expected.", state); - AssertEqual(test->data_, expectedDataValue, "test->data_ did not equal expectedDataValue as expected.", state); - AssertEqual(g_times, INITIAL_TIMES, "g_times did not equal INITIAL_TIMES as expected.", state); - - test->NotifyExitSync(); - sleep(SLEEP_FOR_ONE_SECOND); - AssertEqual((pthread_equal(test->GetThread(), INVALID_THREAD_ID) != 0), (test->IsRunning() ? false : true), - "pthread_equal(test->GetThread(), INVALID_THREAD_ID) != 0 did not equal \ - (test->IsRunning() ? false : true) as expected.", state); + ThreadTestProcedure(test, expectedDataValue, state); } BENCHMARK_LOGD("ThreadTest testThread003 end."); } @@ -309,24 +314,7 @@ BENCHMARK_F(BenchmarkThreadTest, testThread005)(benchmark::State& state) AssertEqual((status == ThreadStatus::OK), true, "status == ThreadStatus::OK did not equal true as expected.", state); - pthread_t thread = test->GetThread(); - // pthread_equal return non-zero if equal - AssertEqual((pthread_equal(thread, INVALID_THREAD_ID) != 0), (test->IsRunning() ? false : true), - "pthread_equal(thread, INVALID_THREAD_ID) != 0 did not equal \ - (test->IsRunning() ? false : true) as expected.", state); - // ReadyToWork return false, RUN will not be called! - AssertEqual(test->priority_, DEFAULT_PRIO, - "test->priority_ did not equal DEFAULT_PRIO as expected.", state); - AssertEqual(test->name_, DEFAULT_THREAD_NAME, - "test->name_ did not equal DEFAULT_THREAD_NAME as expected.", state); - AssertEqual(test->data_, expectedDataValue, "test->data_ did not equal expectedDataValue as expected.", state); - AssertEqual(g_times, INITIAL_TIMES, "g_times did not equal INITIAL_TIMES as expected.", state); - - test->NotifyExitSync(); - sleep(SLEEP_FOR_ONE_SECOND); - AssertEqual((pthread_equal(test->GetThread(), INVALID_THREAD_ID) != 0), (test->IsRunning() ? false : true), - "pthread_equal(test->GetThread(), INVALID_THREAD_ID) != 0 did not equal \ - (test->IsRunning() ? false : true) as expected.", state); + ThreadTestProcedure(test, expectedDataValue, state); } BENCHMARK_LOGD("ThreadTest testThread005 end."); } @@ -409,6 +397,22 @@ BENCHMARK_F(BenchmarkThreadTest, testThread007)(benchmark::State& state) BENCHMARK_LOGD("ThreadTest testThread007 end."); } +template +void ThreadTestStart(std::unique_ptr& test, benchmark::State& state) +{ + ThreadStatus status = test->Start("", THREAD_PROI_NORMAL, THREAD_STACK_SIZE); + AssertEqual((status == ThreadStatus::OK), true, + "status == ThreadStatus::OK did not equal true as expected.", state); + + sleep(SLEEP_FOR_ONE_SECOND); + test->NotifyExitAsync(); + + sleep(SLEEP_FOR_ONE_SECOND); // let the new thread has chance to run + AssertEqual((pthread_equal(test->GetThread(), INVALID_THREAD_ID) != 0), (test->IsRunning() ? false : true), + "pthread_equal(test->GetThread(), INVALID_THREAD_ID) != 0 did not equal \ + (test->IsRunning() ? false : true) as expected.", state); +} + /* * @tc.name: testThread008 * @tc.desc: ThreadTest @@ -423,17 +427,7 @@ BENCHMARK_F(BenchmarkThreadTest, testThread008)(benchmark::State& state) bool res = test->Thread::ReadyToWork(); AssertEqual(res, true, "res did not equal true as expected.", state); - ThreadStatus status = test->Start("", THREAD_PROI_NORMAL, THREAD_STACK_SIZE); - AssertEqual((status == ThreadStatus::OK), true, - "status == ThreadStatus::OK did not equal true as expected.", state); - - sleep(SLEEP_FOR_ONE_SECOND); - test->NotifyExitAsync(); - - sleep(SLEEP_FOR_ONE_SECOND); // let the new thread has chance to run - AssertEqual((pthread_equal(test->GetThread(), INVALID_THREAD_ID) != 0), (test->IsRunning() ? false : true), - "pthread_equal(test->GetThread(), INVALID_THREAD_ID) != 0 did not equal \ - (test->IsRunning() ? false : true) as expected.", state); + ThreadTestStart(test, state); } BENCHMARK_LOGD("ThreadTest testThread008 end."); } @@ -486,17 +480,7 @@ BENCHMARK_F(BenchmarkThreadTest, testThread009)(benchmark::State& state) bool res = test->ReadyToWork(); AssertEqual(res, true, "res did not equal true as expected.", state); - ThreadStatus status = test->Start("", THREAD_PROI_NORMAL, THREAD_STACK_SIZE); - AssertEqual((status == ThreadStatus::OK), true, - "status == ThreadStatus::OK did not equal true as expected.", state); - - sleep(SLEEP_FOR_ONE_SECOND); - test->NotifyExitAsync(); - - sleep(SLEEP_FOR_ONE_SECOND); // let the new thread has chance to run - AssertEqual((pthread_equal(test->GetThread(), INVALID_THREAD_ID) != 0), (test->IsRunning() ? false : true), - "pthread_equal(test->GetThread(), INVALID_THREAD_ID) != 0 did not equal \ - (test->IsRunning() ? false : true) as expected.", state); + ThreadTestStart(test, state); } BENCHMARK_LOGD("ThreadTest testThread009 end."); } diff --git a/base/test/benchmarktest/thread_pool_benchmark_test/thread_pool_benchmark_test.cpp b/base/test/benchmarktest/thread_pool_benchmark_test/thread_pool_benchmark_test.cpp index 49ae526..a12a70f 100644 --- a/base/test/benchmarktest/thread_pool_benchmark_test/thread_pool_benchmark_test.cpp +++ b/base/test/benchmarktest/thread_pool_benchmark_test/thread_pool_benchmark_test.cpp @@ -154,6 +154,14 @@ void TestFuncSubOneTime(int& i) BENCHMARK_LOGD("ThreadPoolTest TestFuncSubOneTime g_times:%{public}d", g_times); } +static void AddPoolTaskByForLoop(ThreadPool& pool, const int loopIterations, void(*p)(int&)) +{ + for (int i = 0; i < loopIterations; ++i) { + auto task = std::bind(p, i); + pool.AddTask(task); + } +} + // simple task, total task num less than the MaxTaskNum BENCHMARK_F(BenchmarkThreadPoolTest, test_05)(benchmark::State& state) { @@ -174,15 +182,8 @@ BENCHMARK_F(BenchmarkThreadPoolTest, test_05)(benchmark::State& state) AssertEqual((int)pool.GetCurTaskNum(), curTaskNum, "(int)pool.GetCurTaskNum() did not equal curTaskNum as expected.", state); - for (int i = 0; i < loopIterations1; ++i) { - auto task = std::bind(TestFuncAddOneTime, i); - pool.AddTask(task); - } - - for (int i = 0; i < loopIterations2; ++i) { - auto task = std::bind(TestFuncSubOneTime, i); - pool.AddTask(task); - } + AddPoolTaskByForLoop(pool, loopIterations1, TestFuncAddOneTime); + AddPoolTaskByForLoop(pool, loopIterations2, TestFuncSubOneTime); sleep(SLEEP_FOR_ONE_SECOND); AssertEqual((int)pool.GetCurTaskNum(), curTaskNum, @@ -212,22 +213,15 @@ BENCHMARK_F(BenchmarkThreadPoolTest, test_06)(benchmark::State& state) ThreadPool pool; pool.Start(startThreads); AssertEqual(pool.GetName(), "", "pool.GetName() did not equal \"\" as expected.", state); - AssertEqual((int)pool.GetThreadsNum(), threadsNum, - "(int)pool.GetThreadsNum() did not equal threadsNum as expected.", state); AssertEqual((int)pool.GetCurTaskNum(), curTaskNum, "(int)pool.GetCurTaskNum() did not equal curTaskNum as expected.", state); + AssertEqual((int)pool.GetThreadsNum(), threadsNum, + "(int)pool.GetThreadsNum() did not equal threadsNum as expected.", state); pool.SetMaxTaskNum(maxTaskNum); - for (int i = 0; i < loopIterations1; ++i) { - auto task = std::bind(TestFuncAddOneTime, i); - pool.AddTask(task); - } - - for (int i = 0; i < loopIterations2; ++i) { - auto task = std::bind(TestFuncSubOneTime, i); - pool.AddTask(task); - } + AddPoolTaskByForLoop(pool, loopIterations1, TestFuncAddOneTime); + AddPoolTaskByForLoop(pool, loopIterations2, TestFuncSubOneTime); sleep(SLEEP_FOR_ONE_SECOND); // 1 second should be enough to complete these tasks. if not, this case would be fail @@ -280,15 +274,8 @@ BENCHMARK_F(BenchmarkThreadPoolTest, test_07)(benchmark::State& state) AssertEqual((int)pool.GetCurTaskNum(), curTaskNum, "(int)pool.GetCurTaskNum() did not equal curTaskNum as expected.", state); - for (int i = 0; i < loopIterations1; ++i) { - auto task = std::bind(TestFuncAddWait, i); - pool.AddTask(task); - } - - for (int i = 0; i < loopIterations2; ++i) { - auto task = std::bind(TestFuncSubWait, i); - pool.AddTask(task); - } + AddPoolTaskByForLoop(pool, loopIterations1, TestFuncAddWait); + AddPoolTaskByForLoop(pool, loopIterations2, TestFuncSubWait); // release cpu proactively, let the task threads go into wait std::this_thread::sleep_for(std::chrono::seconds(SLEEP_FOR_ONE_SECOND)); @@ -336,15 +323,8 @@ BENCHMARK_F(BenchmarkThreadPoolTest, test_08)(benchmark::State& state) pool.SetMaxTaskNum(maxTaskNum); // ADD 15 tasks - for (int i = 0; i < loopIterations1; ++i) { - auto task = std::bind(TestFuncAddWait, i); - pool.AddTask(task); - } - - for (int i = 0; i < loopIterations2; ++i) { - auto task = std::bind(TestFuncSubWait, i); - pool.AddTask(task); - } + AddPoolTaskByForLoop(pool, loopIterations1, TestFuncAddWait); + AddPoolTaskByForLoop(pool, loopIterations2, TestFuncSubWait); sleep(SLEEP_FOR_ONE_SECOND); // at this time, the first 5 tasks execute and wait for notify, the rest 10 tasks stay in the task queue. diff --git a/base/test/benchmarktest/timer_benchmark_test/timer_benchmark_test.cpp b/base/test/benchmarktest/timer_benchmark_test/timer_benchmark_test.cpp index d69b649..a3ab986 100644 --- a/base/test/benchmarktest/timer_benchmark_test/timer_benchmark_test.cpp +++ b/base/test/benchmarktest/timer_benchmark_test/timer_benchmark_test.cpp @@ -40,22 +40,23 @@ int64_t CurMs() class BenchmarkTimerTest : public benchmark::Fixture { public: - BenchmarkTimerTest() + void SetUp(const ::benchmark::State& state) override { - Iterations(iterations); - Repetitions(repetitions); - ReportAggregatesOnly(); } - ~BenchmarkTimerTest() override = default; - void SetUp(const ::benchmark::State& state) override + void TearDown(const ::benchmark::State& state) override { } - void TearDown(const ::benchmark::State& state) override + BenchmarkTimerTest() { + Iterations(iterations); + Repetitions(repetitions); + ReportAggregatesOnly(); } + ~BenchmarkTimerTest() override = default; + protected: const int32_t repetitions = 3; const int32_t iterations = 50; -- Gitee