File: callback_unary_ping_pong.h

package info (click to toggle)
grpc 1.51.1-7
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 78,328 kB
  • sloc: cpp: 361,873; python: 72,206; ansic: 37,787; objc: 12,434; ruby: 11,521; sh: 7,652; php: 7,615; makefile: 3,481; xml: 3,246; cs: 1,836; javascript: 1,614; java: 465; pascal: 227; awk: 132
file content (100 lines) | stat: -rw-r--r-- 3,333 bytes parent folder | download | duplicates (3)
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
/*
 *
 * 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.
 *
 */

/* Benchmark gRPC end2end in various configurations */

#ifndef TEST_CPP_MICROBENCHMARKS_CALLBACK_UNARY_PING_PONG_H
#define TEST_CPP_MICROBENCHMARKS_CALLBACK_UNARY_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
 */

inline void SendCallbackUnaryPingPong(
    benchmark::State* state, ClientContext* cli_ctx, EchoRequest* request,
    EchoResponse* response, EchoTestService::Stub* stub_, bool* done,
    std::mutex* mu, std::condition_variable* cv) {
  int response_msgs_size = state->range(1);
  cli_ctx->AddMetadata(kServerMessageSize, std::to_string(response_msgs_size));
  stub_->async()->Echo(
      cli_ctx, request, response,
      [state, cli_ctx, request, response, stub_, done, mu, cv](Status s) {
        GPR_ASSERT(s.ok());
        if (state->KeepRunning()) {
          cli_ctx->~ClientContext();
          new (cli_ctx) ClientContext();
          SendCallbackUnaryPingPong(state, cli_ctx, request, response, stub_,
                                    done, mu, cv);
        } else {
          std::lock_guard<std::mutex> l(*mu);
          *done = true;
          cv->notify_one();
        }
      });
};

template <class Fixture, class ClientContextMutator, class ServerContextMutator>
static void BM_CallbackUnaryPingPong(benchmark::State& state) {
  int request_msgs_size = state.range(0);
  int response_msgs_size = 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 (request_msgs_size > 0) {
    request.set_message(std::string(request_msgs_size, 'a'));
  } else {
    request.set_message("");
  }

  std::mutex mu;
  std::condition_variable cv;
  bool done = false;
  if (state.KeepRunning()) {
    SendCallbackUnaryPingPong(&state, &cli_ctx, &request, &response,
                              stub_.get(), &done, &mu, &cv);
  }
  std::unique_lock<std::mutex> l(mu);
  while (!done) {
    cv.wait(l);
  }
  fixture.reset();
  state.SetBytesProcessed(request_msgs_size * state.iterations() +
                          response_msgs_size * state.iterations());
}

}  // namespace testing
}  // namespace grpc

#endif  // TEST_CPP_MICROBENCHMARKS_FULLSTACK_UNARY_PING_PONG_H