|
| 1 | +#include <atomic> |
| 2 | +#include <chrono> |
| 3 | +#include <string_view> |
| 4 | +#include <thread> |
| 5 | + |
| 6 | +#include <MIME.hpp> |
| 7 | +#include <RestClient.hpp> |
| 8 | +#include <URI.hpp> |
| 9 | + |
| 10 | +#include "ClientCommon.hpp" |
| 11 | +#include "helpers.hpp" |
| 12 | + |
| 13 | +using namespace std::chrono_literals; |
| 14 | + |
| 15 | +std::string schema() { |
| 16 | + if (auto env = ::getenv("DISABLE_REST_HTTPS"); env != nullptr && std::string_view(env) == "1") { |
| 17 | + return "http"; |
| 18 | + } else { |
| 19 | + return "https"; |
| 20 | + } |
| 21 | +} |
| 22 | + |
| 23 | + |
| 24 | +int main() { |
| 25 | + constexpr auto kServerPort = 8080; |
| 26 | + constexpr auto kNClients = 80UZ; |
| 27 | + constexpr auto kNSubscriptions = 10UZ; |
| 28 | + constexpr auto kNUpdates = 5000UZ; |
| 29 | + constexpr auto kIntervalMs = 40UZ; |
| 30 | + constexpr auto kPayloadSize = 4096UZ; |
| 31 | + |
| 32 | + std::array<std::unique_ptr<opencmw::client::RestClient>, kNClients> clients; |
| 33 | + for (std::size_t i = 0; i < clients.size(); i++) { |
| 34 | + clients[i] = std::make_unique<opencmw::client::RestClient>(opencmw::client::DefaultContentTypeHeader(opencmw::MIME::BINARY), opencmw::client::VerifyServerCertificates(false)); |
| 35 | + } |
| 36 | + std::atomic<std::size_t> responseCount = 0; |
| 37 | + |
| 38 | + const auto start = std::chrono::system_clock::now(); |
| 39 | + |
| 40 | + for (std::size_t i = 0; i < kNClients; i++) { |
| 41 | + for (std::size_t j = 0; j < kNSubscriptions; j++) { |
| 42 | + opencmw::client::Command cmd; |
| 43 | + cmd.command = opencmw::mdp::Command::Subscribe; |
| 44 | + cmd.serviceName = "/loadTest"; |
| 45 | + cmd.topic = opencmw::URI<>(fmt::format("{}://localhost:{}/loadTest?initialDelayMs=1000&topic={}&intervalMs={}&payloadSize={}&nUpdates={}", schema(), kServerPort, /*i,*/ j, kIntervalMs, kPayloadSize, kNUpdates)); |
| 46 | + cmd.callback = [&responseCount](const auto &msg) { |
| 47 | + responseCount++; |
| 48 | + }; |
| 49 | + clients[i]->request(std::move(cmd)); |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + constexpr auto expectedResponses = kNClients * kNSubscriptions * kNUpdates; |
| 54 | + |
| 55 | + std::uint64_t counter = 0; |
| 56 | + while (responseCount < expectedResponses) { |
| 57 | + counter += 50; |
| 58 | + std::this_thread::sleep_for(std::chrono::milliseconds(50)); |
| 59 | + if (counter % 20 == 0) { |
| 60 | + fmt::println("Received {} of {} responses", responseCount.load(), expectedResponses); |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + const auto end = std::chrono::system_clock::now(); |
| 65 | + const auto elapsed = std::chrono::duration_cast<std::chrono::milliseconds>(end - start).count(); |
| 66 | + fmt::println("Elapsed time: {} ms", elapsed); |
| 67 | + return 0; |
| 68 | +} |
0 commit comments