|
| 1 | +// SPDX-License-Identifier: MIT |
| 2 | +// TechEmpower Framework Benchmarks - Aeronet Implementation |
| 3 | +// Implements Test 1 (JSON) and Test 6 (Plaintext) endpoints |
| 4 | + |
| 5 | +#include <aeronet/aeronet.hpp> |
| 6 | +#include <aeronet/json-serializer.hpp> |
| 7 | +#include <charconv> |
| 8 | +#include <cstdlib> |
| 9 | +#include <cstring> |
| 10 | +#include <glaze/glaze.hpp> |
| 11 | +#include <iostream> |
| 12 | +#include <optional> |
| 13 | +#include <system_error> |
| 14 | + |
| 15 | +// Test 1: JSON message structure |
| 16 | +struct MessageResponse { |
| 17 | + std::string_view message; |
| 18 | +}; |
| 19 | + |
| 20 | +// Glaze metadata for JSON serialization |
| 21 | +template <> |
| 22 | +struct glz::meta<MessageResponse> { |
| 23 | + using T = MessageResponse; |
| 24 | + static constexpr auto value = glz::object("message", &T::message); |
| 25 | +}; |
| 26 | + |
| 27 | +int main(int argc, char* argv[]) { |
| 28 | + // Enable signal handler for graceful shutdown on Ctrl+C |
| 29 | + aeronet::SignalHandler::Enable(); |
| 30 | + |
| 31 | + try { |
| 32 | + using namespace aeronet; |
| 33 | + |
| 34 | + auto parseEnvUInt = [](const char* envVar) -> std::optional<uint32_t> { |
| 35 | + const char* value = std::getenv(envVar); |
| 36 | + if (value == nullptr || value[0] == '\0') { |
| 37 | + return std::nullopt; |
| 38 | + } |
| 39 | + uint32_t parsed = 0; |
| 40 | + const auto [ptr, errc] = std::from_chars(value, value + std::strlen(value), parsed); |
| 41 | + if (errc != std::errc{} || ptr != value + std::strlen(value)) { |
| 42 | + return std::nullopt; |
| 43 | + } |
| 44 | + return parsed; |
| 45 | + }; |
| 46 | + |
| 47 | + uint16_t port = 8080; |
| 48 | + if (argc > 1) { |
| 49 | + const auto [ptr, errc] = std::from_chars(argv[1], argv[1] + std::strlen(argv[1]), port); |
| 50 | + if (errc != std::errc{} || ptr != argv[1] + std::strlen(argv[1])) { |
| 51 | + std::cerr << "Invalid port number: " << argv[1] << "\n"; |
| 52 | + return EXIT_FAILURE; |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + HttpServerConfig config; |
| 57 | + config.port = port; |
| 58 | + if (const auto threads = parseEnvUInt("AERONET_THREADS"); threads.has_value()) { |
| 59 | + config.nbThreads = *threads; |
| 60 | + } else if (const auto threads = parseEnvUInt("THREADS"); threads.has_value()) { |
| 61 | + config.nbThreads = *threads; |
| 62 | + } |
| 63 | + |
| 64 | + Router router; |
| 65 | + |
| 66 | + // Test 6: Plaintext endpoint |
| 67 | + // Returns a simple "Hello, World!" text response |
| 68 | + router.setPath(http::Method::GET, "/plaintext", [](const HttpRequest& req) { |
| 69 | + return req.makeResponse("Hello, World!", "text/plain; charset=UTF-8"); |
| 70 | + }); |
| 71 | + |
| 72 | + // Test 1: JSON endpoint |
| 73 | + router.setPath(http::Method::GET, "/json", [](const HttpRequest& req) { |
| 74 | + MessageResponse msg{"Hello, World!"}; |
| 75 | + return req.makeResponse(aeronet::SerializeToJson(msg), "application/json"); |
| 76 | + }); |
| 77 | + |
| 78 | + // Health check endpoint (optional, useful for Docker) |
| 79 | + router.setPath(http::Method::GET, "/health", |
| 80 | + [](const HttpRequest& req) { return req.makeResponse("OK", "text/plain"); }); |
| 81 | + |
| 82 | + // Start the server |
| 83 | + std::cout << "Starting TechEmpower benchmark server on port " << port << '\n'; |
| 84 | + std::cout << " - JSON test (Test 1): GET /json\n"; |
| 85 | + std::cout << " - Plaintext test (Test 6): GET /plaintext\n"; |
| 86 | + std::cout << " - Health check: GET /health\n"; |
| 87 | + |
| 88 | + HttpServer server(config, std::move(router)); |
| 89 | + server.run(); // blocking run, until Ctrl+C |
| 90 | + |
| 91 | + } catch (const std::exception& e) { |
| 92 | + std::cerr << "Error: " << e.what() << '\n'; |
| 93 | + return 1; |
| 94 | + } |
| 95 | + |
| 96 | + return 0; |
| 97 | +} |
0 commit comments