Add benchmark tool
[platform/core/appfw/rpc-port.git] / benchmark / tool / main.cc
1 /*
2  * Copyright (c) 2022 Samsung Electronics Co., Ltd.
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
18 #include <glib.h>
19
20 #include <chrono>
21 #include <iostream>
22
23 #include "log-private.hh"
24 #include "options.hh"
25 #include "BenchmarkProxy.h"
26
27 namespace {
28
29 constexpr const char SERVER_PROC_NAME[] = "org.tizen.appfw.rpc_port.benchmark";
30
31 namespace bp = rpc_port::BenchmarkProxy::proxy;
32
33 class ConnectionListener : public bp::Benchmark::IEventListener {
34  public:
35   void OnConnected() override {
36   }
37
38   void OnDisconnected() override {
39   }
40
41   void OnRejected() override {
42   }
43 };
44
45 class Tester {
46  public:
47   Tester() {
48   }
49
50   ~Tester() {
51   }
52
53   void Run(int argc, char** argv) {
54     options_ = rpc_port::benchmark::Options::Parse(argc, argv);
55     if (!options_) {
56       _E("options is nullptr");
57       exit(1);
58     }
59
60     proxy_.reset(new bp::Benchmark(&listener_, SERVER_PROC_NAME));
61
62     try {
63       proxy_->Connect(true);
64     } catch (const bp::Exception& e) {
65       _E("Connect() failed");
66       exit(1);
67     }
68
69     DoTest();
70   }
71
72  private:
73   void DoTest() {
74     int iters = options_->GetIters();
75     int size = options_->GetSize();
76
77     StartTime();
78     for (int i = 0; i < iters; i++) {
79       int ret = proxy_->Test(std::string(size, 'a'));
80       if (ret != 0) {
81         _E("Invalid return");
82         break;
83       }
84     }
85     EndTime();
86   }
87
88   void StartTime() {
89     start_ = std::chrono::system_clock::now();
90   }
91
92   void EndTime() {
93     std::chrono::duration<double> sec = std::chrono::system_clock::now() - start_;
94     std::cout << "Iterations: " << std::to_string(options_->GetIters()) << std::endl;
95     std::cout << "Data size: " << std::to_string(options_->GetSize()) << "Byte" << std::endl;
96     std::cout << "Duration: " << sec.count() << "s" << std::endl;
97     double t = options_->GetSize() * options_->GetIters() / sec.count() / 1024 / 1024;
98     std::cout << "Throughput: " << std::to_string(t * 8) << "Mb/s" << std::endl;
99     double l = sec.count() * 1000 / options_->GetIters();
100     std::cout << "Latency: " << std::to_string(l) << "ms" << std::endl;
101   }
102
103  private:
104   std::unique_ptr<rpc_port::benchmark::Options> options_;
105   std::unique_ptr<bp::Benchmark> proxy_;
106   ConnectionListener listener_;
107   std::chrono::system_clock::time_point start_;
108 };
109
110 }  // namespace
111
112 int main(int argc, char** argv) {
113   Tester tester;
114   tester.Run(argc, argv);
115   return 0;
116 }