Separate benchmark tool from rpc-port
[platform/core/appfw/rpc-port.git] / benchmark / tool / options.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 <getopt.h>
18
19 #include <iostream>
20 #include <cstring>
21 #include <memory>
22
23 #include "options.hh"
24
25 namespace rpc_port {
26 namespace benchmark {
27
28 Options::Options() {
29   help_ = R"__option_cb(
30 Usage:
31   rpc-port-benchmark-tool [OPTION...]
32
33 Help Options:
34   -h, --help                  Show help options
35
36 Additional Options:
37   -f, --funct ion                     Use function call instead of RPC
38   -d, --dbus                          Use Dbus method instead of TIDL RPC
39   -g, --grpc                          Use gRPC over UDS instead of TIDL RPC
40   -a, --all                           Test pre-defined test-cases
41   -i, --interations=<Iterations>      Iterations
42   -s, --size=<Data size>              Data size (byte)
43   -t, --time                          Print starting time of this tool
44
45 Application Options:
46   -v, --version               Show version information
47 )__option_cb";
48 }
49
50 void Options::PrintUsage() {
51   std::cerr << help_ << std::endl;
52 }
53
54 void Options::PrintVersion() {
55   std::cerr << "rpc-port-benchmark-tool " << FULLVER << std::endl;
56 }
57
58 void Options::PrintSample() {
59   std::cerr << "rpc-port-benchmark-tool -i 1000 -s 10000" << std::endl;
60 }
61
62 std::unique_ptr<Options> Options::Parse(int argc, char** argv) {
63   bool cmd[CMD_MAX] = { false, };
64   bool opt[OPT_MAX] = { false, };
65   auto options = std::unique_ptr<Options>(new Options());
66   int option_index = 0;
67
68   struct option long_options[] = {
69     {"version", no_argument, nullptr, 'v'},
70     {"help", no_argument, nullptr, 'h'},
71     {"iterations", required_argument, nullptr, 'i'},
72     {"size", required_argument, nullptr, 's'},
73     {"function", no_argument, nullptr, 'f'},
74     {"dbus", no_argument, nullptr, 'd'},
75     {"grpc", no_argument, nullptr, 'g'},
76     {"all", no_argument, nullptr, 'a'},
77     {"time", no_argument, nullptr, 't'},
78     {0, 0, 0, 0}
79   };
80
81   while (true) {
82     int c = getopt_long(argc, argv, "vhfdgati:s:", long_options,
83         &option_index);
84     if (c == -1)
85       break;
86
87     switch (c) {
88       case 0:
89         break;
90
91       case 'v':
92         cmd[CMD_VERSION] = true;
93         break;
94
95       case 'h':
96         cmd[CMD_HELP] = true;
97         break;
98
99       case 'i':
100         opt[OPT_ITER] = true;
101         options->iters_ = std::stoi(optarg);
102         break;
103
104       case 's':
105         opt[OPT_SIZE] = true;
106         options->size_ = std::stoi(optarg);
107         break;
108
109       case 'f':
110         opt[OPT_FUNCTION] = true;
111         options->is_function_ = true;
112         break;
113
114       case 'd':
115         opt[OPT_DBUS] = true;
116         options->is_dbus_ = true;
117         break;
118
119       case 'g':
120         opt[OPT_GRPC] = true;
121         options->is_grpc_ = true;
122         break;
123
124       case 'a':
125         opt[OPT_ALL] = true;
126         options->is_all_ = true;
127         break;
128
129       case 't':
130         opt[OPT_TIME] = true,
131         options->should_print_time_ = true;
132         break;
133
134       default:
135         cmd[CMD_HELP] = true;
136     }
137   }
138
139   if (cmd[CMD_VERSION]) {
140     options->PrintVersion();
141     return std::unique_ptr<Options>(nullptr);
142   }
143
144   if (cmd[CMD_HELP]) {
145     options->PrintUsage();
146     return std::unique_ptr<Options>(nullptr);
147   } else if (opt[OPT_ALL]) {
148     return options;
149   } else if (!opt[OPT_ITER] || !opt[OPT_SIZE]) {
150     options->PrintSample();
151     return std::unique_ptr<Options>(nullptr);
152   }
153
154   return options;
155 }
156
157 }  // namespace benchmark
158 }  // namespace rpc_port