From fb0bfe9631afa7a376ef41ac73405df101deb1be Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matheus=20Catarino=20Fran=C3=A7a?= Date: Wed, 24 May 2023 15:24:23 -0300 Subject: [PATCH 1/2] wrk: cpp-beast sample added --- wrk/cpp/build.zig | 39 +++++++++++++++++++++++++++++ wrk/cpp/build.zig.zon | 10 ++++++++ wrk/cpp/hello.html | 1 + wrk/cpp/main.cpp | 58 +++++++++++++++++++++++++++++++++++++++++++ wrk/measure.sh | 7 ++++++ 5 files changed, 115 insertions(+) create mode 100644 wrk/cpp/build.zig create mode 100644 wrk/cpp/build.zig.zon create mode 100644 wrk/cpp/hello.html create mode 100644 wrk/cpp/main.cpp diff --git a/wrk/cpp/build.zig b/wrk/cpp/build.zig new file mode 100644 index 0000000..caa0656 --- /dev/null +++ b/wrk/cpp/build.zig @@ -0,0 +1,39 @@ +const std = @import("std"); + +pub fn build(b: *std.Build) void { + const target = b.standardTargetOptions(.{}); + + const optimize = b.standardOptimizeOption(.{}); + + const exe = b.addExecutable(.{ + .name = "cpp-beast", + .target = target, + .optimize = optimize, + }); + exe.addIncludePath(.{ .path = "." }); + exe.addCSourceFiles(&.{"main.cpp"}, &.{ + "-Wall", + "-Wextra", + "-Wshadow", + }); + const libasio_dep = b.dependency("beast", .{ + .target = target, + .optimize = optimize, + }); + const libasio = libasio_dep.artifact("beast"); + for (libasio.include_dirs.items) |include| { + exe.include_dirs.append(include) catch {}; + } + exe.linkLibrary(libasio); + exe.linkLibCpp(); + + b.installArtifact(exe); + const run_cmd = b.addRunArtifact(exe); + run_cmd.step.dependOn(b.getInstallStep()); + if (b.args) |args| { + run_cmd.addArgs(args); + } + + const run_step = b.step("run", "Run C++ Http Server"); + run_step.dependOn(&run_cmd.step); +} diff --git a/wrk/cpp/build.zig.zon b/wrk/cpp/build.zig.zon new file mode 100644 index 0000000..ff48e0d --- /dev/null +++ b/wrk/cpp/build.zig.zon @@ -0,0 +1,10 @@ +.{ + .name = "cpp-beast", + .version = "0.1.0", + .dependencies = .{ + .beast = .{ + .url = "https://github.com/kassane/beast/archive/df69ba4d48fbe874730f6a28e9528d9ef7a9547c.tar.gz", + .hash = "1220548f8727394522081ab48ed2f7111c20fa5f051ff287ec3c3f82340faa5d68c2", + }, + }, +} diff --git a/wrk/cpp/hello.html b/wrk/cpp/hello.html new file mode 100644 index 0000000..4be4d6b --- /dev/null +++ b/wrk/cpp/hello.html @@ -0,0 +1 @@ +Hello from C++! diff --git a/wrk/cpp/main.cpp b/wrk/cpp/main.cpp new file mode 100644 index 0000000..e6bd520 --- /dev/null +++ b/wrk/cpp/main.cpp @@ -0,0 +1,58 @@ +#include +#include +#include +#include + +namespace beast = boost::beast; +namespace http = beast::http; +namespace net = boost::asio; +using tcp = net::ip::tcp; + +std::string read_html_file(const std::string& file_path) { + std::ifstream file(file_path); + if (!file) { + return "File not found: " + file_path; + } + + std::string content((std::istreambuf_iterator(file)), std::istreambuf_iterator()); + return content; +} + +int main() { + try { + net::io_context io_context; + + // Create an endpoint to bind to + tcp::endpoint endpoint(tcp::v4(), 8070); + + // Create and bind the acceptor + tcp::acceptor acceptor(io_context, endpoint); + std::cout << "Server listening on port 8070..." << std::endl; + + while (true) { + // Wait for a client to connect + tcp::socket socket(io_context); + acceptor.accept(socket); + + // Read HTML content from a file (e.g., "index.html") + std::string html_content = read_html_file("hello.html"); + + // Construct an HTTP response with the HTML content + http::response response; + response.version(11); + response.result(http::status::ok); + response.reason("OK"); + response.set(http::field::server, "C++ Server"); + response.set(http::field::content_type, "text/html"); + response.body() = html_content; + response.prepare_payload(); + + // Send the response to the client + http::write(socket, response); + } + } catch (const std::exception& e) { + std::cerr << "Error: " << e.what() << std::endl; + } + + return 0; +} diff --git a/wrk/measure.sh b/wrk/measure.sh index 3e25aea..f3c06b4 100755 --- a/wrk/measure.sh +++ b/wrk/measure.sh @@ -65,6 +65,13 @@ if [ "$SUBJECT" = "csharp" ] ; then URL=http://127.0.0.1:5026 fi +if [ "$SUBJECT" = "cpp" ] ; then + cd wrk/cpp && zig build -Doptimize=ReleaseFast + ./zig-out/bin/cpp-beast 127.0.0.1 8070 . & + PID=$! + URL=http://127.0.0.1:8070 +fi + sleep 1 echo "========================================================================" echo " $SUBJECT" From e68435ea9b1b3cdcdb1d87c59e419d33697b7c36 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matheus=20Catarino=20Fran=C3=A7a?= Date: Mon, 21 Aug 2023 09:01:16 -0300 Subject: [PATCH 2/2] static 17-byte string --- wrk/cpp/hello.html | 2 +- wrk/cpp/main.cpp | 14 +++++++++++--- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/wrk/cpp/hello.html b/wrk/cpp/hello.html index 4be4d6b..205d9a7 100644 --- a/wrk/cpp/hello.html +++ b/wrk/cpp/hello.html @@ -1 +1 @@ -Hello from C++! +Hello from C++!! diff --git a/wrk/cpp/main.cpp b/wrk/cpp/main.cpp index e6bd520..3aa71bb 100644 --- a/wrk/cpp/main.cpp +++ b/wrk/cpp/main.cpp @@ -13,8 +13,8 @@ std::string read_html_file(const std::string& file_path) { if (!file) { return "File not found: " + file_path; } - std::string content((std::istreambuf_iterator(file)), std::istreambuf_iterator()); + file.close(); return content; } @@ -34,8 +34,14 @@ int main() { tcp::socket socket(io_context); acceptor.accept(socket); + // static 17-byte string + // Read HTML content from a file (e.g., "index.html") - std::string html_content = read_html_file("hello.html"); + // std::string html_content = read_html_file("hello.html"); + // or + std::string msg = "Hello from C++!!!"; + + // std::cout << "str len: " << (html_content.length() == msg.length()) << std::boolalpha << "\n"; // Construct an HTTP response with the HTML content http::response response; @@ -44,7 +50,9 @@ int main() { response.reason("OK"); response.set(http::field::server, "C++ Server"); response.set(http::field::content_type, "text/html"); - response.body() = html_content; + + // response.body() = html_content; + response.body() = msg; response.prepare_payload(); // Send the response to the client