LCOV - code coverage report
Current view: top level - test/cpp/qps - client_sync.cc (source / functions) Hit Total Coverage
Test: tmp.zDYK9MVh93 Lines: 53 56 94.6 %
Date: 2015-10-10 Functions: 15 16 93.8 %

          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 <cassert>
      35             : #include <chrono>
      36             : #include <memory>
      37             : #include <mutex>
      38             : #include <string>
      39             : #include <thread>
      40             : #include <vector>
      41             : #include <sstream>
      42             : 
      43             : #include <gflags/gflags.h>
      44             : #include <grpc/grpc.h>
      45             : #include <grpc/support/alloc.h>
      46             : #include <grpc/support/histogram.h>
      47             : #include <grpc/support/host_port.h>
      48             : #include <grpc/support/log.h>
      49             : #include <grpc/support/time.h>
      50             : #include <grpc++/client_context.h>
      51             : #include <grpc++/server.h>
      52             : #include <grpc++/server_builder.h>
      53             : #include <gtest/gtest.h>
      54             : 
      55             : #include "test/cpp/util/create_test_channel.h"
      56             : #include "test/cpp/qps/client.h"
      57             : #include "test/cpp/qps/qpstest.grpc.pb.h"
      58             : #include "test/cpp/qps/histogram.h"
      59             : #include "test/cpp/qps/interarrival.h"
      60             : #include "test/cpp/qps/timer.h"
      61             : 
      62             : namespace grpc {
      63             : namespace testing {
      64             : 
      65             : class SynchronousClient : public Client {
      66             :  public:
      67           2 :   SynchronousClient(const ClientConfig& config) : Client(config) {
      68             :     num_threads_ =
      69           2 :         config.outstanding_rpcs_per_channel() * config.client_channels();
      70           2 :     responses_.resize(num_threads_);
      71           2 :     SetupLoadTest(config, num_threads_);
      72           2 :   }
      73             : 
      74           2 :   virtual ~SynchronousClient(){};
      75             : 
      76             :  protected:
      77      111951 :   void WaitToIssue(int thread_idx) {
      78      111951 :     grpc_time next_time;
      79      111951 :     if (NextIssueTime(thread_idx, &next_time)) {
      80             :       gpr_timespec next_timespec;
      81           0 :       TimepointHR2Timespec(next_time, &next_timespec);
      82           0 :       gpr_sleep_until(next_timespec);
      83             :     }
      84      111951 :   }
      85             : 
      86             :   size_t num_threads_;
      87             :   std::vector<SimpleResponse> responses_;
      88             : };
      89             : 
      90             : class SynchronousUnaryClient GRPC_FINAL : public SynchronousClient {
      91             :  public:
      92           1 :   SynchronousUnaryClient(const ClientConfig& config)
      93           1 :       : SynchronousClient(config) {
      94           1 :     StartThreads(num_threads_);
      95           1 :   }
      96           2 :   ~SynchronousUnaryClient() { EndThreads(); }
      97             : 
      98       58803 :   bool ThreadFunc(Histogram* histogram, size_t thread_idx) GRPC_OVERRIDE {
      99       58803 :     WaitToIssue(thread_idx);
     100       58803 :     auto* stub = channels_[thread_idx % channels_.size()].get_stub();
     101       58803 :     double start = Timer::Now();
     102       58803 :     grpc::ClientContext context;
     103             :     grpc::Status s =
     104      117606 :         stub->UnaryCall(&context, request_, &responses_[thread_idx]);
     105       58803 :     histogram->Add((Timer::Now() - start) * 1e9);
     106      117606 :     return s.ok();
     107             :   }
     108             : };
     109             : 
     110             : class SynchronousStreamingClient GRPC_FINAL : public SynchronousClient {
     111             :  public:
     112           1 :   SynchronousStreamingClient(const ClientConfig& config)
     113           1 :       : SynchronousClient(config) {
     114           1 :     context_ = new grpc::ClientContext[num_threads_];
     115             :     stream_ = new std::unique_ptr<
     116           1 :         grpc::ClientReaderWriter<SimpleRequest, SimpleResponse>>[num_threads_];
     117           2 :     for (size_t thread_idx = 0; thread_idx < num_threads_; thread_idx++) {
     118           1 :       auto* stub = channels_[thread_idx % channels_.size()].get_stub();
     119           1 :       stream_[thread_idx] = stub->StreamingCall(&context_[thread_idx]);
     120             :     }
     121           1 :     StartThreads(num_threads_);
     122           1 :   }
     123           3 :   ~SynchronousStreamingClient() {
     124           1 :     EndThreads();
     125           2 :     for (auto stream = &stream_[0]; stream != &stream_[num_threads_];
     126             :          stream++) {
     127           1 :       if (*stream) {
     128           1 :         (*stream)->WritesDone();
     129           1 :         EXPECT_TRUE((*stream)->Finish().ok());
     130             :       }
     131             :     }
     132           1 :     delete[] stream_;
     133           1 :     delete[] context_;
     134           2 :   }
     135             : 
     136       53148 :   bool ThreadFunc(Histogram* histogram, size_t thread_idx) GRPC_OVERRIDE {
     137       53148 :     WaitToIssue(thread_idx);
     138       53148 :     double start = Timer::Now();
     139      106296 :     if (stream_[thread_idx]->Write(request_) &&
     140       53148 :         stream_[thread_idx]->Read(&responses_[thread_idx])) {
     141       53148 :       histogram->Add((Timer::Now() - start) * 1e9);
     142       53148 :       return true;
     143             :     }
     144           0 :     return false;
     145             :   }
     146             : 
     147             :  private:
     148             :   // These are both conceptually std::vector but cannot be for old compilers
     149             :   // that expect contained classes to support copy constructors
     150             :   grpc::ClientContext* context_;
     151             :   std::unique_ptr<grpc::ClientReaderWriter<SimpleRequest, SimpleResponse>>*
     152             :       stream_;
     153             : };
     154             : 
     155           1 : std::unique_ptr<Client> CreateSynchronousUnaryClient(
     156             :     const ClientConfig& config) {
     157           1 :   return std::unique_ptr<Client>(new SynchronousUnaryClient(config));
     158             : }
     159           1 : std::unique_ptr<Client> CreateSynchronousStreamingClient(
     160             :     const ClientConfig& config) {
     161           1 :   return std::unique_ptr<Client>(new SynchronousStreamingClient(config));
     162             : }
     163             : 
     164             : }  // namespace testing
     165          18 : }  // namespace grpc

Generated by: LCOV version 1.10