Separate benchmark tool from rpc-port
[platform/core/appfw/rpc-port.git] / benchmark / server / tidl / 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 <sys/types.h>
19 #include <unistd.h>
20
21 #include <rpc-port-internal.h>
22
23 #include "BenchmarkStub.h"
24 #include "log-private.hh"
25
26 namespace {
27 constexpr const char SERVER_PROC_NAME[] =
28     "d::org.tizen.appfw.rpc_port.benchmark";
29
30 namespace bs = rpc_port::BenchmarkStub::stub;
31
32 class MainLoop {
33  public:
34   MainLoop();
35   ~MainLoop();
36
37   void Run();
38   void Quit();
39   void SetTermTimer();
40   void RemoveTermTimer();
41
42  private:
43   GMainLoop* loop_;
44   bs::Benchmark stub_;
45   guint timer_ = 0;
46 };
47
48 class TestService : public bs::Benchmark::ServiceBase {
49  public:
50   class Factory : public bs::Benchmark::ServiceBase::Factory {
51    public:
52     explicit Factory(MainLoop* loop) : loop_(loop) {}
53
54     virtual ~Factory() = default;
55
56     std::unique_ptr<bs::Benchmark::ServiceBase>
57         CreateService(std::string sender, std::string instance) {
58       return std::unique_ptr<bs::Benchmark::ServiceBase>(
59           new TestService(std::move(sender), std::move(instance), loop_));
60     }
61
62    private:
63     MainLoop* loop_ = nullptr;
64   };
65
66   TestService(std::string sender, std::string instance, MainLoop* loop)
67       : bs::Benchmark::ServiceBase(std::move(sender), std::move(instance)),
68         loop_(loop) {}
69
70   virtual ~TestService() = default;
71
72   void OnCreate() override {
73     loop_->RemoveTermTimer();
74   }
75
76   void OnTerminate() override {
77     loop_->SetTermTimer();
78   }
79
80   int Test(std::string data) override { return 0; }
81
82  private:
83   MainLoop* loop_ = nullptr;
84 };
85
86 MainLoop::MainLoop() { loop_ = g_main_loop_new(nullptr, FALSE); }
87
88 MainLoop::~MainLoop() {
89   RemoveTermTimer();
90   g_main_loop_unref(loop_);
91 }
92
93 void MainLoop::Run() {
94   std::string proc_name = std::string(SERVER_PROC_NAME);
95   if (getuid() >= 5000) proc_name = "u" + proc_name;
96
97   int ret = rpc_port_register_proc_info(proc_name.c_str(), nullptr);
98   if (ret != RPC_PORT_ERROR_NONE) {
99     _E("rpc_port_register_proc_info() failed (%d)", ret);
100     return;
101   }
102
103   try {
104     stub_.Listen(std::shared_ptr<bs::Benchmark::ServiceBase::Factory>(
105         new TestService::Factory(this)));
106   } catch (const bs::Exception&) {
107     _E("stub listen is failed");
108   }
109
110   g_main_loop_run(loop_);
111 }
112
113 void MainLoop::Quit() { g_main_loop_quit(loop_); }
114
115 void MainLoop::SetTermTimer() {
116   if (timer_) return;
117
118   timer_ = g_timeout_add(
119       5000,
120       [](gpointer data) -> gboolean {
121         auto* main_loop = static_cast<MainLoop*>(data);
122         main_loop->Quit();
123         main_loop->timer_ = 0;
124         return G_SOURCE_REMOVE;
125       },
126       this);
127 }
128
129 void MainLoop::RemoveTermTimer() {
130   if (timer_) {
131     g_source_remove(timer_);
132     timer_ = 0;
133   }
134 }
135
136 }  // namespace
137
138 int main(int argc, char** argv) {
139   MainLoop loop;
140   loop.Run();
141   return 0;
142 }