diff --git a/include/cinatra/coro_http_client.hpp b/include/cinatra/coro_http_client.hpp index 3b92efa4..ce76a357 100644 --- a/include/cinatra/coro_http_client.hpp +++ b/include/cinatra/coro_http_client.hpp @@ -515,7 +515,6 @@ class coro_http_client : public std::enable_shared_from_this { #ifdef BENCHMARK_TEST void set_bench_stop() { stop_bench_ = true; } - void set_read_fix() { read_fix_ = 1; } #endif async_simple::coro::Lazy async_patch( @@ -558,73 +557,6 @@ class coro_http_client : public std::enable_shared_from_this { std::string uri, std::unordered_map 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(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)); @@ -892,7 +824,6 @@ class coro_http_client : public std::enable_shared_from_this { } #endif #ifdef BENCHMARK_TEST - req_str_.clear(); total_len_ = 0; #endif @@ -1160,9 +1091,6 @@ class coro_http_client : public std::enable_shared_from_this { 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 @@ -2136,10 +2064,8 @@ class coro_http_client : public std::enable_shared_from_this { #endif #ifdef BENCHMARK_TEST - std::string req_str_; bool stop_bench_ = false; size_t total_len_ = 0; - int read_fix_ = 0; #endif }; } // namespace cinatra diff --git a/press_tool/main.cpp b/press_tool/main.cpp index 2dc7cfb7..feddb378 100644 --- a/press_tool/main.cpp +++ b/press_tool/main.cpp @@ -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("readfix"); std::string duration_str = parser.get("duration"); if (duration_str.size() < 2) { @@ -94,7 +93,7 @@ async_simple::coro::Lazy 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 @@ -111,23 +110,15 @@ async_simple::coro::Lazy 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 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(); @@ -141,7 +132,7 @@ async_simple::coro::Lazy 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(); @@ -203,12 +194,16 @@ int main(int argc, char* argv[]) { " e.g. \"User-Agent: coro_http_press && x-frame-options: " "SAMEORIGIN\"", false, ""); - parser.add("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 v; std::vector> works; @@ -227,8 +222,9 @@ int main(int argc, char* argv[]) { // create parallel request std::vector> 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