1 /* 2 * Copyright (c) 2024 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 <deque> 18 19 #include <meta/api/make_callback.h> 20 #include <meta/api/object.h> 21 #include <meta/base/namespace.h> 22 #include <meta/interface/intf_task_queue.h> 23 24 #include "property_utils.h" 25 26 META_BEGIN_NAMESPACE() 27 namespace benchmarks { 28 PushToTaskQueue(benchmark::State & state)29void PushToTaskQueue(benchmark::State& state) 30 { 31 auto q = GetObjectRegistry().Create<IPollingTaskQueue>(ClassId::PollingTaskQueue); 32 auto task = MakeCallback<ITaskQueueTask>([] { return false; }); 33 for (auto _ : state) { 34 for (int i = 0; i != 200; ++i) { 35 q->AddTask(task); 36 } 37 q->ProcessTasks(); 38 } 39 } 40 PushToTaskQueueWithLongLastingTasks(benchmark::State & state)41void PushToTaskQueueWithLongLastingTasks(benchmark::State& state) 42 { 43 auto q = GetObjectRegistry().Create<IPollingTaskQueue>(ClassId::PollingTaskQueue); 44 auto task = MakeCallback<ITaskQueueTask>([] { return false; }); 45 for (int i = 0; i != 20; ++i) { 46 q->AddTask(task, TimeSpan::Seconds(10)); 47 } 48 for (auto _ : state) { 49 for (int i = 0; i != 200; ++i) { 50 q->AddTask(task); 51 } 52 q->ProcessTasks(); 53 } 54 } 55 PushToThreadedTaskQueue(benchmark::State & state)56void PushToThreadedTaskQueue(benchmark::State& state) 57 { 58 auto q = GetObjectRegistry().Create<ITaskQueue>(ClassId::ThreadedTaskQueue); 59 std::atomic<int> value {}; 60 auto task = MakeCallback<ITaskQueueTask>([&] { 61 ++value; 62 return false; 63 }); 64 for (auto _ : state) { 65 for (int i = 0; i != 200; ++i) { 66 q->AddTask(task); 67 } 68 state.PauseTiming(); 69 while (value < 200) { 70 } 71 value = 0; 72 state.ResumeTiming(); 73 } 74 } 75 PushToThreadedTaskQueueLongLastingTasks(benchmark::State & state)76void PushToThreadedTaskQueueLongLastingTasks(benchmark::State& state) 77 { 78 auto q = GetObjectRegistry().Create<ITaskQueue>(ClassId::ThreadedTaskQueue); 79 auto dummytask = MakeCallback<ITaskQueueTask>([] { return false; }); 80 for (int i = 0; i != 20; ++i) { 81 q->AddTask(dummytask, TimeSpan::Seconds(10)); 82 } 83 std::atomic<int> value {}; 84 auto task = MakeCallback<ITaskQueueTask>([&] { 85 ++value; 86 return false; 87 }); 88 for (auto _ : state) { 89 for (int i = 0; i != 200; ++i) { 90 q->AddTask(task); 91 } 92 state.PauseTiming(); 93 while (value < 200) { 94 } 95 value = 0; 96 state.ResumeTiming(); 97 } 98 } 99 100 BENCHMARK(PushToTaskQueue); 101 BENCHMARK(PushToTaskQueueWithLongLastingTasks); 102 BENCHMARK(PushToThreadedTaskQueue); 103 BENCHMARK(PushToThreadedTaskQueueLongLastingTasks); 104 105 } // namespace benchmarks 106 META_END_NAMESPACE() 107