mirror of
https://github.com/nghttp2/nghttp2.git
synced 2025-12-08 02:58:53 +08:00
h2load: Avoid ev_now
This commit is contained in:
@@ -344,11 +344,13 @@ void rps_cb(struct ev_loop *loop, ev_timer *w, int revents) {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
auto now = ev_now(loop);
|
auto now = std::chrono::steady_clock::now();
|
||||||
auto d = now - client->rps_duration_started;
|
auto d = now - client->rps_duration_started;
|
||||||
auto n = static_cast<size_t>(round(d * config.rps));
|
auto n = static_cast<size_t>(
|
||||||
|
round(std::chrono::duration<double>(d).count() * config.rps));
|
||||||
client->rps_req_pending += n;
|
client->rps_req_pending += n;
|
||||||
client->rps_duration_started = now - d + static_cast<double>(n) / config.rps;
|
client->rps_duration_started +=
|
||||||
|
util::duration_from(static_cast<double>(n) / config.rps);
|
||||||
|
|
||||||
if (client->rps_req_pending == 0) {
|
if (client->rps_req_pending == 0) {
|
||||||
return;
|
return;
|
||||||
@@ -425,10 +427,10 @@ void client_request_timeout_cb(struct ev_loop *loop, ev_timer *w, int revents) {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
ev_tstamp duration =
|
auto duration =
|
||||||
config.timings[client->reqidx] - config.timings[client->reqidx - 1];
|
config.timings[client->reqidx] - config.timings[client->reqidx - 1];
|
||||||
|
|
||||||
while (duration < 1e-9) {
|
while (duration < std::chrono::duration<double>(1e-9)) {
|
||||||
if (client->submit_request() != 0) {
|
if (client->submit_request() != 0) {
|
||||||
ev_timer_stop(client->worker->loop, w);
|
ev_timer_stop(client->worker->loop, w);
|
||||||
client->process_request_failure();
|
client->process_request_failure();
|
||||||
@@ -443,7 +445,7 @@ void client_request_timeout_cb(struct ev_loop *loop, ev_timer *w, int revents) {
|
|||||||
config.timings[client->reqidx] - config.timings[client->reqidx - 1];
|
config.timings[client->reqidx] - config.timings[client->reqidx - 1];
|
||||||
}
|
}
|
||||||
|
|
||||||
client->request_timeout_watcher.repeat = duration;
|
client->request_timeout_watcher.repeat = util::ev_tstamp_from(duration);
|
||||||
ev_timer_again(client->worker->loop, &client->request_timeout_watcher);
|
ev_timer_again(client->worker->loop, &client->request_timeout_watcher);
|
||||||
}
|
}
|
||||||
} // namespace
|
} // namespace
|
||||||
@@ -470,7 +472,6 @@ Client::Client(uint32_t id, Worker *worker, size_t req_todo)
|
|||||||
local_addr{},
|
local_addr{},
|
||||||
new_connection_requested(false),
|
new_connection_requested(false),
|
||||||
final(false),
|
final(false),
|
||||||
rps_duration_started(0),
|
|
||||||
rps_req_pending(0),
|
rps_req_pending(0),
|
||||||
rps_req_inflight(0) {
|
rps_req_inflight(0) {
|
||||||
if (req_todo == 0) { // this means infinite number of requests are to be made
|
if (req_todo == 0) { // this means infinite number of requests are to be made
|
||||||
@@ -1178,7 +1179,7 @@ int Client::connection_made() {
|
|||||||
if (config.rps_enabled()) {
|
if (config.rps_enabled()) {
|
||||||
rps_watcher.repeat = std::max(0.01, 1. / config.rps);
|
rps_watcher.repeat = std::max(0.01, 1. / config.rps);
|
||||||
ev_timer_again(worker->loop, &rps_watcher);
|
ev_timer_again(worker->loop, &rps_watcher);
|
||||||
rps_duration_started = ev_now(worker->loop);
|
rps_duration_started = std::chrono::steady_clock::now();
|
||||||
}
|
}
|
||||||
|
|
||||||
if (config.rps_enabled()) {
|
if (config.rps_enabled()) {
|
||||||
@@ -1202,9 +1203,9 @@ int Client::connection_made() {
|
|||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
|
||||||
ev_tstamp duration = config.timings[reqidx];
|
auto duration = config.timings[reqidx];
|
||||||
|
|
||||||
while (duration < 1e-9) {
|
while (duration < std::chrono::duration<double>(1e-9)) {
|
||||||
if (submit_request() != 0) {
|
if (submit_request() != 0) {
|
||||||
process_request_failure();
|
process_request_failure();
|
||||||
break;
|
break;
|
||||||
@@ -1217,10 +1218,10 @@ int Client::connection_made() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (duration >= 1e-9) {
|
if (duration >= std::chrono::duration<double>(1e-9)) {
|
||||||
// double check since we may have break due to reqidx wraps
|
// double check since we may have break due to reqidx wraps
|
||||||
// around back to 0
|
// around back to 0
|
||||||
request_timeout_watcher.repeat = duration;
|
request_timeout_watcher.repeat = util::ev_tstamp_from(duration);
|
||||||
ev_timer_again(worker->loop, &request_timeout_watcher);
|
ev_timer_again(worker->loop, &request_timeout_watcher);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1971,9 +1972,10 @@ std::vector<std::string> read_uri_from_file(std::istream &infile) {
|
|||||||
} // namespace
|
} // namespace
|
||||||
|
|
||||||
namespace {
|
namespace {
|
||||||
void read_script_from_file(std::istream &infile,
|
void read_script_from_file(
|
||||||
std::vector<ev_tstamp> &timings,
|
std::istream &infile,
|
||||||
std::vector<std::string> &uris) {
|
std::vector<std::chrono::steady_clock::duration> &timings,
|
||||||
|
std::vector<std::string> &uris) {
|
||||||
std::string script_line;
|
std::string script_line;
|
||||||
int line_count = 0;
|
int line_count = 0;
|
||||||
while (std::getline(infile, script_line)) {
|
while (std::getline(infile, script_line)) {
|
||||||
@@ -2006,7 +2008,9 @@ void read_script_from_file(std::istream &infile,
|
|||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
|
|
||||||
timings.push_back(v / 1000.0);
|
timings.emplace_back(
|
||||||
|
std::chrono::duration_cast<std::chrono::steady_clock::duration>(
|
||||||
|
std::chrono::duration<double, std::milli>(v)));
|
||||||
uris.push_back(script_line.substr(pos + 1, script_line.size()));
|
uris.push_back(script_line.substr(pos + 1, script_line.size()));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -73,7 +73,7 @@ struct Worker;
|
|||||||
struct Config {
|
struct Config {
|
||||||
std::vector<std::vector<nghttp2_nv>> nva;
|
std::vector<std::vector<nghttp2_nv>> nva;
|
||||||
std::vector<std::string> h1reqs;
|
std::vector<std::string> h1reqs;
|
||||||
std::vector<ev_tstamp> timings;
|
std::vector<std::chrono::steady_clock::duration> timings;
|
||||||
nghttp2::Headers custom_headers;
|
nghttp2::Headers custom_headers;
|
||||||
std::string scheme;
|
std::string scheme;
|
||||||
std::string host;
|
std::string host;
|
||||||
@@ -396,7 +396,7 @@ struct Client {
|
|||||||
ev_timer rps_watcher;
|
ev_timer rps_watcher;
|
||||||
// The timestamp that starts the period which contributes to the
|
// The timestamp that starts the period which contributes to the
|
||||||
// next request generation.
|
// next request generation.
|
||||||
ev_tstamp rps_duration_started;
|
std::chrono::steady_clock::time_point rps_duration_started;
|
||||||
// The number of requests allowed by rps, but limited by stream
|
// The number of requests allowed by rps, but limited by stream
|
||||||
// concurrency.
|
// concurrency.
|
||||||
size_t rps_req_pending;
|
size_t rps_req_pending;
|
||||||
|
|||||||
11
src/util.h
11
src/util.h
@@ -47,6 +47,8 @@
|
|||||||
#include <map>
|
#include <map>
|
||||||
#include <random>
|
#include <random>
|
||||||
|
|
||||||
|
#include <ev.h>
|
||||||
|
|
||||||
#include "url-parser/url_parser.h"
|
#include "url-parser/url_parser.h"
|
||||||
|
|
||||||
#include "template.h"
|
#include "template.h"
|
||||||
@@ -695,6 +697,15 @@ template <typename Clock, typename Rep> Rep clock_precision() {
|
|||||||
return duration.count();
|
return duration.count();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
template <typename Duration = std::chrono::steady_clock::duration>
|
||||||
|
Duration duration_from(ev_tstamp d) {
|
||||||
|
return std::chrono::duration_cast<Duration>(std::chrono::duration<double>(d));
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename Duration> ev_tstamp ev_tstamp_from(const Duration &d) {
|
||||||
|
return std::chrono::duration<double>(d).count();
|
||||||
|
}
|
||||||
|
|
||||||
int make_socket_closeonexec(int fd);
|
int make_socket_closeonexec(int fd);
|
||||||
int make_socket_nonblocking(int fd);
|
int make_socket_nonblocking(int fd);
|
||||||
int make_socket_nodelay(int fd);
|
int make_socket_nodelay(int fd);
|
||||||
|
|||||||
Reference in New Issue
Block a user