This repository has been archived by the owner on May 11, 2024. It is now read-only.
forked from LMODroid/platform_packages_modules_adb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
transport_benchmark.cpp
194 lines (157 loc) · 6.76 KB
/
transport_benchmark.cpp
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
/*
* Copyright (C) 2018 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 <malloc.h>
#include <stdio.h>
#include <android-base/logging.h>
#include <benchmark/benchmark.h>
#include "adb_trace.h"
#include "sysdeps.h"
#include "transport.h"
#define ADB_CONNECTION_BENCHMARK(benchmark_name, ...) \
BENCHMARK_TEMPLATE(benchmark_name, FdConnection, ##__VA_ARGS__) \
->Arg(1) \
->Arg(16384) \
->Arg(MAX_PAYLOAD) \
->UseRealTime(); \
BENCHMARK_TEMPLATE(benchmark_name, NonblockingFdConnection, ##__VA_ARGS__) \
->Arg(1) \
->Arg(16384) \
->Arg(MAX_PAYLOAD) \
->UseRealTime()
struct NonblockingFdConnection;
template <typename ConnectionType>
std::unique_ptr<Connection> MakeConnection(unique_fd fd);
template <>
std::unique_ptr<Connection> MakeConnection<FdConnection>(unique_fd fd) {
auto fd_connection = std::make_unique<FdConnection>(std::move(fd));
return std::make_unique<BlockingConnectionAdapter>(std::move(fd_connection));
}
template <>
std::unique_ptr<Connection> MakeConnection<NonblockingFdConnection>(unique_fd fd) {
return Connection::FromFd(std::move(fd));
}
template <typename ConnectionType>
void BM_Connection_Unidirectional(benchmark::State& state) {
int fds[2];
if (adb_socketpair(fds) != 0) {
LOG(FATAL) << "failed to create socketpair";
}
auto client = MakeConnection<ConnectionType>(unique_fd(fds[0]));
auto server = MakeConnection<ConnectionType>(unique_fd(fds[1]));
std::atomic<size_t> received_bytes;
client->SetReadCallback([](Connection*, std::unique_ptr<apacket>) -> bool { return true; });
server->SetReadCallback([&received_bytes](Connection*, std::unique_ptr<apacket> packet) -> bool {
received_bytes += packet->payload.size();
return true;
});
client->SetErrorCallback(
[](Connection*, const std::string& error) { LOG(INFO) << "client closed: " << error; });
server->SetErrorCallback(
[](Connection*, const std::string& error) { LOG(INFO) << "server closed: " << error; });
client->Start();
server->Start();
for (auto _ : state) {
size_t data_size = state.range(0);
std::unique_ptr<apacket> packet = std::make_unique<apacket>();
memset(&packet->msg, 0, sizeof(packet->msg));
packet->msg.command = A_WRTE;
packet->msg.data_length = data_size;
packet->payload.resize(data_size);
memset(&packet->payload[0], 0xff, data_size);
received_bytes = 0;
client->Write(std::move(packet));
while (received_bytes < data_size) {
continue;
}
}
state.SetBytesProcessed(static_cast<int64_t>(state.iterations()) * state.range(0));
client->Stop();
server->Stop();
}
ADB_CONNECTION_BENCHMARK(BM_Connection_Unidirectional);
enum class ThreadPolicy {
MainThread,
SameThread,
};
template <typename ConnectionType, enum ThreadPolicy Policy>
void BM_Connection_Echo(benchmark::State& state) {
int fds[2];
if (adb_socketpair(fds) != 0) {
LOG(FATAL) << "failed to create socketpair";
}
auto client = MakeConnection<ConnectionType>(unique_fd(fds[0]));
auto server = MakeConnection<ConnectionType>(unique_fd(fds[1]));
std::atomic<size_t> received_bytes;
fdevent_reset();
std::thread fdevent_thread([]() { fdevent_loop(); });
client->SetReadCallback([&received_bytes](Connection*, std::unique_ptr<apacket> packet) -> bool {
received_bytes += packet->payload.size();
return true;
});
static const auto handle_packet = [](Connection* connection, std::unique_ptr<apacket> packet) {
connection->Write(std::move(packet));
};
server->SetReadCallback([](Connection* connection, std::unique_ptr<apacket> packet) -> bool {
if (Policy == ThreadPolicy::MainThread) {
auto raw_packet = packet.release();
fdevent_run_on_main_thread([connection, raw_packet]() {
std::unique_ptr<apacket> packet(raw_packet);
handle_packet(connection, std::move(packet));
});
} else {
handle_packet(connection, std::move(packet));
}
return true;
});
client->SetErrorCallback(
[](Connection*, const std::string& error) { LOG(INFO) << "client closed: " << error; });
server->SetErrorCallback(
[](Connection*, const std::string& error) { LOG(INFO) << "server closed: " << error; });
client->Start();
server->Start();
for (auto _ : state) {
size_t data_size = state.range(0);
std::unique_ptr<apacket> packet = std::make_unique<apacket>();
memset(&packet->msg, 0, sizeof(packet->msg));
packet->msg.command = A_WRTE;
packet->msg.data_length = data_size;
packet->payload.resize(data_size);
memset(&packet->payload[0], 0xff, data_size);
received_bytes = 0;
client->Write(std::move(packet));
while (received_bytes < data_size) {
continue;
}
}
state.SetBytesProcessed(static_cast<int64_t>(state.iterations()) * state.range(0));
client->Stop();
server->Stop();
// TODO: Make it so that you don't need to poke the fdevent loop to make it terminate?
fdevent_terminate_loop();
fdevent_run_on_main_thread([]() {});
fdevent_thread.join();
}
ADB_CONNECTION_BENCHMARK(BM_Connection_Echo, ThreadPolicy::SameThread);
ADB_CONNECTION_BENCHMARK(BM_Connection_Echo, ThreadPolicy::MainThread);
int main(int argc, char** argv) {
// Set M_DECAY_TIME so that our allocations aren't immediately purged on free.
mallopt(M_DECAY_TIME, 1);
android::base::SetMinimumLogSeverity(android::base::WARNING);
adb_trace_init(argv);
::benchmark::Initialize(&argc, argv);
if (::benchmark::ReportUnrecognizedArguments(argc, argv)) return 1;
::benchmark::RunSpecifiedBenchmarks();
}