Home | History | Annotate | Download | only in microbenchmarks
      1 /*
      2  *
      3  * Copyright 2017 gRPC authors.
      4  *
      5  * Licensed under the Apache License, Version 2.0 (the "License");
      6  * you may not use this file except in compliance with the License.
      7  * You may obtain a copy of the License at
      8  *
      9  *     http://www.apache.org/licenses/LICENSE-2.0
     10  *
     11  * Unless required by applicable law or agreed to in writing, software
     12  * distributed under the License is distributed on an "AS IS" BASIS,
     13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     14  * See the License for the specific language governing permissions and
     15  * limitations under the License.
     16  *
     17  */
     18 
     19 #ifndef TEST_CPP_MICROBENCHMARKS_FULLSTACK_FIXTURES_H
     20 #define TEST_CPP_MICROBENCHMARKS_FULLSTACK_FIXTURES_H
     21 
     22 #include <grpc/support/atm.h>
     23 #include <grpc/support/log.h>
     24 #include <grpcpp/channel.h>
     25 #include <grpcpp/create_channel.h>
     26 #include <grpcpp/security/credentials.h>
     27 #include <grpcpp/security/server_credentials.h>
     28 #include <grpcpp/server.h>
     29 #include <grpcpp/server_builder.h>
     30 
     31 #include "src/core/ext/transport/chttp2/transport/chttp2_transport.h"
     32 #include "src/core/lib/channel/channel_args.h"
     33 #include "src/core/lib/iomgr/endpoint.h"
     34 #include "src/core/lib/iomgr/endpoint_pair.h"
     35 #include "src/core/lib/iomgr/exec_ctx.h"
     36 #include "src/core/lib/iomgr/tcp_posix.h"
     37 #include "src/core/lib/surface/channel.h"
     38 #include "src/core/lib/surface/completion_queue.h"
     39 #include "src/core/lib/surface/server.h"
     40 #include "test/core/util/passthru_endpoint.h"
     41 #include "test/core/util/port.h"
     42 
     43 #include "src/cpp/client/create_channel_internal.h"
     44 #include "test/cpp/microbenchmarks/helpers.h"
     45 
     46 namespace grpc {
     47 namespace testing {
     48 
     49 class FixtureConfiguration {
     50  public:
     51   virtual ~FixtureConfiguration() {}
     52   virtual void ApplyCommonChannelArguments(ChannelArguments* c) const {
     53     c->SetInt(GRPC_ARG_MAX_RECEIVE_MESSAGE_LENGTH, INT_MAX);
     54     c->SetInt(GRPC_ARG_MAX_SEND_MESSAGE_LENGTH, INT_MAX);
     55   }
     56 
     57   virtual void ApplyCommonServerBuilderConfig(ServerBuilder* b) const {
     58     b->SetMaxReceiveMessageSize(INT_MAX);
     59     b->SetMaxSendMessageSize(INT_MAX);
     60   }
     61 };
     62 
     63 class BaseFixture : public TrackCounters {};
     64 
     65 // Special tag to be used in Server shutdown. This tag is *NEVER* returned when
     66 // Cq->Next() API is called (This is because FinalizeResult() function in this
     67 // class always returns 'false'). This is intentional and makes writing shutdown
     68 // code easier.
     69 class ShutdownTag : public internal::CompletionQueueTag {
     70  public:
     71   bool FinalizeResult(void** tag, bool* status) { return false; }
     72 };
     73 
     74 class FullstackFixture : public BaseFixture {
     75  public:
     76   FullstackFixture(Service* service, const FixtureConfiguration& config,
     77                    const grpc::string& address) {
     78     ServerBuilder b;
     79     if (address.length() > 0) {
     80       b.AddListeningPort(address, InsecureServerCredentials());
     81     }
     82     cq_ = b.AddCompletionQueue(true);
     83     b.RegisterService(service);
     84     config.ApplyCommonServerBuilderConfig(&b);
     85     server_ = b.BuildAndStart();
     86     ChannelArguments args;
     87     config.ApplyCommonChannelArguments(&args);
     88     if (address.length() > 0) {
     89       channel_ =
     90           CreateCustomChannel(address, InsecureChannelCredentials(), args);
     91     } else {
     92       channel_ = server_->InProcessChannel(args);
     93     }
     94   }
     95 
     96   virtual ~FullstackFixture() {
     97     // Dummy shutdown tag (this tag is swallowed by cq->Next() and is not
     98     // returned to the user) see ShutdownTag definition for more details
     99     ShutdownTag shutdown_tag;
    100     grpc_server_shutdown_and_notify(server_->c_server(), cq_->cq(),
    101                                     &shutdown_tag);
    102     cq_->Shutdown();
    103     void* tag;
    104     bool ok;
    105     while (cq_->Next(&tag, &ok)) {
    106     }
    107   }
    108 
    109   void AddToLabel(std::ostream& out, benchmark::State& state) {
    110     BaseFixture::AddToLabel(out, state);
    111     out << " polls/iter:"
    112         << static_cast<double>(grpc_get_cq_poll_num(this->cq()->cq())) /
    113                state.iterations();
    114   }
    115 
    116   ServerCompletionQueue* cq() { return cq_.get(); }
    117   std::shared_ptr<Channel> channel() { return channel_; }
    118 
    119  private:
    120   std::unique_ptr<Server> server_;
    121   std::unique_ptr<ServerCompletionQueue> cq_;
    122   std::shared_ptr<Channel> channel_;
    123 };
    124 
    125 class TCP : public FullstackFixture {
    126  public:
    127   TCP(Service* service, const FixtureConfiguration& fixture_configuration =
    128                             FixtureConfiguration())
    129       : FullstackFixture(service, fixture_configuration, MakeAddress(&port_)) {}
    130 
    131   ~TCP() { grpc_recycle_unused_port(port_); }
    132 
    133  private:
    134   int port_;
    135 
    136   static grpc::string MakeAddress(int* port) {
    137     *port = grpc_pick_unused_port_or_die();
    138     std::stringstream addr;
    139     addr << "localhost:" << *port;
    140     return addr.str();
    141   }
    142 };
    143 
    144 class UDS : public FullstackFixture {
    145  public:
    146   UDS(Service* service, const FixtureConfiguration& fixture_configuration =
    147                             FixtureConfiguration())
    148       : FullstackFixture(service, fixture_configuration, MakeAddress(&port_)) {}
    149 
    150   ~UDS() { grpc_recycle_unused_port(port_); }
    151 
    152  private:
    153   int port_;
    154 
    155   static grpc::string MakeAddress(int* port) {
    156     *port = grpc_pick_unused_port_or_die();  // just for a unique id - not a
    157                                              // real port
    158     std::stringstream addr;
    159     addr << "unix:/tmp/bm_fullstack." << *port;
    160     return addr.str();
    161   }
    162 };
    163 
    164 class InProcess : public FullstackFixture {
    165  public:
    166   InProcess(Service* service,
    167             const FixtureConfiguration& fixture_configuration =
    168                 FixtureConfiguration())
    169       : FullstackFixture(service, fixture_configuration, "") {}
    170   ~InProcess() {}
    171 };
    172 
    173 class EndpointPairFixture : public BaseFixture {
    174  public:
    175   EndpointPairFixture(Service* service, grpc_endpoint_pair endpoints,
    176                       const FixtureConfiguration& fixture_configuration)
    177       : endpoint_pair_(endpoints) {
    178     ServerBuilder b;
    179     cq_ = b.AddCompletionQueue(true);
    180     b.RegisterService(service);
    181     fixture_configuration.ApplyCommonServerBuilderConfig(&b);
    182     server_ = b.BuildAndStart();
    183 
    184     grpc_core::ExecCtx exec_ctx;
    185 
    186     /* add server endpoint to server_
    187      * */
    188     {
    189       const grpc_channel_args* server_args =
    190           grpc_server_get_channel_args(server_->c_server());
    191       server_transport_ = grpc_create_chttp2_transport(
    192           server_args, endpoints.server, false /* is_client */);
    193 
    194       grpc_pollset** pollsets;
    195       size_t num_pollsets = 0;
    196       grpc_server_get_pollsets(server_->c_server(), &pollsets, &num_pollsets);
    197 
    198       for (size_t i = 0; i < num_pollsets; i++) {
    199         grpc_endpoint_add_to_pollset(endpoints.server, pollsets[i]);
    200       }
    201 
    202       grpc_server_setup_transport(server_->c_server(), server_transport_,
    203                                   nullptr, server_args);
    204       grpc_chttp2_transport_start_reading(server_transport_, nullptr, nullptr);
    205     }
    206 
    207     /* create channel */
    208     {
    209       ChannelArguments args;
    210       args.SetString(GRPC_ARG_DEFAULT_AUTHORITY, "test.authority");
    211       fixture_configuration.ApplyCommonChannelArguments(&args);
    212 
    213       grpc_channel_args c_args = args.c_channel_args();
    214       client_transport_ =
    215           grpc_create_chttp2_transport(&c_args, endpoints.client, true);
    216       GPR_ASSERT(client_transport_);
    217       grpc_channel* channel = grpc_channel_create(
    218           "target", &c_args, GRPC_CLIENT_DIRECT_CHANNEL, client_transport_);
    219       grpc_chttp2_transport_start_reading(client_transport_, nullptr, nullptr);
    220 
    221       channel_ = CreateChannelInternal("", channel);
    222     }
    223   }
    224 
    225   virtual ~EndpointPairFixture() {
    226     // Dummy shutdown tag (this tag is swallowed by cq->Next() and is not
    227     // returned to the user) see ShutdownTag definition for more details
    228     ShutdownTag shutdown_tag;
    229     grpc_server_shutdown_and_notify(server_->c_server(), cq_->cq(),
    230                                     &shutdown_tag);
    231     cq_->Shutdown();
    232     void* tag;
    233     bool ok;
    234     while (cq_->Next(&tag, &ok)) {
    235     }
    236   }
    237 
    238   void AddToLabel(std::ostream& out, benchmark::State& state) {
    239     BaseFixture::AddToLabel(out, state);
    240     out << " polls/iter:"
    241         << static_cast<double>(grpc_get_cq_poll_num(this->cq()->cq())) /
    242                state.iterations();
    243   }
    244 
    245   ServerCompletionQueue* cq() { return cq_.get(); }
    246   std::shared_ptr<Channel> channel() { return channel_; }
    247 
    248  protected:
    249   grpc_endpoint_pair endpoint_pair_;
    250   grpc_transport* client_transport_;
    251   grpc_transport* server_transport_;
    252 
    253  private:
    254   std::unique_ptr<Server> server_;
    255   std::unique_ptr<ServerCompletionQueue> cq_;
    256   std::shared_ptr<Channel> channel_;
    257 };
    258 
    259 class SockPair : public EndpointPairFixture {
    260  public:
    261   SockPair(Service* service, const FixtureConfiguration& fixture_configuration =
    262                                  FixtureConfiguration())
    263       : EndpointPairFixture(service,
    264                             grpc_iomgr_create_endpoint_pair("test", nullptr),
    265                             fixture_configuration) {}
    266 };
    267 
    268 /* Use InProcessCHTTP2 instead. This class (with stats as an explicit parameter)
    269    is here only to be able to initialize both the base class and stats_ with the
    270    same stats instance without accessing the stats_ fields before the object is
    271    properly initialized. */
    272 class InProcessCHTTP2WithExplicitStats : public EndpointPairFixture {
    273  public:
    274   InProcessCHTTP2WithExplicitStats(
    275       Service* service, grpc_passthru_endpoint_stats* stats,
    276       const FixtureConfiguration& fixture_configuration)
    277       : EndpointPairFixture(service, MakeEndpoints(stats),
    278                             fixture_configuration),
    279         stats_(stats) {}
    280 
    281   virtual ~InProcessCHTTP2WithExplicitStats() {
    282     if (stats_ != nullptr) {
    283       grpc_passthru_endpoint_stats_destroy(stats_);
    284     }
    285   }
    286 
    287   void AddToLabel(std::ostream& out, benchmark::State& state) {
    288     EndpointPairFixture::AddToLabel(out, state);
    289     out << " writes/iter:"
    290         << static_cast<double>(gpr_atm_no_barrier_load(&stats_->num_writes)) /
    291                static_cast<double>(state.iterations());
    292   }
    293 
    294  private:
    295   grpc_passthru_endpoint_stats* stats_;
    296 
    297   static grpc_endpoint_pair MakeEndpoints(grpc_passthru_endpoint_stats* stats) {
    298     grpc_endpoint_pair p;
    299     grpc_passthru_endpoint_create(&p.client, &p.server, Library::get().rq(),
    300                                   stats);
    301     return p;
    302   }
    303 };
    304 
    305 class InProcessCHTTP2 : public InProcessCHTTP2WithExplicitStats {
    306  public:
    307   InProcessCHTTP2(Service* service,
    308                   const FixtureConfiguration& fixture_configuration =
    309                       FixtureConfiguration())
    310       : InProcessCHTTP2WithExplicitStats(service,
    311                                          grpc_passthru_endpoint_stats_create(),
    312                                          fixture_configuration) {}
    313 };
    314 
    315 ////////////////////////////////////////////////////////////////////////////////
    316 // Minimal stack fixtures
    317 
    318 class MinStackConfiguration : public FixtureConfiguration {
    319   void ApplyCommonChannelArguments(ChannelArguments* a) const override {
    320     a->SetInt(GRPC_ARG_MINIMAL_STACK, 1);
    321     FixtureConfiguration::ApplyCommonChannelArguments(a);
    322   }
    323 
    324   void ApplyCommonServerBuilderConfig(ServerBuilder* b) const override {
    325     b->AddChannelArgument(GRPC_ARG_MINIMAL_STACK, 1);
    326     FixtureConfiguration::ApplyCommonServerBuilderConfig(b);
    327   }
    328 };
    329 
    330 template <class Base>
    331 class MinStackize : public Base {
    332  public:
    333   MinStackize(Service* service) : Base(service, MinStackConfiguration()) {}
    334 };
    335 
    336 typedef MinStackize<TCP> MinTCP;
    337 typedef MinStackize<UDS> MinUDS;
    338 typedef MinStackize<InProcess> MinInProcess;
    339 typedef MinStackize<SockPair> MinSockPair;
    340 typedef MinStackize<InProcessCHTTP2> MinInProcessCHTTP2;
    341 
    342 }  // namespace testing
    343 }  // namespace grpc
    344 
    345 #endif
    346