1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
|
/*
*
* Copyright 2019 gRPC authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
#ifndef TEST_CPP_MICROBENCHMARKS_CALLBACK_STREAMING_PING_PONG_H
#define TEST_CPP_MICROBENCHMARKS_CALLBACK_STREAMING_PING_PONG_H
#include <sstream>
#include <benchmark/benchmark.h>
#include "src/proto/grpc/testing/echo.grpc.pb.h"
#include "test/cpp/microbenchmarks/callback_test_service.h"
#include "test/cpp/microbenchmarks/fullstack_context_mutators.h"
#include "test/cpp/microbenchmarks/fullstack_fixtures.h"
namespace grpc {
namespace testing {
/*******************************************************************************
* BENCHMARKING KERNELS
*/
class BidiClient : public grpc::ClientBidiReactor<EchoRequest, EchoResponse> {
public:
BidiClient(benchmark::State* state, EchoTestService::Stub* stub,
ClientContext* cli_ctx, EchoRequest* request,
EchoResponse* response)
: state_{state},
stub_{stub},
cli_ctx_{cli_ctx},
request_{request},
response_{response} {
msgs_size_ = state->range(0);
msgs_to_send_ = state->range(1);
StartNewRpc();
}
void OnReadDone(bool ok) override {
if (!ok) {
gpr_log(GPR_ERROR, "Client read failed");
return;
}
MaybeWrite();
}
void OnWriteDone(bool ok) override {
if (!ok) {
gpr_log(GPR_ERROR, "Client write failed");
return;
}
writes_complete_++;
StartRead(response_);
}
void OnDone(const Status& s) override {
GPR_ASSERT(s.ok());
GPR_ASSERT(writes_complete_ == msgs_to_send_);
if (state_->KeepRunning()) {
writes_complete_ = 0;
StartNewRpc();
} else {
std::unique_lock<std::mutex> l(mu);
done = true;
cv.notify_one();
}
}
void StartNewRpc() {
cli_ctx_->~ClientContext();
new (cli_ctx_) ClientContext();
cli_ctx_->AddMetadata(kServerMessageSize, std::to_string(msgs_size_));
stub_->async()->BidiStream(cli_ctx_, this);
MaybeWrite();
StartCall();
}
void Await() {
std::unique_lock<std::mutex> l(mu);
while (!done) {
cv.wait(l);
}
}
private:
void MaybeWrite() {
if (writes_complete_ < msgs_to_send_) {
StartWrite(request_);
} else {
StartWritesDone();
}
}
benchmark::State* state_;
EchoTestService::Stub* stub_;
ClientContext* cli_ctx_;
EchoRequest* request_;
EchoResponse* response_;
int writes_complete_{0};
int msgs_to_send_;
int msgs_size_;
std::mutex mu;
std::condition_variable cv;
bool done = false;
};
template <class Fixture, class ClientContextMutator, class ServerContextMutator>
static void BM_CallbackBidiStreaming(benchmark::State& state) {
int message_size = state.range(0);
int max_ping_pongs = state.range(1);
CallbackStreamingTestService service;
std::unique_ptr<Fixture> fixture(new Fixture(&service));
std::unique_ptr<EchoTestService::Stub> stub_(
EchoTestService::NewStub(fixture->channel()));
EchoRequest request;
EchoResponse response;
ClientContext cli_ctx;
if (message_size > 0) {
request.set_message(std::string(message_size, 'a'));
} else {
request.set_message("");
}
if (state.KeepRunning()) {
BidiClient test{&state, stub_.get(), &cli_ctx, &request, &response};
test.Await();
}
fixture.reset();
state.SetBytesProcessed(2 * message_size * max_ping_pongs *
state.iterations());
}
} // namespace testing
} // namespace grpc
#endif // TEST_CPP_MICROBENCHMARKS_CALLBACK_STREAMING_PING_PONG_H
|