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