From ca8b9e2d70be7d22351badc39d35fdd89a2c77c6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=9C=A4=EC=A7=80=EC=98=81/On-Device=20Lab=28SR=29/Staff?= =?UTF-8?q?=20Engineer/=EC=82=BC=EC=84=B1=EC=A0=84=EC=9E=90?= Date: Tue, 4 Jun 2019 13:57:13 +0900 Subject: [kbenchmark] Add argument parser (#5336) This patch adds argument parsers for kbenchmark tool. Signed-off-by: Jiyoung Yun --- tools/kbenchmark/Args.cc | 105 ++++++++++++++++++++++++++++++++++++++++ tools/kbenchmark/Args.h | 53 ++++++++++++++++++++ tools/kbenchmark/CMakeLists.txt | 14 ++++++ tools/kbenchmark/Driver.cc | 40 +++++++++++++++ tools/kbenchmark/README.md | 21 ++++++++ 5 files changed, 233 insertions(+) create mode 100644 tools/kbenchmark/Args.cc create mode 100644 tools/kbenchmark/Args.h create mode 100644 tools/kbenchmark/Driver.cc (limited to 'tools') diff --git a/tools/kbenchmark/Args.cc b/tools/kbenchmark/Args.cc new file mode 100644 index 000000000..95145a84e --- /dev/null +++ b/tools/kbenchmark/Args.cc @@ -0,0 +1,105 @@ +/* + * Copyright (c) 2019 Samsung Electronics Co., Ltd. All Rights Reserved + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "Args.h" + +#include +#include + +namespace kbenchmark +{ + +Args::Args(const int argc, char **argv) noexcept { Initialize(argc, argv); } + +void Args::Initialize(const int argc, char **argv) +{ + // General options + po::options_description general("General options"); + // clang-format off + general.add_options()("help,h", "Display available options") + ("config,c", po::value(&_config)->required(), "Configuration filename") + ("kernel,k", po::value>(&_kernel)->multitoken()->composing()->required(), "Kernel library name, support multiple kernel libraries") + ("reporter,r", po::value(&_reporter)->default_value("standard"), "Set reporter types(standard, html, junit, csv)") + ("verbose,v", po::value(&_verbose)->default_value(0)->implicit_value(true), "Show verbose output") + ("output,o", po::value(&_output)->default_value(""), "Set additional strings for output file name") + ; + // clang-format on + + po::variables_map vm; + po::store(po::parse_command_line(argc, argv, general), vm); + + try + { + po::notify(vm); + } + catch (const boost::program_options::required_option &e) + { + if (vm.count("help")) + { + std::cout << general << std::endl; + exit(0); + } + else + { + throw e; + } + } + + if (vm.count("help")) + { + std::cout << general << std::endl; + exit(0); + } + + if (vm.count("config")) + { + if (_config.substr(_config.find_last_of(".") + 1) != "config") + { + std::cerr << "Please specify .config file" << std::endl; + exit(1); + } + + if (!boost::filesystem::exists(_config)) + { + std::cerr << _config << " file not found" << std::endl; + exit(1); + } + } + + if (vm.count("kernel")) + { + for (auto &k : _kernel) + { + if (!boost::filesystem::exists(k)) + { + std::cerr << k << " file not found" << std::endl; + exit(1); + } + } + } + + if (vm.count("reporter")) + { + if (_reporter != "junit" && _reporter != "csv" && _reporter != "html" && + _reporter != "standard") + { + std::cerr << "Invalid reporter" << std::endl; + exit(1); + } + } +} + +} // namespace kbenchmark diff --git a/tools/kbenchmark/Args.h b/tools/kbenchmark/Args.h new file mode 100644 index 000000000..b848f9579 --- /dev/null +++ b/tools/kbenchmark/Args.h @@ -0,0 +1,53 @@ +/* + * Copyright (c) 2019 Samsung Electronics Co., Ltd. All Rights Reserved + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef __KBENCHMARK_ARGS_H__ +#define __KBENCHMARK_ARGS_H__ + +#include +#include +#include + +namespace po = boost::program_options; + +namespace kbenchmark +{ + +class Args +{ +public: + Args(const int argc, char **argv) noexcept; + + const std::string &config(void) { return _config; } + const std::vector &kernel(void) { return _kernel; } + const std::string &reporter(void) { return _reporter; } + const std::string &output(void) { return _output; } + int verbose(void) { return _verbose; } + +private: + void Initialize(const int argc, char **argv); + +private: + std::string _config; + std::vector _kernel; + std::string _reporter; + std::string _output; + int _verbose; +}; + +} // namespace kbenchmark + +#endif // __KBENCHMARK_ARGS_H__ diff --git a/tools/kbenchmark/CMakeLists.txt b/tools/kbenchmark/CMakeLists.txt index 3207000b5..71881ec7b 100644 --- a/tools/kbenchmark/CMakeLists.txt +++ b/tools/kbenchmark/CMakeLists.txt @@ -7,3 +7,17 @@ nnfw_find_package(Nonius QUIET) if(NOT Nonius_FOUND) return() endif(NOT Nonius_FOUND) + +nnfw_find_package(Boost QUIET) + +if(NOT Boost_FOUND) + return() +endif(NOT Boost_FOUND) + +# driver +file(GLOB_RECURSE SOURCES "*.cc") + +add_executable(kbenchmark ${SOURCES}) +target_include_directories(kbenchmark PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}) +target_link_libraries(kbenchmark PUBLIC pthread boost_program_options boost_system boost_filesystem) +install(TARGETS kbenchmark DESTINATION bin) diff --git a/tools/kbenchmark/Driver.cc b/tools/kbenchmark/Driver.cc new file mode 100644 index 000000000..c2cc33454 --- /dev/null +++ b/tools/kbenchmark/Driver.cc @@ -0,0 +1,40 @@ +/* + * Copyright (c) 2019 Samsung Electronics Co., Ltd. All Rights Reserved + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "Args.h" + +#include +#include + +using namespace kbenchmark; + +int main(int argc, char *argv[]) +{ + Args args(argc, argv); + + // Set default test name + std::string config_name{args.config()}; + config_name = config_name.substr(config_name.find_last_of("/") + 1); + config_name = config_name.substr(0, config_name.find_last_of(".")); + std::string test_name{"test_benchmark_" + config_name}; + if (!args.output().empty()) + { + test_name += (std::string{"_"} + args.output()); + } + std::cout << "Benchmark test name\n " << test_name << std::endl; + + return 0; +} diff --git a/tools/kbenchmark/README.md b/tools/kbenchmark/README.md index 0a684a80d..61c34511b 100644 --- a/tools/kbenchmark/README.md +++ b/tools/kbenchmark/README.md @@ -52,3 +52,24 @@ weights_type: FLOAT32 bias: [32] ``` +## kbenchmark + +### Available commands +The binary takes the following required parameters: + +* `config`: `string` \ + The path to the configuration file. +* `kernel`: `string` \ + The path to the benchmark kernel library file. It allows multiple kernel libraries either by using space or by repeatedly calling `--kernel`. + +and the following optional parameters: + +* `reporter`: `string` \ + Set the reporter types among `standard`, `html`, `junit` or `csv`. Default reporter type is `standard`. +* `output`: `string` \ + Set the additional strings for output file name. +* `help`: \ + Display available options. +* `verbose`: \ + Show verbose messages. + -- cgit v1.2.3