1/*
2 * Copyright (C) 2010 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 <binder/IInterface.h>
18#include <binder/IServiceManager.h>
19#include <binder/IPCThreadState.h>
20#include <utils/Log.h>
21
22#include <stdio.h>
23#include <time.h>
24#include <unistd.h>
25
26using namespace android;
27
28static const int WARMUP = 100;
29static const int COUNT = 10000;
30
31class ITestService : public IInterface {
32public:
33    DECLARE_META_INTERFACE(TestService);
34};
35
36typedef BpInterface<ITestService> BpTestService;
37
38IMPLEMENT_META_INTERFACE(TestService, "TestService");
39
40int main(int argc, const char *argv[]) {
41    if (argc != 2 || argv[1][0] == '-') {
42        fprintf(stderr, "usage: rpcperftest service-to-test | :service-to-serve\n");
43        return 2;
44    }
45
46    sp<IServiceManager> sm = defaultServiceManager();
47    if (sm == NULL) {
48        fprintf(stderr, "error: can't get default service manager\n");
49        return 1;
50    }
51
52    if (argv[1][0] == ':') {
53        String16 name(argv[1] + 1);
54        status_t status = sm->addService(name, new BnInterface<ITestService>());
55        if (status != OK) {
56            fprintf(stderr, "error: can't register service: %s\n", argv[1] + 1);
57            return 1;
58        }
59
60        ProcessState::self()->startThreadPool();
61        IPCThreadState::self()->joinThreadPool();
62        fprintf(stderr, "error: can't run service\n");
63        return 1;
64    }
65
66    sp<IBinder> service = sm->checkService(String16(argv[1]));
67    if (service == NULL) {
68        fprintf(stderr, "error: can't find service: %s\n", argv[1]);
69        return 1;
70    }
71
72    for (int i = 0; i < WARMUP; i++) {
73        status_t status = service->pingBinder();
74        if (status != OK) {
75            fprintf(stderr, "error: can't ping: %s [%d]\n", argv[1], status);
76            return 1;
77        }
78    }
79
80    struct timespec before, after;
81    clock_gettime(CLOCK_MONOTONIC, &before);
82    for (int i = 0; i < COUNT; i++) {
83        status_t status = service->pingBinder();
84        if (status != OK) {
85            fprintf(stderr, "error: can't ping: %s [%d]\n", argv[1], status);
86            return 1;
87        }
88    }
89    clock_gettime(CLOCK_MONOTONIC, &after);
90
91    double seconds = (after.tv_sec - before.tv_sec);
92    seconds += (after.tv_nsec - before.tv_nsec) / 1000000000.0;
93    printf("%d calls in %.3f sec => %.3f ms/call\n",
94        COUNT, seconds, 1000.0 * seconds / COUNT);
95
96    return 0;
97}
98