LCOV - code coverage report
Current view: top level - test/cpp/qps - qps_worker.cc (source / functions) Hit Total Coverage
Test: tmp.zDYK9MVh93 Lines: 82 96 85.4 %
Date: 2015-10-10 Functions: 16 16 100.0 %

          Line data    Source code
       1             : /*
       2             :  *
       3             :  * Copyright 2015, Google Inc.
       4             :  * All rights reserved.
       5             :  *
       6             :  * Redistribution and use in source and binary forms, with or without
       7             :  * modification, are permitted provided that the following conditions are
       8             :  * met:
       9             :  *
      10             :  *     * Redistributions of source code must retain the above copyright
      11             :  * notice, this list of conditions and the following disclaimer.
      12             :  *     * Redistributions in binary form must reproduce the above
      13             :  * copyright notice, this list of conditions and the following disclaimer
      14             :  * in the documentation and/or other materials provided with the
      15             :  * distribution.
      16             :  *     * Neither the name of Google Inc. nor the names of its
      17             :  * contributors may be used to endorse or promote products derived from
      18             :  * this software without specific prior written permission.
      19             :  *
      20             :  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
      21             :  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
      22             :  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
      23             :  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
      24             :  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
      25             :  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
      26             :  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
      27             :  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
      28             :  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
      29             :  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
      30             :  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
      31             :  *
      32             :  */
      33             : 
      34             : #include "test/cpp/qps/qps_worker.h"
      35             : 
      36             : #include <cassert>
      37             : #include <memory>
      38             : #include <mutex>
      39             : #include <string>
      40             : #include <thread>
      41             : #include <vector>
      42             : #include <sstream>
      43             : 
      44             : #include <grpc/grpc.h>
      45             : #include <grpc/support/alloc.h>
      46             : #include <grpc/support/histogram.h>
      47             : #include <grpc/support/log.h>
      48             : #include <grpc/support/host_port.h>
      49             : #include <grpc++/client_context.h>
      50             : #include <grpc++/server.h>
      51             : #include <grpc++/server_builder.h>
      52             : #include <grpc++/security/server_credentials.h>
      53             : 
      54             : #include "test/core/util/grpc_profiler.h"
      55             : #include "test/cpp/qps/qpstest.pb.h"
      56             : #include "test/cpp/qps/client.h"
      57             : #include "test/cpp/qps/server.h"
      58             : #include "test/cpp/util/create_test_channel.h"
      59             : 
      60             : namespace grpc {
      61             : namespace testing {
      62             : 
      63           6 : std::unique_ptr<Client> CreateClient(const ClientConfig& config) {
      64           6 :   switch (config.client_type()) {
      65             :     case ClientType::SYNCHRONOUS_CLIENT:
      66           2 :       return (config.rpc_type() == RpcType::UNARY)
      67             :                  ? CreateSynchronousUnaryClient(config)
      68           2 :                  : CreateSynchronousStreamingClient(config);
      69             :     case ClientType::ASYNC_CLIENT:
      70           4 :       return (config.rpc_type() == RpcType::UNARY)
      71             :                  ? CreateAsyncUnaryClient(config)
      72           4 :                  : CreateAsyncStreamingClient(config);
      73             :     default:
      74           0 :       abort();
      75             :   }
      76             :   abort();
      77             : }
      78             : 
      79           6 : std::unique_ptr<Server> CreateServer(const ServerConfig& config,
      80             :                                      int server_port) {
      81           6 :   switch (config.server_type()) {
      82             :     case ServerType::SYNCHRONOUS_SERVER:
      83           2 :       return CreateSynchronousServer(config, server_port);
      84             :     case ServerType::ASYNC_SERVER:
      85           4 :       return CreateAsyncServer(config, server_port);
      86             :     default:
      87           0 :       abort();
      88             :   }
      89             :   abort();
      90             : }
      91             : 
      92          24 : class WorkerImpl GRPC_FINAL : public Worker::Service {
      93             :  public:
      94          12 :   explicit WorkerImpl(int server_port)
      95          12 :       : server_port_(server_port), acquired_(false) {}
      96             : 
      97           6 :   Status RunTest(ServerContext* ctx,
      98             :                  ServerReaderWriter<ClientStatus, ClientArgs>* stream)
      99             :       GRPC_OVERRIDE {
     100           6 :     InstanceGuard g(this);
     101           6 :     if (!g.Acquired()) {
     102           0 :       return Status(StatusCode::RESOURCE_EXHAUSTED, "");
     103             :     }
     104             : 
     105           6 :     grpc_profiler_start("qps_client.prof");
     106          12 :     Status ret = RunTestBody(ctx, stream);
     107           6 :     grpc_profiler_stop();
     108          12 :     return ret;
     109             :   }
     110             : 
     111           6 :   Status RunServer(ServerContext* ctx,
     112             :                    ServerReaderWriter<ServerStatus, ServerArgs>* stream)
     113             :       GRPC_OVERRIDE {
     114           6 :     InstanceGuard g(this);
     115           6 :     if (!g.Acquired()) {
     116           0 :       return Status(StatusCode::RESOURCE_EXHAUSTED, "");
     117             :     }
     118             : 
     119           6 :     grpc_profiler_start("qps_server.prof");
     120          12 :     Status ret = RunServerBody(ctx, stream);
     121           6 :     grpc_profiler_stop();
     122          12 :     return ret;
     123             :   }
     124             : 
     125             :  private:
     126             :   // Protect against multiple clients using this worker at once.
     127             :   class InstanceGuard {
     128             :    public:
     129          12 :     InstanceGuard(WorkerImpl* impl)
     130          12 :         : impl_(impl), acquired_(impl->TryAcquireInstance()) {}
     131          12 :     ~InstanceGuard() {
     132          12 :       if (acquired_) {
     133          12 :         impl_->ReleaseInstance();
     134             :       }
     135          12 :     }
     136             : 
     137          12 :     bool Acquired() const { return acquired_; }
     138             : 
     139             :    private:
     140             :     WorkerImpl* const impl_;
     141             :     const bool acquired_;
     142             :   };
     143             : 
     144          12 :   bool TryAcquireInstance() {
     145          12 :     std::lock_guard<std::mutex> g(mu_);
     146          12 :     if (acquired_) return false;
     147          12 :     acquired_ = true;
     148          12 :     return true;
     149             :   }
     150             : 
     151          12 :   void ReleaseInstance() {
     152          12 :     std::lock_guard<std::mutex> g(mu_);
     153          12 :     GPR_ASSERT(acquired_);
     154          12 :     acquired_ = false;
     155          12 :   }
     156             : 
     157           6 :   Status RunTestBody(ServerContext* ctx,
     158             :                      ServerReaderWriter<ClientStatus, ClientArgs>* stream) {
     159           6 :     ClientArgs args;
     160           6 :     if (!stream->Read(&args)) {
     161           0 :       return Status(StatusCode::INVALID_ARGUMENT, "");
     162             :     }
     163           6 :     if (!args.has_setup()) {
     164           0 :       return Status(StatusCode::INVALID_ARGUMENT, "");
     165             :     }
     166          12 :     auto client = CreateClient(args.setup());
     167           6 :     if (!client) {
     168           0 :       return Status(StatusCode::INVALID_ARGUMENT, "");
     169             :     }
     170          12 :     ClientStatus status;
     171           6 :     if (!stream->Write(status)) {
     172           0 :       return Status(StatusCode::UNKNOWN, "");
     173             :     }
     174          24 :     while (stream->Read(&args)) {
     175          12 :       if (!args.has_mark()) {
     176           0 :         return Status(StatusCode::INVALID_ARGUMENT, "");
     177             :       }
     178          12 :       *status.mutable_stats() = client->Mark();
     179          12 :       stream->Write(status);
     180             :     }
     181             : 
     182          12 :     return Status::OK;
     183             :   }
     184             : 
     185           6 :   Status RunServerBody(ServerContext* ctx,
     186             :                        ServerReaderWriter<ServerStatus, ServerArgs>* stream) {
     187           6 :     ServerArgs args;
     188           6 :     if (!stream->Read(&args)) {
     189           0 :       return Status(StatusCode::INVALID_ARGUMENT, "");
     190             :     }
     191           6 :     if (!args.has_setup()) {
     192           0 :       return Status(StatusCode::INVALID_ARGUMENT, "");
     193             :     }
     194          12 :     auto server = CreateServer(args.setup(), server_port_);
     195           6 :     if (!server) {
     196           0 :       return Status(StatusCode::INVALID_ARGUMENT, "");
     197             :     }
     198          12 :     ServerStatus status;
     199           6 :     status.set_port(server_port_);
     200           6 :     if (!stream->Write(status)) {
     201           0 :       return Status(StatusCode::UNKNOWN, "");
     202             :     }
     203          24 :     while (stream->Read(&args)) {
     204          12 :       if (!args.has_mark()) {
     205           0 :         return Status(StatusCode::INVALID_ARGUMENT, "");
     206             :       }
     207          12 :       *status.mutable_stats() = server->Mark();
     208          12 :       stream->Write(status);
     209             :     }
     210             : 
     211          12 :     return Status::OK;
     212             :   }
     213             : 
     214             :   const int server_port_;
     215             : 
     216             :   std::mutex mu_;
     217             :   bool acquired_;
     218             : };
     219             : 
     220          12 : QpsWorker::QpsWorker(int driver_port, int server_port) {
     221          12 :   impl_.reset(new WorkerImpl(server_port));
     222             : 
     223          12 :   char* server_address = NULL;
     224          12 :   gpr_join_host_port(&server_address, "::", driver_port);
     225             : 
     226          12 :   ServerBuilder builder;
     227          12 :   builder.AddListeningPort(server_address, InsecureServerCredentials());
     228          12 :   builder.RegisterService(impl_.get());
     229             : 
     230          12 :   gpr_free(server_address);
     231             : 
     232          12 :   server_ = builder.BuildAndStart();
     233          12 : }
     234             : 
     235          12 : QpsWorker::~QpsWorker() {}
     236             : 
     237             : }  // namespace testing
     238             : }  // namespace grpc

Generated by: LCOV version 1.10