blob: c6b9f3bca55fd2d9b6f7fb8cb939b605a819189b [file] [log] [blame]
Chris Craikb251a2f2016-02-08 19:36:46 +00001/*
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 Reck0418afa32016-03-07 13:24:25 -080017#include <benchmark/benchmark.h>
Chris Craikb251a2f2016-02-08 19:36:46 +000018
19#include "thread/Task.h"
20#include "thread/TaskManager.h"
21#include "thread/TaskProcessor.h"
Chris Craikb251a2f2016-02-08 19:36:46 +000022
23#include <vector>
24
25using namespace android;
26using namespace android::uirenderer;
27
28class TrivialTask : public Task<char> {};
29
30class TrivialProcessor : public TaskProcessor<char> {
31public:
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 Reck0418afa32016-03-07 13:24:25 -080041void BM_TaskManager_allocateTask(benchmark::State& state) {
Chris Craikb251a2f2016-02-08 19:36:46 +000042 std::vector<sp<TrivialTask> > tasks;
John Reck0418afa32016-03-07 13:24:25 -080043 tasks.reserve(state.max_iterations);
Chris Craikb251a2f2016-02-08 19:36:46 +000044
John Reck0418afa32016-03-07 13:24:25 -080045 while (state.KeepRunning()) {
Chris Craikb251a2f2016-02-08 19:36:46 +000046 tasks.emplace_back(new TrivialTask);
John Reck0418afa32016-03-07 13:24:25 -080047 benchmark::DoNotOptimize(tasks.back());
Chris Craikb251a2f2016-02-08 19:36:46 +000048 }
Chris Craikb251a2f2016-02-08 19:36:46 +000049}
John Reck0418afa32016-03-07 13:24:25 -080050BENCHMARK(BM_TaskManager_allocateTask);
Chris Craikb251a2f2016-02-08 19:36:46 +000051
John Reck0418afa32016-03-07 13:24:25 -080052void BM_TaskManager_enqueueTask(benchmark::State& state) {
Chris Craikb251a2f2016-02-08 19:36:46 +000053 TaskManager taskManager;
54 sp<TrivialProcessor> processor(new TrivialProcessor(&taskManager));
55 std::vector<sp<TrivialTask> > tasks;
John Reck0418afa32016-03-07 13:24:25 -080056 tasks.reserve(state.max_iterations);
Chris Craikb251a2f2016-02-08 19:36:46 +000057
John Reck0418afa32016-03-07 13:24:25 -080058 while (state.KeepRunning()) {
Chris Craikb251a2f2016-02-08 19:36:46 +000059 tasks.emplace_back(new TrivialTask);
John Reck0418afa32016-03-07 13:24:25 -080060 benchmark::DoNotOptimize(tasks.back());
Chris Craikb251a2f2016-02-08 19:36:46 +000061 processor->add(tasks.back());
62 }
Chris Craikb251a2f2016-02-08 19:36:46 +000063
64 for (sp<TrivialTask>& task : tasks) {
65 task->getResult();
66 }
67}
John Reck0418afa32016-03-07 13:24:25 -080068BENCHMARK(BM_TaskManager_enqueueTask);
Chris Craikb251a2f2016-02-08 19:36:46 +000069
John Reck0418afa32016-03-07 13:24:25 -080070void BM_TaskManager_enqueueRunDeleteTask(benchmark::State& state) {
Chris Craikb251a2f2016-02-08 19:36:46 +000071 TaskManager taskManager;
72 sp<TrivialProcessor> processor(new TrivialProcessor(&taskManager));
73 std::vector<sp<TrivialTask> > tasks;
John Reck0418afa32016-03-07 13:24:25 -080074 tasks.reserve(state.max_iterations);
Chris Craikb251a2f2016-02-08 19:36:46 +000075
John Reck0418afa32016-03-07 13:24:25 -080076 while (state.KeepRunning()) {
Chris Craikb251a2f2016-02-08 19:36:46 +000077 tasks.emplace_back(new TrivialTask);
John Reck0418afa32016-03-07 13:24:25 -080078 benchmark::DoNotOptimize(tasks.back());
Chris Craikb251a2f2016-02-08 19:36:46 +000079 processor->add(tasks.back());
80 }
John Reck0418afa32016-03-07 13:24:25 -080081 state.ResumeTiming();
Chris Craikb251a2f2016-02-08 19:36:46 +000082 for (sp<TrivialTask>& task : tasks) {
John Reck0418afa32016-03-07 13:24:25 -080083 benchmark::DoNotOptimize(task->getResult());
Chris Craikb251a2f2016-02-08 19:36:46 +000084 }
85 tasks.clear();
John Reck0418afa32016-03-07 13:24:25 -080086 state.PauseTiming();
Chris Craikb251a2f2016-02-08 19:36:46 +000087}
John Reck0418afa32016-03-07 13:24:25 -080088BENCHMARK(BM_TaskManager_enqueueRunDeleteTask);