Skip to content

Commit

Permalink
unify benchmark client (#586)
Browse files Browse the repository at this point in the history
  • Loading branch information
qicosmos authored May 21, 2024
1 parent 02c1117 commit 7d957fe
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 89 deletions.
74 changes: 0 additions & 74 deletions include/cinatra/coro_http_client.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -515,7 +515,6 @@ class coro_http_client : public std::enable_shared_from_this<coro_http_client> {

#ifdef BENCHMARK_TEST
void set_bench_stop() { stop_bench_ = true; }
void set_read_fix() { read_fix_ = 1; }
#endif

async_simple::coro::Lazy<resp_data> async_patch(
Expand Down Expand Up @@ -558,73 +557,6 @@ class coro_http_client : public std::enable_shared_from_this<coro_http_client> {
std::string uri,
std::unordered_map<std::string, std::string> headers = {}) {
resp_data data{};
#ifdef BENCHMARK_TEST
if (!req_str_.empty()) {
if (has_closed()) {
data.net_err = std::make_error_code(std::errc::not_connected);
data.status = 404;
co_return data;
}

std::error_code ec{};
size_t size = 0;
if (std::tie(ec, size) = co_await async_write(asio::buffer(req_str_));
ec) {
data.net_err = ec;
data.status = 404;
close_socket(*socket_);
co_return data;
}

if (read_fix_ == 0) {
req_context<> ctx{};
bool is_keep_alive = true;
data = co_await handle_read(ec, size, is_keep_alive, std::move(ctx),
http_method::GET);
handle_result(data, ec, is_keep_alive);
if (ec) {
if (!stop_bench_)
CINATRA_LOG_ERROR << "do_bench_read error:" << ec.message();
data.net_err = ec;
data.status = 404;
}
else {
data.status = 200;
data.total = total_len_;
}

co_return data;
}

std::tie(ec, size) = co_await async_read(head_buf_, total_len_);

if (ec) {
if (!stop_bench_)
CINATRA_LOG_ERROR << "do_bench_read error:" << ec.message();
data.net_err = ec;
data.status = 404;
close_socket(*socket_);
co_return data;
}
else {
const char *data_ptr =
asio::buffer_cast<const char *>(head_buf_.data());
head_buf_.consume(total_len_);
// check status
if (data_ptr[9] > '3') {
data.status = 404;
co_return data;
}
}

head_buf_.consume(total_len_);
data.status = 200;
data.total = total_len_;

co_return data;
}
#endif

req_context<> ctx{};
data = co_await async_request(std::move(uri), http_method::GET,
std::move(ctx), std::move(headers));
Expand Down Expand Up @@ -892,7 +824,6 @@ class coro_http_client : public std::enable_shared_from_this<coro_http_client> {
}
#endif
#ifdef BENCHMARK_TEST
req_str_.clear();
total_len_ = 0;
#endif

Expand Down Expand Up @@ -1160,9 +1091,6 @@ class coro_http_client : public std::enable_shared_from_this<coro_http_client> {
vec.push_back(asio::buffer(ctx.content.data(), ctx.content.size()));
}

#ifdef BENCHMARK_TEST
req_str_ = req_head_str;
#endif
#ifdef CORO_HTTP_PRINT_REQ_HEAD
CINATRA_LOG_DEBUG << req_head_str;
#endif
Expand Down Expand Up @@ -2136,10 +2064,8 @@ class coro_http_client : public std::enable_shared_from_this<coro_http_client> {
#endif

#ifdef BENCHMARK_TEST
std::string req_str_;
bool stop_bench_ = false;
size_t total_len_ = 0;
int read_fix_ = 0;
#endif
};
} // namespace cinatra
26 changes: 11 additions & 15 deletions press_tool/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ press_config init_conf(const cmdline::parser& parser) {
std::cerr << "number of connections must be >= threads\n";
exit(1);
}
conf.read_fix = parser.get<int>("readfix");

std::string duration_str = parser.get<std::string>("duration");
if (duration_str.size() < 2) {
Expand Down Expand Up @@ -94,7 +93,7 @@ async_simple::coro::Lazy<void> create_clients(const press_config& conf,
for (auto& single_header : conf.add_headers)
client->add_header(single_header.first, single_header.second);
}
result = co_await client->async_get(conf.url);
result = co_await client->connect(conf.url);
if (result.status != 200) {
client->reset();
std::cout << "create client " << i + 1 << " failed, retry " << j + 1
Expand All @@ -111,23 +110,15 @@ async_simple::coro::Lazy<void> create_clients(const press_config& conf,
exit(1);
}

if (conf.read_fix > 0) {
client->set_read_fix();
}

thd_counter.conns.push_back(std::move(client));
}

std::cout << "create " << conf.connections << " connections"
<< " successfully";
if (conf.read_fix > 0) {
std::cout << ", will read fixed len response";
}
std::cout << "\n";
<< " successfully\n";
}

async_simple::coro::Lazy<void> press(thread_counter& counter,
const std::string& url,
const std::string& path,
std::atomic_bool& stop) {
size_t err_count = 0;
size_t conn_num = counter.conns.size();
Expand All @@ -141,7 +132,7 @@ async_simple::coro::Lazy<void> press(thread_counter& counter,
continue;
}

futures.push_back(conn->async_get(url));
futures.push_back(conn->async_get(path));
}

auto start = std::chrono::steady_clock::now();
Expand Down Expand Up @@ -203,12 +194,16 @@ int main(int argc, char* argv[]) {
" e.g. \"User-Agent: coro_http_press && x-frame-options: "
"SAMEORIGIN\"",
false, "");
parser.add<int>("readfix", 'r', "read fixed response", false, 0);

parser.parse_check(argc, argv);

press_config conf = init_conf(parser);

if (conf.connections <= 0) {
std::cout << "connection number is negative: " << conf.connections << "\n";
std::exit(1);
}

// create threads
std::vector<thread_counter> v;
std::vector<std::shared_ptr<asio::io_context::work>> works;
Expand All @@ -227,8 +222,9 @@ int main(int argc, char* argv[]) {
// create parallel request
std::vector<async_simple::coro::Lazy<void>> futures;
std::atomic_bool stop = false;
std::string path = conf.url.substr(conf.url.rfind('/'));
for (auto& counter : v) {
futures.push_back(press(counter, conf.url, stop));
futures.push_back(press(counter, path, stop));
}

// start timer
Expand Down

0 comments on commit 7d957fe

Please sign in to comment.