Remove build warning messages
[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 #include <glib.h>
18 #include <signal.h>
19 #include <sys/types.h>
20 #include <sys/wait.h>
21 #include <unistd.h>
22
23 #include <chrono>
24 #include <iostream>
25
26 #include "log-private.hh"
27 #include "options.hh"
28 #include "BenchmarkProxy.h"
29
30 namespace {
31
32 #define SERVER_PROC_NAME "org.tizen.appfw.rpc_port.benchmark"
33 #define SERVER_BIN "/usr/bin/rpc-port-benchmark-server"
34
35 namespace bp = rpc_port::BenchmarkProxy::proxy;
36
37 class ConnectionListener : public bp::Benchmark::IEventListener {
38  public:
39   void OnConnected() override {
40   }
41
42   void OnDisconnected() override {
43   }
44
45   void OnRejected() override {
46   }
47 };
48
49 class Tester {
50  public:
51   Tester() {
52   }
53
54   ~Tester() {
55     if (server_pid_ > 0) {
56       if (kill(server_pid_, SIGTERM) != 0) {
57         std::cerr << "kill() is failed. errno: " << errno << std::endl;
58       } else {
59         int status;
60         waitpid(server_pid_, &status, 0);
61       }
62     }
63   }
64
65   void Run(int argc, char** argv) {
66     options_ = rpc_port::benchmark::Options::Parse(argc, argv);
67     if (!options_) {
68       _E("options is nullptr");
69       exit(1);
70     }
71
72     ExecuteServer();
73     proxy_.reset(new bp::Benchmark(&listener_, SERVER_PROC_NAME));
74
75     try {
76       proxy_->Connect(true);
77     } catch (const bp::Exception& e) {
78       _E("Connect() failed");
79       exit(1);
80     }
81
82     printf("%15s\t%15s\t%15s\t\t%15s\t\t%15s\n", "Iterations", "Data size",
83         "Time", "Throughput", "Latency");
84     if (options_->IsAll()) {
85       DoTest(4000, 10);
86       DoTest(4000, 20);
87       DoTest(4000, 100);
88       DoTest(4000, 200);
89       DoTest(2000, 1000);
90       DoTest(2000, 2000);
91       DoTest(1000, 10000);
92       DoTest(1000, 20000);
93       DoTest(1000, 30000);
94       DoTest(1000, 40000);
95       DoTest(1000, 50000);
96       DoTest(1000, 60000);
97       DoTest(500, 100000);
98     } else {
99       DoTest(options_->GetIters(), options_->GetSize());
100     }
101   }
102
103  private:
104   void DoTest(int iters, int size) {
105     bool is_func = options_->IsFunction();
106
107     StartTime();
108     for (int i = 0; i < iters; i++) {
109       if (is_func) {
110         int ret = FakeFunction(std::string(size, 'a'));
111         if (ret != 0) {
112           _E("Invalid return");
113           break;
114         }
115
116         continue;
117       }
118
119       int ret = proxy_->Test(std::string(size, 'a'));
120       if (ret != 0) {
121         _E("Invalid return");
122         break;
123       }
124     }
125     EndTime(iters, size);
126   }
127
128   int FakeFunction(std::string str) {
129     return 0;
130   }
131
132   void StartTime() {
133     start_ = std::chrono::system_clock::now();
134   }
135
136   void EndTime(int iters, int size) {
137     std::chrono::duration<double> sec = std::chrono::system_clock::now() - start_;
138     double t = size * iters * 8 / sec.count() / 1024 / 1024;
139     double l = sec.count() * 1000 / iters;
140
141     printf("%10d\t%10dByte\t%15.4fs\t%15.4fMb/s\t%15.4fms\n", iters, size,
142         sec.count(), t, l);
143   }
144
145   void ExecuteServer() {
146     server_pid_ = fork();
147     if (server_pid_ == 0) {
148       setsid();
149
150       char bin[] = { SERVER_BIN };
151       char* argv[] = { bin, nullptr, nullptr };
152       int ret = execv(argv[0], argv);
153       if (ret < 0) {
154         std::cerr << "execv() is failed. errno: " << errno << std::endl;
155         exit(-1);
156       }
157     } else if (server_pid_ == -1) {
158       std::cerr << "fork() is failed. errno: " << errno << std::endl;
159       exit(-1);
160     } else {
161       _W("benchmark server is running. pid: %d", server_pid_);
162       usleep(100 * 1000);
163     }
164   }
165
166  private:
167   std::unique_ptr<rpc_port::benchmark::Options> options_;
168   std::unique_ptr<bp::Benchmark> proxy_;
169   ConnectionListener listener_;
170   std::chrono::system_clock::time_point start_;
171   pid_t server_pid_ = -1;
172 };
173
174 }  // namespace
175
176 int main(int argc, char** argv) {
177   Tester tester;
178   try {
179     tester.Run(argc, argv);
180   } catch (const std::exception& e) {
181     std::cerr << "Exception occurred!!!" << e.what() << std::endl;
182   }
183
184   return 0;
185 }