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" |
Chris Craik | b251a2f | 2016-02-08 19:36:46 +0000 | [diff] [blame] | 22 | |
| 23 | #include <vector> |
| 24 | |
| 25 | using namespace android; |
| 26 | using namespace android::uirenderer; |
| 27 | |
| 28 | class TrivialTask : public Task<char> {}; |
| 29 | |
| 30 | class TrivialProcessor : public TaskProcessor<char> { |
| 31 | public: |
| 32 | TrivialProcessor(TaskManager* manager) |
| 33 | : TaskProcessor(manager) {} |
| 34 | virtual ~TrivialProcessor() {} |
| 35 | virtual void onProcess(const sp<Task<char> >& task) override { |
| 36 | TrivialTask* t = static_cast<TrivialTask*>(task.get()); |
| 37 | t->setResult(reinterpret_cast<intptr_t>(t) % 16 == 0 ? 'a' : 'b'); |
| 38 | } |
| 39 | }; |
| 40 | |
John Reck | 0418afa3 | 2016-03-07 13:24:25 -0800 | [diff] [blame] | 41 | void BM_TaskManager_allocateTask(benchmark::State& state) { |
Chris Craik | b251a2f | 2016-02-08 19:36:46 +0000 | [diff] [blame] | 42 | std::vector<sp<TrivialTask> > tasks; |
John Reck | 0418afa3 | 2016-03-07 13:24:25 -0800 | [diff] [blame] | 43 | tasks.reserve(state.max_iterations); |
Chris Craik | b251a2f | 2016-02-08 19:36:46 +0000 | [diff] [blame] | 44 | |
John Reck | 0418afa3 | 2016-03-07 13:24:25 -0800 | [diff] [blame] | 45 | while (state.KeepRunning()) { |
Chris Craik | b251a2f | 2016-02-08 19:36:46 +0000 | [diff] [blame] | 46 | tasks.emplace_back(new TrivialTask); |
John Reck | 0418afa3 | 2016-03-07 13:24:25 -0800 | [diff] [blame] | 47 | benchmark::DoNotOptimize(tasks.back()); |
Chris Craik | b251a2f | 2016-02-08 19:36:46 +0000 | [diff] [blame] | 48 | } |
Chris Craik | b251a2f | 2016-02-08 19:36:46 +0000 | [diff] [blame] | 49 | } |
John Reck | 0418afa3 | 2016-03-07 13:24:25 -0800 | [diff] [blame] | 50 | BENCHMARK(BM_TaskManager_allocateTask); |
Chris Craik | b251a2f | 2016-02-08 19:36:46 +0000 | [diff] [blame] | 51 | |
John Reck | 0418afa3 | 2016-03-07 13:24:25 -0800 | [diff] [blame] | 52 | void BM_TaskManager_enqueueTask(benchmark::State& state) { |
Chris Craik | b251a2f | 2016-02-08 19:36:46 +0000 | [diff] [blame] | 53 | TaskManager taskManager; |
| 54 | sp<TrivialProcessor> processor(new TrivialProcessor(&taskManager)); |
| 55 | std::vector<sp<TrivialTask> > tasks; |
John Reck | 0418afa3 | 2016-03-07 13:24:25 -0800 | [diff] [blame] | 56 | tasks.reserve(state.max_iterations); |
Chris Craik | b251a2f | 2016-02-08 19:36:46 +0000 | [diff] [blame] | 57 | |
John Reck | 0418afa3 | 2016-03-07 13:24:25 -0800 | [diff] [blame] | 58 | while (state.KeepRunning()) { |
Chris Craik | b251a2f | 2016-02-08 19:36:46 +0000 | [diff] [blame] | 59 | tasks.emplace_back(new TrivialTask); |
John Reck | 0418afa3 | 2016-03-07 13:24:25 -0800 | [diff] [blame] | 60 | benchmark::DoNotOptimize(tasks.back()); |
Chris Craik | b251a2f | 2016-02-08 19:36:46 +0000 | [diff] [blame] | 61 | processor->add(tasks.back()); |
| 62 | } |
Chris Craik | b251a2f | 2016-02-08 19:36:46 +0000 | [diff] [blame] | 63 | |
| 64 | for (sp<TrivialTask>& task : tasks) { |
| 65 | task->getResult(); |
| 66 | } |
| 67 | } |
John Reck | 0418afa3 | 2016-03-07 13:24:25 -0800 | [diff] [blame] | 68 | BENCHMARK(BM_TaskManager_enqueueTask); |
Chris Craik | b251a2f | 2016-02-08 19:36:46 +0000 | [diff] [blame] | 69 | |
John Reck | 0418afa3 | 2016-03-07 13:24:25 -0800 | [diff] [blame] | 70 | void BM_TaskManager_enqueueRunDeleteTask(benchmark::State& state) { |
Chris Craik | b251a2f | 2016-02-08 19:36:46 +0000 | [diff] [blame] | 71 | TaskManager taskManager; |
| 72 | sp<TrivialProcessor> processor(new TrivialProcessor(&taskManager)); |
| 73 | std::vector<sp<TrivialTask> > tasks; |
John Reck | 0418afa3 | 2016-03-07 13:24:25 -0800 | [diff] [blame] | 74 | tasks.reserve(state.max_iterations); |
Chris Craik | b251a2f | 2016-02-08 19:36:46 +0000 | [diff] [blame] | 75 | |
John Reck | 0418afa3 | 2016-03-07 13:24:25 -0800 | [diff] [blame] | 76 | while (state.KeepRunning()) { |
Chris Craik | b251a2f | 2016-02-08 19:36:46 +0000 | [diff] [blame] | 77 | tasks.emplace_back(new TrivialTask); |
John Reck | 0418afa3 | 2016-03-07 13:24:25 -0800 | [diff] [blame] | 78 | benchmark::DoNotOptimize(tasks.back()); |
Chris Craik | b251a2f | 2016-02-08 19:36:46 +0000 | [diff] [blame] | 79 | processor->add(tasks.back()); |
| 80 | } |
John Reck | 0418afa3 | 2016-03-07 13:24:25 -0800 | [diff] [blame] | 81 | state.ResumeTiming(); |
Chris Craik | b251a2f | 2016-02-08 19:36:46 +0000 | [diff] [blame] | 82 | for (sp<TrivialTask>& task : tasks) { |
John Reck | 0418afa3 | 2016-03-07 13:24:25 -0800 | [diff] [blame] | 83 | benchmark::DoNotOptimize(task->getResult()); |
Chris Craik | b251a2f | 2016-02-08 19:36:46 +0000 | [diff] [blame] | 84 | } |
| 85 | tasks.clear(); |
John Reck | 0418afa3 | 2016-03-07 13:24:25 -0800 | [diff] [blame] | 86 | state.PauseTiming(); |
Chris Craik | b251a2f | 2016-02-08 19:36:46 +0000 | [diff] [blame] | 87 | } |
John Reck | 0418afa3 | 2016-03-07 13:24:25 -0800 | [diff] [blame] | 88 | BENCHMARK(BM_TaskManager_enqueueRunDeleteTask); |