From 47f1d17ad30df5662fdf663e32f5f0b504fc335b Mon Sep 17 00:00:00 2001 From: Tatsuhiro Tsujikawa Date: Sat, 10 Oct 2015 11:36:13 +0900 Subject: [PATCH] h2load: Support subsecond rate period This change adds subsecond rate period support to h2load. Now --rate-period option only accepts integer, but it can be followed by units. Currently, h, m, s, and ms are supported, which are hours, minutes, seconds, and milliseconds respectively. The underlying functionality and usecase are already extensively used in nghttpx. --- src/h2load.cc | 42 +++++++++++++++++++----------------------- src/util.h | 7 ++++--- 2 files changed, 23 insertions(+), 26 deletions(-) diff --git a/src/h2load.cc b/src/h2load.cc index 944a409a..f3156015 100644 --- a/src/h2load.cc +++ b/src/h2load.cc @@ -1261,7 +1261,7 @@ std::unique_ptr create_worker(uint32_t id, SSL_CTX *ssl_ctx, std::stringstream rate_report; if (config.is_rate_mode() && nclients > rate) { rate_report << "Up to " << rate << " client(s) will be created every " - << std::setprecision(3) << config.rate_period << " seconds. "; + << util::duration_str(config.rate_period) << " "; } std::cout << "spawning thread #" << id << ": " << nclients @@ -1379,12 +1379,12 @@ Options: will run as it normally does, creating connections at whatever variable rate it wants. The default value for this option is 0. - --rate-period= - Specifies the time period between creating connections. - The period must be a positive number greater than or - equal to 1.0, representing the length of the period in - seconds. This option is ignored if the rate option is - not used. The default value for this option is 1.0. + --rate-period= + Specifies the time period between creating connections. + The period must be a positive number, representing the + length of the period in time. This option is ignored if + the rate option is not used. The default value for this + option is 1s. -T, --connection-active-timeout= Specifies the maximum time that h2load is willing to keep a connection open, regardless of the activity on @@ -1434,7 +1434,14 @@ Options: -v, --verbose Output debug information. --version Display version information and exit. - -h, --help Display this help and exit.)" << std::endl; + -h, --help Display this help and exit. + +-- + + The argument is an integer and an optional unit (e.g., 1s + is 1 second and 500ms is 500 milliseconds). Units are h, m, s or ms + (hours, minutes, seconds and milliseconds, respectively). If a unit + is omitted, a second is used as unit.)" << std::endl; } } // namespace @@ -1629,26 +1636,15 @@ int main(int argc, char **argv) { // npn-list option config.npn_list = util::parse_config_str_list(optarg); break; - case 5: { + case 5: // rate-period - const char *start = optarg; - char *end; - errno = 0; - auto v = std::strtod(start, &end); - - if (v < 1.0 || !std::isfinite(v) || end == start || errno != 0) { - auto error = errno; - std::cerr << "Rate period value error " << optarg << std::endl; - if (error != 0) { - std::cerr << "\n\t" << strerror(error) << std::endl; - } + config.rate_period = util::parse_duration_with_unit(optarg); + if (!std::isfinite(config.rate_period)) { + std::cerr << "--rate-period: value error " << optarg << std::endl; exit(EXIT_FAILURE); } - - config.rate_period = v; break; } - } break; default: break; diff --git a/src/util.h b/src/util.h index 7998b4c0..00265e87 100644 --- a/src/util.h +++ b/src/util.h @@ -659,9 +659,10 @@ int64_t parse_uint(const std::string &s); // Parses NULL terminated string |s| as unsigned integer and returns // the parsed integer casted to double. If |s| ends with "s", the // parsed value's unit is a second. If |s| ends with "ms", the unit -// is millisecond. If none of them are given, the unit is second. -// This function returns std::numeric_limits::infinity() if -// error occurs. +// is millisecond. Similarly, it also supports 'm' and 'h' for +// minutes and hours respectively. If none of them are given, the +// unit is second. This function returns +// std::numeric_limits::infinity() if error occurs. double parse_duration_with_unit(const char *s); // Returns string representation of time duration |t|. If t has