Chris Craik | b251a2f | 2016-02-08 19:36:46 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
John Reck | 0418afa3 | 2016-03-07 13:24:25 -0800 | [diff] [blame] | 17 | #include <benchmark/benchmark.h> |
Chris Craik | b251a2f | 2016-02-08 19:36:46 +0000 | [diff] [blame] | 18 | |
| 19 | #include "thread/Task.h" |
| 20 | #include "thread/TaskManager.h" |
| 21 | #include "thread/TaskProcessor.h" |
John Reck | f8441e6 | 2017-10-23 13:10:41 -0700 | [diff] [blame^] | 22 | #include "thread/ThreadBase.h" |
Chris Craik | b251a2f | 2016-02-08 19:36:46 +0000 | [diff] [blame] | 23 | |
John Reck | f8441e6 | 2017-10-23 13:10:41 -0700 | [diff] [blame^] | 24 | #include <atomic> |
Chris Craik | b251a2f | 2016-02-08 19:36:46 +0000 | [diff] [blame] | 25 | #include <vector> |
| 26 | |
| 27 | using namespace android; |
| 28 | using namespace android::uirenderer; |
| 29 | |
| 30 | class TrivialTask : public Task<char> {}; |
| 31 | |
| 32 | class TrivialProcessor : public TaskProcessor<char> { |
| 33 | public: |
Chih-Hung Hsieh | d53e3be | 2016-05-03 10:02:51 -0700 | [diff] [blame] | 34 | explicit TrivialProcessor(TaskManager* manager) |
Chris Craik | b251a2f | 2016-02-08 19:36:46 +0000 | [diff] [blame] | 35 | : TaskProcessor(manager) {} |
| 36 | virtual ~TrivialProcessor() {} |
| 37 | virtual void onProcess(const sp<Task<char> >& task) override { |
| 38 | TrivialTask* t = static_cast<TrivialTask*>(task.get()); |
| 39 | t->setResult(reinterpret_cast<intptr_t>(t) % 16 == 0 ? 'a' : 'b'); |
| 40 | } |
| 41 | }; |
| 42 | |
John Reck | f8441e6 | 2017-10-23 13:10:41 -0700 | [diff] [blame^] | 43 | class TestThread : public ThreadBase, public virtual RefBase {}; |
| 44 | |
John Reck | 0418afa3 | 2016-03-07 13:24:25 -0800 | [diff] [blame] | 45 | void BM_TaskManager_allocateTask(benchmark::State& state) { |
Chris Craik | b251a2f | 2016-02-08 19:36:46 +0000 | [diff] [blame] | 46 | std::vector<sp<TrivialTask> > tasks; |
John Reck | 0418afa3 | 2016-03-07 13:24:25 -0800 | [diff] [blame] | 47 | tasks.reserve(state.max_iterations); |
Chris Craik | b251a2f | 2016-02-08 19:36:46 +0000 | [diff] [blame] | 48 | |
John Reck | 0418afa3 | 2016-03-07 13:24:25 -0800 | [diff] [blame] | 49 | while (state.KeepRunning()) { |
Chris Craik | b251a2f | 2016-02-08 19:36:46 +0000 | [diff] [blame] | 50 | tasks.emplace_back(new TrivialTask); |
John Reck | 0418afa3 | 2016-03-07 13:24:25 -0800 | [diff] [blame] | 51 | benchmark::DoNotOptimize(tasks.back()); |
Chris Craik | b251a2f | 2016-02-08 19:36:46 +0000 | [diff] [blame] | 52 | } |
Chris Craik | b251a2f | 2016-02-08 19:36:46 +0000 | [diff] [blame] | 53 | } |
John Reck | 0418afa3 | 2016-03-07 13:24:25 -0800 | [diff] [blame] | 54 | BENCHMARK(BM_TaskManager_allocateTask); |
Chris Craik | b251a2f | 2016-02-08 19:36:46 +0000 | [diff] [blame] | 55 | |
John Reck | 0418afa3 | 2016-03-07 13:24:25 -0800 | [diff] [blame] | 56 | void BM_TaskManager_enqueueTask(benchmark::State& state) { |
Chris Craik | b251a2f | 2016-02-08 19:36:46 +0000 | [diff] [blame] | 57 | TaskManager taskManager; |
| 58 | sp<TrivialProcessor> processor(new TrivialProcessor(&taskManager)); |
| 59 | std::vector<sp<TrivialTask> > tasks; |
John Reck | 0418afa3 | 2016-03-07 13:24:25 -0800 | [diff] [blame] | 60 | tasks.reserve(state.max_iterations); |
Chris Craik | b251a2f | 2016-02-08 19:36:46 +0000 | [diff] [blame] | 61 | |
John Reck | 0418afa3 | 2016-03-07 13:24:25 -0800 | [diff] [blame] | 62 | while (state.KeepRunning()) { |
Chris Craik | b251a2f | 2016-02-08 19:36:46 +0000 | [diff] [blame] | 63 | tasks.emplace_back(new TrivialTask); |
John Reck | 0418afa3 | 2016-03-07 13:24:25 -0800 | [diff] [blame] | 64 | benchmark::DoNotOptimize(tasks.back()); |
Chris Craik | b251a2f | 2016-02-08 19:36:46 +0000 | [diff] [blame] | 65 | processor->add(tasks.back()); |
| 66 | } |
Chris Craik | b251a2f | 2016-02-08 19:36:46 +0000 | [diff] [blame] | 67 | |
| 68 | for (sp<TrivialTask>& task : tasks) { |
| 69 | task->getResult(); |
| 70 | } |
| 71 | } |
John Reck | 0418afa3 | 2016-03-07 13:24:25 -0800 | [diff] [blame] | 72 | BENCHMARK(BM_TaskManager_enqueueTask); |
Chris Craik | b251a2f | 2016-02-08 19:36:46 +0000 | [diff] [blame] | 73 | |
John Reck | 0418afa3 | 2016-03-07 13:24:25 -0800 | [diff] [blame] | 74 | void BM_TaskManager_enqueueRunDeleteTask(benchmark::State& state) { |
Chris Craik | b251a2f | 2016-02-08 19:36:46 +0000 | [diff] [blame] | 75 | TaskManager taskManager; |
| 76 | sp<TrivialProcessor> processor(new TrivialProcessor(&taskManager)); |
| 77 | std::vector<sp<TrivialTask> > tasks; |
John Reck | 0418afa3 | 2016-03-07 13:24:25 -0800 | [diff] [blame] | 78 | tasks.reserve(state.max_iterations); |
Chris Craik | b251a2f | 2016-02-08 19:36:46 +0000 | [diff] [blame] | 79 | |
John Reck | 0418afa3 | 2016-03-07 13:24:25 -0800 | [diff] [blame] | 80 | while (state.KeepRunning()) { |
Chris Craik | b251a2f | 2016-02-08 19:36:46 +0000 | [diff] [blame] | 81 | tasks.emplace_back(new TrivialTask); |
John Reck | 0418afa3 | 2016-03-07 13:24:25 -0800 | [diff] [blame] | 82 | benchmark::DoNotOptimize(tasks.back()); |
Chris Craik | b251a2f | 2016-02-08 19:36:46 +0000 | [diff] [blame] | 83 | processor->add(tasks.back()); |
| 84 | } |
John Reck | 0418afa3 | 2016-03-07 13:24:25 -0800 | [diff] [blame] | 85 | state.ResumeTiming(); |
Chris Craik | b251a2f | 2016-02-08 19:36:46 +0000 | [diff] [blame] | 86 | for (sp<TrivialTask>& task : tasks) { |
John Reck | 0418afa3 | 2016-03-07 13:24:25 -0800 | [diff] [blame] | 87 | benchmark::DoNotOptimize(task->getResult()); |
Chris Craik | b251a2f | 2016-02-08 19:36:46 +0000 | [diff] [blame] | 88 | } |
| 89 | tasks.clear(); |
John Reck | 0418afa3 | 2016-03-07 13:24:25 -0800 | [diff] [blame] | 90 | state.PauseTiming(); |
Chris Craik | b251a2f | 2016-02-08 19:36:46 +0000 | [diff] [blame] | 91 | } |
John Reck | 0418afa3 | 2016-03-07 13:24:25 -0800 | [diff] [blame] | 92 | BENCHMARK(BM_TaskManager_enqueueRunDeleteTask); |
John Reck | f8441e6 | 2017-10-23 13:10:41 -0700 | [diff] [blame^] | 93 | |
| 94 | void BM_Thread_enqueueTask(benchmark::State& state) { |
| 95 | sp<TestThread> thread{new TestThread}; |
| 96 | thread->start(); |
| 97 | |
| 98 | atomic_int counter(0); |
| 99 | int expected = 0; |
| 100 | while (state.KeepRunning()) { |
| 101 | expected++; |
| 102 | thread->queue().post([&counter](){ |
| 103 | counter++; |
| 104 | }); |
| 105 | } |
| 106 | thread->queue().runSync([](){}); |
| 107 | |
| 108 | thread->requestExit(); |
| 109 | thread->join(); |
| 110 | if (counter != expected) { |
| 111 | printf("Ran %d lambads, should have been %d\n", counter.load(), expected); |
| 112 | } |
| 113 | } |
| 114 | BENCHMARK(BM_Thread_enqueueTask); |
| 115 | |
| 116 | void BM_Thread_enqueueRunDeleteTask(benchmark::State& state) { |
| 117 | sp<TestThread> thread{new TestThread}; |
| 118 | thread->start(); |
| 119 | std::vector<std::future<int>> tasks; |
| 120 | tasks.reserve(state.max_iterations); |
| 121 | |
| 122 | int expected = 0; |
| 123 | while (state.KeepRunning()) { |
| 124 | tasks.emplace_back(thread->queue().async([expected]() -> int { |
| 125 | return expected + 1; |
| 126 | })); |
| 127 | expected++; |
| 128 | } |
| 129 | state.ResumeTiming(); |
| 130 | expected = 0; |
| 131 | for (auto& future : tasks) { |
| 132 | if (future.get() != ++expected) { |
| 133 | printf("Mismatch expected %d vs. observed %d\n", expected, future.get()); |
| 134 | } |
| 135 | } |
| 136 | tasks.clear(); |
| 137 | state.PauseTiming(); |
| 138 | } |
| 139 | BENCHMARK(BM_Thread_enqueueRunDeleteTask); |