2 * Copyright (c) 2022 Samsung Electronics Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 #include <sys/types.h>
26 #include "log-private.hh"
28 #include "BenchmarkProxy.h"
32 constexpr const char SERVER_PROC_NAME[] = "org.tizen.appfw.rpc_port.benchmark";
33 constexpr const char SERVER_BIN[] = "/usr/bin/rpc-port-benchmark-server";
35 namespace bp = rpc_port::BenchmarkProxy::proxy;
37 class ConnectionListener : public bp::Benchmark::IEventListener {
39 void OnConnected() override {
42 void OnDisconnected() override {
45 void OnRejected() override {
55 if (server_pid_ > 0) {
56 if (kill(server_pid_, SIGTERM) != 0) {
57 std::cerr << "kill() is failed. errno: " << errno << std::endl;
60 waitpid(server_pid_, &status, 0);
65 void Run(int argc, char** argv) {
66 options_ = rpc_port::benchmark::Options::Parse(argc, argv);
68 _E("options is nullptr");
73 proxy_.reset(new bp::Benchmark(&listener_, SERVER_PROC_NAME));
76 proxy_->Connect(true);
77 } catch (const bp::Exception& e) {
78 _E("Connect() failed");
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()) {
99 DoTest(options_->GetIters(), options_->GetSize());
104 void DoTest(int iters, int size) {
105 bool is_func = options_->IsFunction();
108 for (int i = 0; i < iters; i++) {
110 int ret = FakeFunction(std::string(size, 'a'));
112 _E("Invalid return");
119 int ret = proxy_->Test(std::string(size, 'a'));
121 _E("Invalid return");
125 EndTime(iters, size);
128 int FakeFunction(std::string str) {
133 start_ = std::chrono::system_clock::now();
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;
141 printf("%10d\t%10dByte\t%15.4fs\t%15.4fMb/s\t%15.4fms\n", iters, size,
145 void ExecuteServer() {
146 server_pid_ = fork();
147 if (server_pid_ == 0) {
150 char* argv[] = { strdup(SERVER_BIN), nullptr, nullptr };
151 int ret = execv(argv[0], argv);
153 std::cerr << "execv() is failed. errno: " << errno << std::endl;
156 } else if (server_pid_ == -1) {
157 std::cerr << "fork() is failed. errno: " << errno << std::endl;
160 _W("benchmark server is running. pid: %d", server_pid_);
166 std::unique_ptr<rpc_port::benchmark::Options> options_;
167 std::unique_ptr<bp::Benchmark> proxy_;
168 ConnectionListener listener_;
169 std::chrono::system_clock::time_point start_;
170 pid_t server_pid_ = -1;
175 int main(int argc, char** argv) {
177 tester.Run(argc, argv);