| 1 | // Copyright (C) 2024 The Qt Company Ltd. |
| 2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only |
| 3 | |
| 4 | #include <proto/bench_client.grpc.qpb.h> |
| 5 | #include <qrpcbench_common.h> |
| 6 | |
| 7 | #include <QtGrpc/qgrpcchanneloptions.h> |
| 8 | #include <QtGrpc/qgrpchttp2channel.h> |
| 9 | |
| 10 | #include <QtNetwork/qsslcertificate.h> |
| 11 | #include <QtNetwork/qsslconfiguration.h> |
| 12 | |
| 13 | #include <QtCore/qcoreapplication.h> |
| 14 | #include <QtCore/qeventloop.h> |
| 15 | #include <QtCore/qobject.h> |
| 16 | #include <QtCore/qtimer.h> |
| 17 | |
| 18 | using namespace std::chrono_literals; |
| 19 | using namespace Qt::Literals::StringLiterals; |
| 20 | |
| 21 | class QtGrpcClientBenchmark : public QObject |
| 22 | { |
| 23 | Q_OBJECT |
| 24 | public: |
| 25 | explicit QtGrpcClientBenchmark(const std::string &transport, quint64 calls, |
| 26 | qsizetype payload = 0) |
| 27 | : mCalls(calls) |
| 28 | { |
| 29 | if (payload > 0) |
| 30 | sData = QByteArray(payload, 'x'); |
| 31 | |
| 32 | QUrl uri; |
| 33 | QGrpcChannelOptions opts; |
| 34 | const auto address = QString::fromStdString(getTransportAddress(transport)); |
| 35 | if (transport == "https") { |
| 36 | uri = QString("https://") + address; |
| 37 | QSslCertificate crt(QByteArray(SslRootKey.data(), SslRootKey.size())); |
| 38 | QSslConfiguration sslConfig; |
| 39 | sslConfig.setProtocol(QSsl::TlsV1_2OrLater); |
| 40 | sslConfig.addCaCertificate(crt); |
| 41 | sslConfig.setAllowedNextProtocols({ "h2" }); |
| 42 | opts.setSslConfiguration(sslConfig); |
| 43 | } else if (transport == "http") { |
| 44 | uri = QString("http://") + address; |
| 45 | } else { |
| 46 | uri = address; |
| 47 | } |
| 48 | |
| 49 | mClient.attachChannel(std::make_shared<QGrpcHttp2Channel>(std::move(uri), opts)); |
| 50 | } |
| 51 | ~QtGrpcClientBenchmark() override = default; |
| 52 | |
| 53 | QtGrpcClientBenchmark(const QtGrpcClientBenchmark &) = delete; |
| 54 | QtGrpcClientBenchmark &operator=(const QtGrpcClientBenchmark &) = delete; |
| 55 | |
| 56 | QtGrpcClientBenchmark(QtGrpcClientBenchmark &&) = delete; |
| 57 | QtGrpcClientBenchmark &operator=(QtGrpcClientBenchmark &&) = delete; |
| 58 | |
| 59 | void unaryCall(); |
| 60 | void serverStreaming(); |
| 61 | void clientStreaming(); |
| 62 | void bidiStreaming(); |
| 63 | |
| 64 | private: |
| 65 | void unaryCallHelper(qt::bench::UnaryCallRequest &request, BenchmarkData &benchData); |
| 66 | |
| 67 | private: |
| 68 | qt::bench::BenchmarkService::Client mClient; |
| 69 | QEventLoop mLoop; |
| 70 | QElapsedTimer mTimer; |
| 71 | int64_t mCalls; |
| 72 | |
| 73 | inline static QByteArray sData; |
| 74 | }; |
| 75 | |
| 76 | void QtGrpcClientBenchmark::unaryCall() |
| 77 | { |
| 78 | qt::bench::UnaryCallRequest request; |
| 79 | BenchmarkData benchData(mCalls); |
| 80 | benchData.callCount = -50; // warmup |
| 81 | unaryCallHelper(request, benchData); |
| 82 | mLoop.exec(); |
| 83 | } |
| 84 | |
| 85 | // recursively enqueue a message after the previous message finished |
| 86 | void QtGrpcClientBenchmark::unaryCallHelper(qt::bench::UnaryCallRequest &request, |
| 87 | BenchmarkData &benchData) |
| 88 | { |
| 89 | request.setTimestamp(getTimestamp()); |
| 90 | if (!sData.isEmpty() && benchData.callCount >= 0) { |
| 91 | request.setPayload(sData); |
| 92 | benchData.sendBytes += sData.size(); |
| 93 | } |
| 94 | auto reply = mClient.UnaryCall(request); |
| 95 | auto *replyPtr = reply.get(); |
| 96 | |
| 97 | QObject::connect( |
| 98 | replyPtr, &QGrpcCallReply::finished, &mClient, |
| 99 | [reply = std::move(reply), this, &request, &benchData](const QGrpcStatus &status) { |
| 100 | if (benchData.callCount == 0) |
| 101 | mTimer.start(); |
| 102 | if (status.isOk()) { |
| 103 | ++benchData.callCount; |
| 104 | if (benchData.callCount >= 0) { |
| 105 | const auto response = reply->read<qt::bench::UnaryCallResponse>(); |
| 106 | if (response->hasPayload()) |
| 107 | benchData.receivedBytes += static_cast<quint64>(response->payload().size()); |
| 108 | benchData.requestLatenciesNanos.push_back(response->requestLatencyNanos()); |
| 109 | benchData.responseLatenciesNanos |
| 110 | .push_back(calculateLatencyNanosNow(response->timestamp())); |
| 111 | } |
| 112 | if (benchData.callCount < mCalls) { |
| 113 | unaryCallHelper(request, benchData); |
| 114 | } else { |
| 115 | benchData.elapsedNanos = mTimer.nsecsElapsed(); |
| 116 | Client::printBenchmarkResult("UnaryCall", benchData); |
| 117 | mLoop.quit(); |
| 118 | } |
| 119 | } else { |
| 120 | qDebug() << "FAILED: " << status; |
| 121 | mLoop.quit(); |
| 122 | } |
| 123 | }, |
| 124 | Qt::SingleShotConnection); |
| 125 | } |
| 126 | |
| 127 | void QtGrpcClientBenchmark::serverStreaming() |
| 128 | { |
| 129 | BenchmarkData benchData(mCalls); |
| 130 | |
| 131 | qt::bench::ServerStreamingRequest request; |
| 132 | if (!sData.isEmpty()) { |
| 133 | request.setPayload(sData); |
| 134 | benchData.sendBytes += sData.size(); |
| 135 | } |
| 136 | request.setPing(mCalls); |
| 137 | auto stream = mClient.ServerStreaming(request); |
| 138 | auto *streamPtr = stream.get(); |
| 139 | |
| 140 | QObject::connect(streamPtr, &QGrpcServerStream::messageReceived, this, |
| 141 | [this, stream = streamPtr, &benchData]() { |
| 142 | if (benchData.callCount == 0) |
| 143 | mTimer.start(); |
| 144 | const auto response = stream->read<qt::bench::ServerStreamingResponse>(); |
| 145 | if (response->hasPayload()) |
| 146 | benchData |
| 147 | .receivedBytes += static_cast<quint64>(response->payload().size()); |
| 148 | ++benchData.callCount; |
| 149 | }); |
| 150 | |
| 151 | QObject::connect(streamPtr, &QGrpcServerStream::finished, this, |
| 152 | [this, &benchData](const QGrpcStatus &status) { |
| 153 | if (status.isOk()) { |
| 154 | benchData.elapsedNanos = mTimer.nsecsElapsed(); |
| 155 | Client::printBenchmarkResult("ServerStreaming", benchData); |
| 156 | } else { |
| 157 | qDebug() << "FAILED: " << status; |
| 158 | } |
| 159 | mLoop.quit(); |
| 160 | }); |
| 161 | mLoop.exec(); |
| 162 | } |
| 163 | |
| 164 | void QtGrpcClientBenchmark::clientStreaming() |
| 165 | { |
| 166 | BenchmarkData benchData(mCalls); |
| 167 | |
| 168 | qt::bench::ClientStreamingRequest request; |
| 169 | if (!sData.isEmpty()) { |
| 170 | request.setPayload(sData); |
| 171 | benchData.sendBytes += request.payload().size(); |
| 172 | } |
| 173 | request.setPing(benchData.callCount++); |
| 174 | |
| 175 | const auto stream = mClient.ClientStreaming(request); |
| 176 | |
| 177 | QTimer::singleShot(0, [this, &stream, &request, &benchData]() { |
| 178 | // Run on event loop |
| 179 | mTimer.start(); |
| 180 | for (; benchData.callCount < mCalls; ++benchData.callCount) { |
| 181 | if (request.hasPayload()) |
| 182 | benchData.sendBytes += request.payload().size(); |
| 183 | request.setPing(benchData.callCount); |
| 184 | stream->writeMessage(request); |
| 185 | } |
| 186 | stream->writesDone(); |
| 187 | }); |
| 188 | |
| 189 | QObject::connect(stream.get(), &QGrpcServerStream::finished, this, |
| 190 | [this, &stream, &benchData](const QGrpcStatus &status) { |
| 191 | if (status.isOk()) { |
| 192 | const auto resp = stream->read<qt::bench::ClientStreamingResponse>(); |
| 193 | if (resp->hasPayload()) |
| 194 | benchData.receivedBytes = resp->payload().size(); |
| 195 | benchData.elapsedNanos = mTimer.nsecsElapsed(); |
| 196 | Client::printBenchmarkResult("ClientStreaming", benchData); |
| 197 | } else { |
| 198 | qDebug() << "FAILED: " << status; |
| 199 | } |
| 200 | mLoop.quit(); |
| 201 | }); |
| 202 | mLoop.exec(); |
| 203 | } |
| 204 | |
| 205 | void QtGrpcClientBenchmark::bidiStreaming() |
| 206 | { |
| 207 | BenchmarkData benchData(mCalls); |
| 208 | |
| 209 | QGrpcCallOptions copts; |
| 210 | copts.addMetadata("write-queries"_ba, QString::number(mCalls).toUtf8()); |
| 211 | |
| 212 | qt::bench::BiDiStreamingRequest request; |
| 213 | qt::bench::BiDiStreamingResponse response; |
| 214 | |
| 215 | if (!sData.isEmpty()) { |
| 216 | request.setPayload(sData); |
| 217 | benchData.sendBytes += request.payload().size(); |
| 218 | copts.addMetadata("write-size", QString::number(sData.size()).toUtf8()); |
| 219 | } |
| 220 | |
| 221 | auto stream = mClient.BiDiStreaming(request, copts); |
| 222 | auto *streamPtr = stream.get(); |
| 223 | |
| 224 | QTimer::singleShot(0, [this, &stream, &request, &benchData]() { |
| 225 | // Run on event loop |
| 226 | mTimer.start(); |
| 227 | for (; benchData.callCount < mCalls; ++benchData.callCount) { |
| 228 | if (request.hasPayload()) |
| 229 | benchData.sendBytes += request.payload().size(); |
| 230 | stream->writeMessage(request); |
| 231 | } |
| 232 | stream->writesDone(); |
| 233 | }); |
| 234 | |
| 235 | QObject::connect(streamPtr, &QGrpcBidiStream::messageReceived, this, |
| 236 | [this, stream = streamPtr, &response, &benchData]() { |
| 237 | if (stream->read(&response)) { |
| 238 | if (response.hasPayload()) |
| 239 | benchData.receivedBytes += response.payload().size(); |
| 240 | } else { |
| 241 | qDebug() << "FAILED: read()"; |
| 242 | mLoop.quit(); |
| 243 | } |
| 244 | }); |
| 245 | |
| 246 | QObject::connect(streamPtr, &QGrpcServerStream::finished, this, |
| 247 | [this, &benchData](const QGrpcStatus &status) { |
| 248 | if (status.isOk()) { |
| 249 | benchData.elapsedNanos = mTimer.nsecsElapsed(); |
| 250 | benchData.callCount *= 2; |
| 251 | Client::printBenchmarkResult("BidiStreaming", benchData); |
| 252 | } else { |
| 253 | qDebug() << "FAILED: " << status; |
| 254 | } |
| 255 | mLoop.quit(); |
| 256 | }); |
| 257 | mLoop.exec(); |
| 258 | } |
| 259 | |
| 260 | int main(int argc, char *argv[]) |
| 261 | { |
| 262 | QCoreApplication app(argc, argv); |
| 263 | Client::benchmarkMain<QtGrpcClientBenchmark>("QtGrpcClient", argc, argv); |
| 264 | } |
| 265 | |
| 266 | #include "bench_qtgrpcclient.moc" |