From ba096555abf5cd161bf687e7ed7f82d6a83e20e1 Mon Sep 17 00:00:00 2001 From: Kolobaev Pavel Date: Sun, 17 Feb 2019 18:33:24 +0300 Subject: [PATCH 1/3] Fix: use getopt for parse parametrs --- main.cpp | 155 +++++++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 128 insertions(+), 27 deletions(-) diff --git a/main.cpp b/main.cpp index d918c86..d1ba276 100644 --- a/main.cpp +++ b/main.cpp @@ -11,15 +11,16 @@ #else #include #endif +#include #include #include -#include // TODO: RMOVE IT ! #include #include "mysignals.h" #include "radosutil.h" +#include using namespace librados; using namespace std; @@ -37,6 +38,15 @@ template static uint64_t dur2nsec(const T &dur) { return duration_cast>(dur).count(); } +struct bench_options { + string pool; + string mode; + string specific_bench_item; + unsigned int threads; + unsigned int secs; + size_t block_size; +}; + template static void print_breakdown(const vector &summary, size_t thread_count, size_t block_size) { @@ -228,35 +238,126 @@ static void do_bench(unsigned int secs, const vector &names, print_breakdown(summary, names.size(), block_size); } +static void print_help() { + std::cout << "--pool-name Pool name\n" + "--mode Set mode value. Valide value: " + "\"host\", \"osd\"\n" + "--specific-bench-item Set specific bench item. Like " + "a osd.14, ceph-node-01, etc\n" + "--block-size Set block size value in bytes. " + "Default 4096 * 1024 byte\n" + "--seconds Default 10 seconds\n" + "--threads Default 1 thread\n" + "--help Show help\n"; + //<< std::endl; +} + +static int parse_parametrs(int argc, const char *argv[], + bench_options *settings) { + const char *const short_opts = "p:m:i:b:s:t:h"; + int index = 0; + const option long_opts[] = { + {"help", no_argument, nullptr, 'h'}, + {"pool-name", required_argument, nullptr, 'p'}, + {"mode", required_argument, nullptr, 'm'}, + {"specific-bench-item", required_argument, nullptr, 'i'}, + {"block-size", required_argument, 0, 'b'}, + {"seconds", required_argument, 0, 's'}, + {"threads", required_argument, 0, 't'}, + {nullptr, no_argument, nullptr, 0}}; + + while (true) { + const auto opt = getopt_long_only(argc, (char *const *)argv, short_opts, + long_opts, &index); + + if (-1 == opt) + break; + switch (opt) { + case 'p': + try { + settings->pool = std::string(optarg); + std::cout << "Poolname:\t" << settings->pool << std::endl; + } catch (const std::invalid_argument &obj) { + std::cerr << "Wrong value for option --pool-name" << std::endl; + return (1); + } + break; + + case 'm': + try { + settings->mode = std::string(optarg); + std::cout << "Mode:\t\t" << settings->mode << std::endl; + } catch (const std::exception &obj) { + std::cerr << "Wrong value for option --mode" << std::endl; + return (1); + } + break; + case 'i': + try { + settings->specific_bench_item = std::string(optarg); + std::cout << "Specific item name:\t" << settings->specific_bench_item + << std::endl; + } catch (const std::exception &obj) { + std::cerr << "Wrong value for option --specific-bench-item" + << std::endl; + return (1); + } + break; + case 'b': + try { + settings->block_size = std::stoull(optarg); + std::cout << "Block size:\t" << settings->block_size << std::endl; + } catch (const std::exception &obj) { + std::cerr << "Wrong value for option --block-size" << std::endl; + return (1); + } + break; + case 's': + try { + settings->secs = std::stoul(optarg); + std::cout << "Seconds:\t" << settings->secs << std::endl; + } catch (const std::exception &obj) { + std::cerr << "Wrong value for option --seconds" << std::endl; + return (1); + } + break; + case 't': + try { + settings->threads = std::stoul(optarg); + std::cout << "Threads:\t" << settings->threads << std::endl; + } catch (const std::exception &obj) { + std::cerr << "Wrong value for option --threads" << std::endl; + return (1); + } + break; + + case 'h': // -h or --help + case '?': // Unrecognized option + default: + print_help(); + exit(0); + break; + } + } + return 0; +} + static void _main(int argc, const char *argv[]) { - struct { - string pool; - string mode; - string specific_bench_item; - unsigned int threads; - unsigned int secs; - size_t block_size; - } settings; - - switch (argc) { - case 3: - settings.pool = argv[1]; - settings.mode = argv[2]; - break; - case 4: - settings.pool = argv[1]; - settings.mode = argv[2]; - settings.specific_bench_item = argv[3]; - break; - default: - cerr << "Usage: " << argv[0] - << " [poolname] [mode=host|osd] " << endl; - throw "Wrong cmdline"; + bench_options settings; + + if (0 != parse_parametrs(argc, argv, &settings)) { + print_help(); } - settings.secs = 10; - settings.threads = 1; - settings.block_size = 4096 * 1024; + if (0 == settings.secs) { + settings.secs = 10; + } + if (0 == settings.threads) { + settings.threads = 1; + } + if (0 == settings.block_size) { + settings.block_size = 4096 * 1024; + } Rados rados; int err; From f1964e366577bb523c9cdd69ce9e13337a2d6620 Mon Sep 17 00:00:00 2001 From: StreSSt Date: Sun, 17 Feb 2019 18:33:24 +0300 Subject: [PATCH 2/3] Fix: use getopt for parse parametrs --- main.cpp | 155 +++++++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 128 insertions(+), 27 deletions(-) diff --git a/main.cpp b/main.cpp index d918c86..d1ba276 100644 --- a/main.cpp +++ b/main.cpp @@ -11,15 +11,16 @@ #else #include #endif +#include #include #include -#include // TODO: RMOVE IT ! #include #include "mysignals.h" #include "radosutil.h" +#include using namespace librados; using namespace std; @@ -37,6 +38,15 @@ template static uint64_t dur2nsec(const T &dur) { return duration_cast>(dur).count(); } +struct bench_options { + string pool; + string mode; + string specific_bench_item; + unsigned int threads; + unsigned int secs; + size_t block_size; +}; + template static void print_breakdown(const vector &summary, size_t thread_count, size_t block_size) { @@ -228,35 +238,126 @@ static void do_bench(unsigned int secs, const vector &names, print_breakdown(summary, names.size(), block_size); } +static void print_help() { + std::cout << "--pool-name Pool name\n" + "--mode Set mode value. Valide value: " + "\"host\", \"osd\"\n" + "--specific-bench-item Set specific bench item. Like " + "a osd.14, ceph-node-01, etc\n" + "--block-size Set block size value in bytes. " + "Default 4096 * 1024 byte\n" + "--seconds Default 10 seconds\n" + "--threads Default 1 thread\n" + "--help Show help\n"; + //<< std::endl; +} + +static int parse_parametrs(int argc, const char *argv[], + bench_options *settings) { + const char *const short_opts = "p:m:i:b:s:t:h"; + int index = 0; + const option long_opts[] = { + {"help", no_argument, nullptr, 'h'}, + {"pool-name", required_argument, nullptr, 'p'}, + {"mode", required_argument, nullptr, 'm'}, + {"specific-bench-item", required_argument, nullptr, 'i'}, + {"block-size", required_argument, 0, 'b'}, + {"seconds", required_argument, 0, 's'}, + {"threads", required_argument, 0, 't'}, + {nullptr, no_argument, nullptr, 0}}; + + while (true) { + const auto opt = getopt_long_only(argc, (char *const *)argv, short_opts, + long_opts, &index); + + if (-1 == opt) + break; + switch (opt) { + case 'p': + try { + settings->pool = std::string(optarg); + std::cout << "Poolname:\t" << settings->pool << std::endl; + } catch (const std::invalid_argument &obj) { + std::cerr << "Wrong value for option --pool-name" << std::endl; + return (1); + } + break; + + case 'm': + try { + settings->mode = std::string(optarg); + std::cout << "Mode:\t\t" << settings->mode << std::endl; + } catch (const std::exception &obj) { + std::cerr << "Wrong value for option --mode" << std::endl; + return (1); + } + break; + case 'i': + try { + settings->specific_bench_item = std::string(optarg); + std::cout << "Specific item name:\t" << settings->specific_bench_item + << std::endl; + } catch (const std::exception &obj) { + std::cerr << "Wrong value for option --specific-bench-item" + << std::endl; + return (1); + } + break; + case 'b': + try { + settings->block_size = std::stoull(optarg); + std::cout << "Block size:\t" << settings->block_size << std::endl; + } catch (const std::exception &obj) { + std::cerr << "Wrong value for option --block-size" << std::endl; + return (1); + } + break; + case 's': + try { + settings->secs = std::stoul(optarg); + std::cout << "Seconds:\t" << settings->secs << std::endl; + } catch (const std::exception &obj) { + std::cerr << "Wrong value for option --seconds" << std::endl; + return (1); + } + break; + case 't': + try { + settings->threads = std::stoul(optarg); + std::cout << "Threads:\t" << settings->threads << std::endl; + } catch (const std::exception &obj) { + std::cerr << "Wrong value for option --threads" << std::endl; + return (1); + } + break; + + case 'h': // -h or --help + case '?': // Unrecognized option + default: + print_help(); + exit(0); + break; + } + } + return 0; +} + static void _main(int argc, const char *argv[]) { - struct { - string pool; - string mode; - string specific_bench_item; - unsigned int threads; - unsigned int secs; - size_t block_size; - } settings; - - switch (argc) { - case 3: - settings.pool = argv[1]; - settings.mode = argv[2]; - break; - case 4: - settings.pool = argv[1]; - settings.mode = argv[2]; - settings.specific_bench_item = argv[3]; - break; - default: - cerr << "Usage: " << argv[0] - << " [poolname] [mode=host|osd] " << endl; - throw "Wrong cmdline"; + bench_options settings; + + if (0 != parse_parametrs(argc, argv, &settings)) { + print_help(); } - settings.secs = 10; - settings.threads = 1; - settings.block_size = 4096 * 1024; + if (0 == settings.secs) { + settings.secs = 10; + } + if (0 == settings.threads) { + settings.threads = 1; + } + if (0 == settings.block_size) { + settings.block_size = 4096 * 1024; + } Rados rados; int err; From e9250beb75e1817cf6e87041b31a7c24b2b85a24 Mon Sep 17 00:00:00 2001 From: StreSSt Date: Sun, 17 Feb 2019 18:52:03 +0300 Subject: [PATCH 3/3] Fix: set default values --- main.cpp | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/main.cpp b/main.cpp index d1ba276..c560bbb 100644 --- a/main.cpp +++ b/main.cpp @@ -344,7 +344,10 @@ static int parse_parametrs(int argc, const char *argv[], static void _main(int argc, const char *argv[]) { bench_options settings; - + settings.sec = 0; + settings.threads = 0; + settings.block_size = 0; + if (0 != parse_parametrs(argc, argv, &settings)) { print_help(); }