blob: cb585dec0ade938720ceaedc950100266b9dd38f [file] [log] [blame]
Zhuoyao Zhangd09965d2016-08-24 15:18:22 -07001/*
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
17#include <benchmark/benchmark.h>
18
19#include <binder/IServiceManager.h>
20#include <binder/ProcessState.h>
21#include <utils/String16.h>
22#include <utils/StrongPointer.h>
23
24#include <sys/types.h>
25#include <sys/wait.h>
26#include <unistd.h>
27
28#include <android/tests/binder/IBenchmark.h>
29#include <android/tests/binder/BnBenchmark.h>
30
31// libutils:
32using android::OK;
33using android::sp;
34using android::status_t;
35using android::String16;
36
37// libbinder:
38using android::getService;
Zhuoyao Zhangd09965d2016-08-24 15:18:22 -070039using android::defaultServiceManager;
40using android::ProcessState;
41using android::binder::Status;
42
43// Standard library
44using std::vector;
45
46// Generated AIDL files
47using android::tests::binder::BnBenchmark;
48using android::tests::binder::IBenchmark;
49
50const char kServiceName[] = "android.tests.binder.IBenchmark";
51
52class BenchmarkServiceAidl : public BnBenchmark {
53 public:
54 BenchmarkServiceAidl() {}
55 virtual ~BenchmarkServiceAidl() = default;
56
57 Status sendVec(const vector<uint8_t>& data, vector<uint8_t>* _aidl_return) {
58 *_aidl_return = data;
59 return Status::ok();
60 }
61};
62
63bool startServer() {
64 BenchmarkServiceAidl *service = new BenchmarkServiceAidl();
65 defaultServiceManager()->addService(String16(kServiceName),
66 service);
67 ProcessState::self()->startThreadPool();
68 return 0;
69}
70
71static void BM_sendVec_binder(benchmark::State& state) {
72 sp<IBenchmark> service;
73 // Prepare data to IPC
74 vector<uint8_t> data_vec;
75 vector<uint8_t> data_return;
Martijn Coenena0a7e1e2016-11-14 14:51:33 +010076 data_vec.resize(state.range(0));
77 for (int i = 0; i < state.range(0); i++) {
Zhuoyao Zhangd09965d2016-08-24 15:18:22 -070078 data_vec[i] = i % 256;
79 }
80 // getService automatically retries
81 status_t status = getService(String16(kServiceName), &service);
82 if (status != OK) {
83 state.SkipWithError("Failed to retrieve benchmark service.");
84 }
85 // Start running
86 while (state.KeepRunning()) {
87 service->sendVec(data_vec, &data_return);
88 }
89}
90
91BENCHMARK(BM_sendVec_binder)->RangeMultiplier(2)->Range(4, 65536);
92
93int main(int argc, char* argv []) {
94 ::benchmark::Initialize(&argc, argv);
95
96 pid_t pid = fork();
97 if (pid == 0) {
98 // Child, start benchmarks
99 ::benchmark::RunSpecifiedBenchmarks();
100 } else {
101 int stat;
102 startServer();
103 while (true) {
104 int stat, retval;
105 retval = wait(&stat);
106 if (retval == -1 && errno == ECHILD) {
107 break;
108 }
109 }
110 };
111}