-
Notifications
You must be signed in to change notification settings - Fork 9
feat(http2): add a simple benchmark of HTTP2 using libevents and nghttp2 #20
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| h2_bench_client | ||
| h2_echo_server | ||
|
|
||
| CMakeFiles | ||
| CMakeCache.txt | ||
| Makefile | ||
|
|
||
| build | ||
|
|
||
| *.log |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| cmake_minimum_required(VERSION 3.15) | ||
| project(http2_benchmark) | ||
|
|
||
| set(CMAKE_CXX_STANDARD 17) | ||
| set(CMAKE_CXX_STANDARD_REQUIRED ON) | ||
|
|
||
| include(FetchContent) | ||
|
|
||
| set(FETCHCONTENT_QUIET OFF) | ||
|
|
||
| # Abseil | ||
| set(ABSL_ENABLE_INSTALL OFF CACHE BOOL "" FORCE) | ||
| set(ABSL_PROPAGATE_CXX_STD ON) | ||
|
|
||
| FetchContent_Declare( | ||
| abseil | ||
| GIT_REPOSITORY https://github.com/abseil/abseil-cpp.git | ||
| GIT_TAG 20260107.1 | ||
| ) | ||
|
|
||
| FetchContent_MakeAvailable(abseil) | ||
|
|
||
| find_package(PkgConfig REQUIRED) | ||
| pkg_check_modules(LIBEVENT REQUIRED libevent) | ||
| pkg_check_modules(LIBNGHTTP2 REQUIRED libnghttp2) | ||
|
|
||
| include_directories( | ||
| ${LIBEVENT_INCLUDE_DIRS} | ||
| ${LIBNGHTTP2_INCLUDE_DIRS} | ||
| ${CMAKE_CURRENT_BINARY_DIR} | ||
| ) | ||
|
|
||
| link_directories( | ||
| ${LIBEVENT_LIBRARY_DIRS} | ||
| ${LIBNGHTTP2_LIBRARY_DIRS} | ||
| ) | ||
|
|
||
| add_executable(h2_echo_server server.cc) | ||
| target_link_libraries(h2_echo_server ${LIBEVENT_LIBRARIES} ${LIBNGHTTP2_LIBRARIES} absl::log absl::log_initialize absl::flags absl::flags_parse "$<LINK_LIBRARY:WHOLE_ARCHIVE,absl::log_flags>") | ||
|
|
||
| add_executable(h2_bench_client client.cc) | ||
| target_link_libraries(h2_bench_client ${LIBEVENT_LIBRARIES} ${LIBNGHTTP2_LIBRARIES} absl::log absl::log_initialize absl::flags absl::flags_parse "$<LINK_LIBRARY:WHOLE_ARCHIVE,absl::log_flags>") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| import subprocess | ||
| import time | ||
| import os | ||
|
|
||
| TOTAL_DATA_BYTES = 128 * 1024 * 1024 | ||
|
|
||
| PAYLOAD_SIZES = { | ||
| "1KiB": 1024, | ||
| "128KiB": 131072, | ||
| "1MiB": 1048576, | ||
| } | ||
|
|
||
| def run_benchmark(): | ||
| with open("results.txt", "w") as f: | ||
| for name, size in PAYLOAD_SIZES.items(): | ||
| num_requests = TOTAL_DATA_BYTES // size | ||
| f.write(f"=== Benchmark for {name} payload ({size} bytes), {num_requests} requests ===\n\n") | ||
|
|
||
| f.write("--- Raw HTTP/2 ---\n") | ||
| print(f"Running Raw HTTP/2 bench for {name}...") | ||
| server_process = subprocess.Popen(["./build/h2_echo_server"]) | ||
| time.sleep(1) # wait for server to start | ||
| try: | ||
| client_output = subprocess.check_output([ | ||
| "./build/h2_bench_client", | ||
| f"--payload_size={size}", | ||
| f"--num_requests={num_requests}", | ||
| "--stderrthreshold=0", | ||
| ], stderr=subprocess.STDOUT, text=True) | ||
| f.write(client_output) | ||
| except subprocess.CalledProcessError as e: | ||
| f.write(f"Error running raw HTTP/2 client:\n{e.output}\n") | ||
| except Exception as e: | ||
| f.write(f"Failed to start/run HTTP/2 client: {e}\n") | ||
| finally: | ||
| server_process.terminate() | ||
| server_process.wait() | ||
|
|
||
| f.write("\n\n") | ||
|
|
||
| if __name__ == "__main__": | ||
| if not os.path.isdir("build"): | ||
| print("Error: 'build' directory not found. Please compile the project first.") | ||
| exit(1) | ||
|
|
||
| print("Starting benchmarks...") | ||
| run_benchmark() | ||
| print("Benchmark complete! Results saved to results.txt.") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,293 @@ | ||
| #include <arpa/inet.h> | ||
| #include <event2/buffer.h> | ||
| #include <event2/bufferevent.h> | ||
| #include <event2/event.h> | ||
| #include <netinet/in.h> | ||
| #include <nghttp2/nghttp2.h> | ||
| #include <stdio.h> | ||
| #include <stdlib.h> | ||
| #include <string.h> | ||
| #include <sys/socket.h> | ||
| #include <sys/time.h> | ||
| #include <time.h> | ||
| #include <unistd.h> | ||
|
|
||
| #include <algorithm> | ||
| #include <chrono> | ||
| #include <cmath> | ||
| #include <map> | ||
| #include <numeric> | ||
| #include <string> | ||
| #include <vector> | ||
|
|
||
| #include "absl/flags/flag.h" | ||
| #include "absl/flags/parse.h" | ||
| #include "absl/log/flags.h" | ||
| #include "absl/log/initialize.h" | ||
| #include "absl/log/log.h" | ||
|
|
||
| ABSL_FLAG(std::string, host, "127.0.0.1", "Server host to connect to"); | ||
| ABSL_FLAG(int, port, 8080, "Server port to connect to"); | ||
| ABSL_FLAG(int, num_requests, 5000, "Total number of requests to send"); | ||
| ABSL_FLAG(int, payload_size, 1024 * 1024, "Size of the request payload in bytes"); | ||
| ABSL_FLAG(int, report_interval, 1000, "Frequency of progress reports"); | ||
| ABSL_FLAG(int, concurrent_requests, 100, "Number of concurrent requests"); | ||
|
|
||
| #define MAKE_NV(name, value) \ | ||
| { \ | ||
| (uint8_t*)(name), (uint8_t*)(value), strlen(name), strlen(value), \ | ||
| NGHTTP2_NV_FLAG_NONE} | ||
|
|
||
| std::string request_payload_data; | ||
|
|
||
| struct ClientSession { | ||
| struct bufferevent* bev; | ||
| nghttp2_session* session; | ||
| struct event_base* evbase; | ||
| int requests_completed; | ||
| int requests_in_flight; | ||
| int total_requests_submitted; | ||
| struct timeval start_time; | ||
| std::map<int32_t, std::chrono::steady_clock::time_point> request_start_times; | ||
| std::vector<double> latencies; | ||
| }; | ||
|
|
||
| static void submit_request(struct ClientSession* client); | ||
|
|
||
| static ssize_t send_callback(nghttp2_session* session, const uint8_t* data, size_t length, int flags, void* user_data) { | ||
| struct ClientSession* client = (struct ClientSession*)user_data; | ||
| struct bufferevent* bev = client->bev; | ||
| struct evbuffer* output = bufferevent_get_output(bev); | ||
| evbuffer_add(output, data, length); | ||
| return (ssize_t)length; | ||
| } | ||
|
|
||
| static int on_frame_recv_callback(nghttp2_session* session, const nghttp2_frame* frame, void* user_data) { | ||
| struct ClientSession* client = (struct ClientSession*)user_data; | ||
|
|
||
| VLOG(2) << "Client received frame type " << static_cast<int>(frame->hd.type) << " on stream " << frame->hd.stream_id; | ||
|
|
||
| if (frame->hd.type == NGHTTP2_DATA && (frame->hd.flags & NGHTTP2_FLAG_END_STREAM)) { | ||
| client->requests_completed++; | ||
| client->requests_in_flight--; | ||
|
|
||
| int report_interval = absl::GetFlag(FLAGS_report_interval); | ||
| if (report_interval > 0 && client->requests_completed % report_interval == 0) { | ||
| struct timeval now; | ||
| gettimeofday(&now, NULL); | ||
| double elapsed = (now.tv_sec - client->start_time.tv_sec) + | ||
| (now.tv_usec - client->start_time.tv_usec) / 1000000.0; | ||
| LOG(INFO) << "Completed " << client->requests_completed << " requests. RPS: " | ||
| << client->requests_completed / elapsed; | ||
| } | ||
|
|
||
| auto it = client->request_start_times.find(frame->hd.stream_id); | ||
| if (it != client->request_start_times.end()) { | ||
| auto end_time = std::chrono::steady_clock::now(); | ||
| double duration = std::chrono::duration<double, std::milli>(end_time - it->second).count(); | ||
| client->latencies.push_back(duration); | ||
| client->request_start_times.erase(it); | ||
| } | ||
|
|
||
| if (client->requests_completed < absl::GetFlag(FLAGS_num_requests)) { // Limit total requests for bench | ||
| submit_request(client); | ||
| } else if (client->requests_in_flight == 0) { | ||
| event_base_loopexit(client->evbase, NULL); | ||
| } | ||
| } else if (frame->hd.type == NGHTTP2_HEADERS && (frame->hd.flags & NGHTTP2_FLAG_END_STREAM)) { | ||
| // If server sent end stream on headers (e.g., error without body) | ||
| LOG(INFO) << "Client received END_STREAM on headers"; | ||
| } | ||
| return 0; | ||
| } | ||
|
|
||
| static ssize_t data_provider_callback(nghttp2_session* session, int32_t stream_id, uint8_t* buf, size_t length, uint32_t* data_flags, nghttp2_data_source* source, void* user_data) { | ||
| int* bytes_sent = (int*)source->ptr; | ||
| size_t payload_len = request_payload_data.length(); | ||
|
|
||
| if (*bytes_sent >= payload_len) { | ||
| *data_flags |= NGHTTP2_DATA_FLAG_EOF; | ||
| return 0; | ||
| } | ||
|
|
||
| size_t remaining = payload_len - *bytes_sent; | ||
| size_t send_len = remaining < length ? remaining : length; | ||
| memcpy(buf, request_payload_data.data() + *bytes_sent, send_len); | ||
|
|
||
| *bytes_sent += send_len; | ||
|
|
||
| if (*bytes_sent >= payload_len) { | ||
| *data_flags |= NGHTTP2_DATA_FLAG_EOF; | ||
| } | ||
|
|
||
| return send_len; | ||
| } | ||
|
|
||
| static int on_stream_close_callback(nghttp2_session* session, int32_t stream_id, uint32_t error_code, void* user_data) { | ||
| int* bytes_sent = (int*)nghttp2_session_get_stream_user_data(session, stream_id); | ||
| if (bytes_sent) { | ||
| delete bytes_sent; | ||
| } | ||
| return 0; | ||
| } | ||
|
|
||
| static void submit_request(struct ClientSession* client) { | ||
| const nghttp2_nv hdrs[] = { | ||
| MAKE_NV(":method", "POST"), | ||
| MAKE_NV(":path", "/"), | ||
| MAKE_NV(":scheme", "http"), | ||
| MAKE_NV(":authority", "127.0.0.1:8080"), | ||
| }; | ||
|
|
||
| int* bytes_sent = new int; | ||
| *bytes_sent = 0; | ||
|
|
||
| nghttp2_data_provider data_prd; | ||
| data_prd.source.ptr = bytes_sent; | ||
| data_prd.read_callback = data_provider_callback; | ||
|
|
||
| int32_t stream_id = nghttp2_submit_request(client->session, NULL, hdrs, 4, &data_prd, bytes_sent); | ||
| if (stream_id < 0) { | ||
| LOG(ERROR) << "nghttp2_submit_request error"; | ||
| delete bytes_sent; | ||
| } else { | ||
| client->requests_in_flight++; | ||
| client->total_requests_submitted++; | ||
| client->request_start_times[stream_id] = std::chrono::steady_clock::now(); | ||
| } | ||
| nghttp2_session_send(client->session); | ||
| } | ||
|
|
||
| static void setup_nghttp2_callbacks(nghttp2_session_callbacks* callbacks) { | ||
| nghttp2_session_callbacks_set_send_callback(callbacks, send_callback); | ||
| nghttp2_session_callbacks_set_on_frame_recv_callback(callbacks, on_frame_recv_callback); | ||
| nghttp2_session_callbacks_set_on_stream_close_callback(callbacks, on_stream_close_callback); | ||
| } | ||
|
|
||
| static void readcb(struct bufferevent* bev, void* ptr) { | ||
| struct ClientSession* client = (struct ClientSession*)ptr; | ||
|
|
||
| struct evbuffer* input = bufferevent_get_input(bev); | ||
| size_t datalen = evbuffer_get_length(input); | ||
| VLOG(2) << "Client readcb: got " << datalen << " bytes"; | ||
|
|
||
| if (datalen == 0) return; | ||
|
|
||
| unsigned char* data = evbuffer_pullup(input, -1); | ||
| ssize_t readlen = nghttp2_session_mem_recv(client->session, data, datalen); | ||
| if (readlen < 0) { | ||
| LOG(ERROR) << "Client: nghttp2_session_mem_recv error: " << nghttp2_strerror(static_cast<int>(readlen)); | ||
| return; | ||
| } | ||
|
|
||
| VLOG(2) << "Client readcb: consumed " << readlen << " bytes"; | ||
|
|
||
| evbuffer_drain(input, readlen); | ||
| nghttp2_session_send(client->session); | ||
| } | ||
|
|
||
| static void eventcb(struct bufferevent* bev, short events, void* ptr) { | ||
| struct ClientSession* client = (struct ClientSession*)ptr; | ||
|
|
||
| if (events & BEV_EVENT_CONNECTED) { | ||
| LOG(INFO) << "Connected to server."; | ||
|
|
||
| nghttp2_session_callbacks* callbacks; | ||
| nghttp2_session_callbacks_new(&callbacks); | ||
| setup_nghttp2_callbacks(callbacks); | ||
|
|
||
| nghttp2_session_client_new(&client->session, callbacks, client); | ||
|
|
||
| nghttp2_session_callbacks_del(callbacks); | ||
|
|
||
| // Send connection preface and initial settings | ||
| nghttp2_settings_entry iv[2] = { | ||
| {NGHTTP2_SETTINGS_MAX_CONCURRENT_STREAMS, static_cast<uint32_t>(absl::GetFlag(FLAGS_concurrent_requests))}, | ||
| {NGHTTP2_SETTINGS_INITIAL_WINDOW_SIZE, 128 * 1024 * 1024}}; | ||
|
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. More study on window size needed to conduct the right benchmark and comparison with gRPC one (chttp2 and BDP behavior) |
||
| nghttp2_submit_settings(client->session, NGHTTP2_FLAG_NONE, iv, 2); | ||
| nghttp2_submit_window_update(client->session, NGHTTP2_FLAG_NONE, 0, 128 * 1024 * 1024); | ||
|
|
||
| gettimeofday(&client->start_time, NULL); | ||
|
|
||
| for (int i = 0; i < absl::GetFlag(FLAGS_concurrent_requests); i++) { | ||
| submit_request(client); | ||
| } | ||
|
|
||
| return; | ||
| } | ||
|
|
||
| if (events & (BEV_EVENT_EOF | BEV_EVENT_ERROR)) { | ||
| LOG(INFO) << "Connection closed or error."; | ||
| event_base_loopexit(client->evbase, NULL); | ||
| } | ||
| } | ||
|
|
||
| int main(int argc, char** argv) { | ||
| absl::ParseCommandLine(argc, argv); | ||
|
|
||
| absl::InitializeLog(); | ||
|
|
||
| request_payload_data = std::string(absl::GetFlag(FLAGS_payload_size), 'A'); | ||
|
|
||
| LOG(INFO) << "Starting HTTP/2 benchmark client..."; | ||
|
|
||
| ClientSession client = {}; | ||
| client.requests_completed = 0; | ||
| client.requests_in_flight = 0; | ||
| client.total_requests_submitted = 0; | ||
|
|
||
| struct event_base* base = event_base_new(); | ||
| client.evbase = base; | ||
|
|
||
| struct sockaddr_in sin; | ||
| memset(&sin, 0, sizeof(sin)); | ||
|
|
||
| const char* host = absl::GetFlag(FLAGS_host).c_str(); | ||
| int port = absl::GetFlag(FLAGS_port); | ||
|
|
||
| sin.sin_family = AF_INET; | ||
| sin.sin_addr.s_addr = inet_addr(host); | ||
| sin.sin_port = htons(port); | ||
|
|
||
| struct bufferevent* bev = bufferevent_socket_new(base, -1, BEV_OPT_CLOSE_ON_FREE); | ||
| client.bev = bev; | ||
|
|
||
| bufferevent_setcb(bev, readcb, NULL, eventcb, &client); | ||
| bufferevent_enable(bev, EV_READ | EV_WRITE); | ||
|
|
||
| if (bufferevent_socket_connect(bev, (struct sockaddr*)&sin, sizeof(sin)) < 0) { | ||
| LOG(ERROR) << "Connect failed"; | ||
| return 1; | ||
| } | ||
|
|
||
| event_base_dispatch(base); | ||
|
|
||
| struct timeval end_time; | ||
| gettimeofday(&end_time, NULL); | ||
| double total_elapsed = (end_time.tv_sec - client.start_time.tv_sec) + | ||
| (end_time.tv_usec - client.start_time.tv_usec) / 1000000.0; | ||
|
|
||
| LOG(INFO) << "Finished benchmark."; | ||
| LOG(INFO) << "Total RPS: " << client.requests_completed / total_elapsed; | ||
|
|
||
| if (!client.latencies.empty()) { | ||
| double sum = std::accumulate(client.latencies.begin(), client.latencies.end(), 0.0); | ||
| double avg = sum / client.latencies.size(); | ||
|
|
||
| double sq_sum = std::inner_product(client.latencies.begin(), client.latencies.end(), client.latencies.begin(), 0.0); | ||
| double stdev = std::sqrt(sq_sum / client.latencies.size() - avg * avg); | ||
|
|
||
| LOG(INFO) << "Latency Stats (ms):"; | ||
| LOG(INFO) << " Average: " << avg; | ||
| LOG(INFO) << " StdDev: " << stdev; | ||
| LOG(INFO) << " Min: " << *std::min_element(client.latencies.begin(), client.latencies.end()); | ||
| LOG(INFO) << " Max: " << *std::max_element(client.latencies.begin(), client.latencies.end()); | ||
| LOG(INFO) << " Samples: " << client.latencies.size(); | ||
| } | ||
|
|
||
| if (client.session) nghttp2_session_del(client.session); | ||
| bufferevent_free(bev); | ||
| event_base_free(base); | ||
|
|
||
| return 0; | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll change this to
chronolater