xref: /aosp_15_r20/external/grpc-grpc/test/cpp/qps/client.h (revision cc02d7e222339f7a4f6ba5f422e6413f4bd931f2)
1 //
2 //
3 // Copyright 2015 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 GRPC_TEST_CPP_QPS_CLIENT_H
20 #define GRPC_TEST_CPP_QPS_CLIENT_H
21 
22 #include <inttypes.h>
23 #include <stdint.h>
24 #include <stdlib.h>
25 
26 #include <condition_variable>
27 #include <mutex>
28 #include <thread>
29 #include <unordered_map>
30 #include <vector>
31 
32 #include "absl/memory/memory.h"
33 #include "absl/strings/match.h"
34 #include "absl/strings/str_format.h"
35 
36 #include <grpc/support/log.h>
37 #include <grpc/support/time.h>
38 #include <grpcpp/channel.h>
39 #include <grpcpp/support/byte_buffer.h>
40 #include <grpcpp/support/channel_arguments.h>
41 #include <grpcpp/support/slice.h>
42 
43 #include "src/core/lib/gprpp/crash.h"
44 #include "src/core/lib/gprpp/env.h"
45 #include "src/proto/grpc/testing/benchmark_service.grpc.pb.h"
46 #include "src/proto/grpc/testing/payloads.pb.h"
47 #include "test/cpp/qps/histogram.h"
48 #include "test/cpp/qps/interarrival.h"
49 #include "test/cpp/qps/qps_worker.h"
50 #include "test/cpp/qps/server.h"
51 #include "test/cpp/qps/usage_timer.h"
52 #include "test/cpp/util/create_test_channel.h"
53 #include "test/cpp/util/test_credentials_provider.h"
54 
55 #define INPROC_NAME_PREFIX "qpsinproc:"
56 
57 namespace grpc {
58 namespace testing {
59 
60 template <class RequestType>
61 class ClientRequestCreator {
62  public:
ClientRequestCreator(RequestType *,const PayloadConfig &)63   ClientRequestCreator(RequestType* /*req*/, const PayloadConfig&) {
64     // this template must be specialized
65     // fail with an assertion rather than a compile-time
66     // check since these only happen at the beginning anyway
67     grpc_core::Crash("unreachable");
68   }
69 };
70 
71 template <>
72 class ClientRequestCreator<SimpleRequest> {
73  public:
ClientRequestCreator(SimpleRequest * req,const PayloadConfig & payload_config)74   ClientRequestCreator(SimpleRequest* req,
75                        const PayloadConfig& payload_config) {
76     if (payload_config.has_bytebuf_params()) {
77       grpc_core::Crash(absl::StrFormat(
78           "Invalid PayloadConfig, config cannot have bytebuf_params: %s",
79           payload_config.DebugString()
80               .c_str()));  // not appropriate for this specialization
81     } else if (payload_config.has_simple_params()) {
82       req->set_response_type(grpc::testing::PayloadType::COMPRESSABLE);
83       req->set_response_size(payload_config.simple_params().resp_size());
84       req->mutable_payload()->set_type(
85           grpc::testing::PayloadType::COMPRESSABLE);
86       int size = payload_config.simple_params().req_size();
87       std::unique_ptr<char[]> body(new char[size]);
88       req->mutable_payload()->set_body(body.get(), size);
89     } else if (payload_config.has_complex_params()) {
90       grpc_core::Crash(absl::StrFormat(
91           "Invalid PayloadConfig, cannot have complex_params: %s",
92           payload_config.DebugString()
93               .c_str()));  // not appropriate for this specialization
94     } else {
95       // default should be simple proto without payloads
96       req->set_response_type(grpc::testing::PayloadType::COMPRESSABLE);
97       req->set_response_size(0);
98       req->mutable_payload()->set_type(
99           grpc::testing::PayloadType::COMPRESSABLE);
100     }
101   }
102 };
103 
104 template <>
105 class ClientRequestCreator<ByteBuffer> {
106  public:
ClientRequestCreator(ByteBuffer * req,const PayloadConfig & payload_config)107   ClientRequestCreator(ByteBuffer* req, const PayloadConfig& payload_config) {
108     if (payload_config.has_bytebuf_params()) {
109       size_t req_sz =
110           static_cast<size_t>(payload_config.bytebuf_params().req_size());
111       std::unique_ptr<char[]> buf(new char[req_sz]);
112       memset(buf.get(), 0, req_sz);
113       Slice slice(buf.get(), req_sz);
114       *req = ByteBuffer(&slice, 1);
115     } else {
116       grpc_core::Crash(absl::StrFormat(
117           "Invalid PayloadConfig, missing bytebug_params: %s",
118           payload_config.DebugString()
119               .c_str()));  // not appropriate for this specialization
120     }
121   }
122 };
123 
124 class HistogramEntry final {
125  public:
HistogramEntry()126   HistogramEntry() : value_used_(false), status_used_(false) {}
value_used()127   bool value_used() const { return value_used_; }
value()128   double value() const { return value_; }
set_value(double v)129   void set_value(double v) {
130     value_used_ = true;
131     value_ = v;
132   }
status_used()133   bool status_used() const { return status_used_; }
status()134   int status() const { return status_; }
set_status(int status)135   void set_status(int status) {
136     status_used_ = true;
137     status_ = status;
138   }
139 
140  private:
141   bool value_used_;
142   double value_;
143   bool status_used_;
144   int status_;
145 };
146 
147 typedef std::unordered_map<int, int64_t> StatusHistogram;
148 
MergeStatusHistogram(const StatusHistogram & from,StatusHistogram * to)149 inline void MergeStatusHistogram(const StatusHistogram& from,
150                                  StatusHistogram* to) {
151   for (StatusHistogram::const_iterator it = from.begin(); it != from.end();
152        ++it) {
153     (*to)[it->first] += it->second;
154   }
155 }
156 
157 class Client {
158  public:
Client()159   Client()
160       : timer_(new UsageTimer),
161         interarrival_timer_(),
162         started_requests_(false),
163         last_reset_poll_count_(0) {
164     gpr_event_init(&start_requests_);
165   }
~Client()166   virtual ~Client() {}
167 
Mark(bool reset)168   ClientStats Mark(bool reset) {
169     Histogram latencies;
170     StatusHistogram statuses;
171     UsageTimer::Result timer_result;
172 
173     MaybeStartRequests();
174 
175     int cur_poll_count = GetPollCount();
176     int poll_count = cur_poll_count - last_reset_poll_count_;
177     if (reset) {
178       std::vector<Histogram> to_merge(threads_.size());
179       std::vector<StatusHistogram> to_merge_status(threads_.size());
180 
181       for (size_t i = 0; i < threads_.size(); i++) {
182         threads_[i]->BeginSwap(&to_merge[i], &to_merge_status[i]);
183       }
184       std::unique_ptr<UsageTimer> timer(new UsageTimer);
185       timer_.swap(timer);
186       for (size_t i = 0; i < threads_.size(); i++) {
187         latencies.Merge(to_merge[i]);
188         MergeStatusHistogram(to_merge_status[i], &statuses);
189       }
190       timer_result = timer->Mark();
191       last_reset_poll_count_ = cur_poll_count;
192     } else {
193       // merge snapshots of each thread histogram
194       for (size_t i = 0; i < threads_.size(); i++) {
195         threads_[i]->MergeStatsInto(&latencies, &statuses);
196       }
197       timer_result = timer_->Mark();
198     }
199 
200     // Print the median latency per interval for one thread.
201     // If the number of warmup seconds is x, then the first x + 1 numbers in the
202     // vector are from the warmup period and should be discarded.
203     if (median_latency_collection_interval_seconds_ > 0) {
204       std::vector<double> medians_per_interval =
205           threads_[0]->GetMedianPerIntervalList();
206       gpr_log(GPR_INFO, "Num threads: %zu", threads_.size());
207       gpr_log(GPR_INFO, "Number of medians: %zu", medians_per_interval.size());
208       for (size_t j = 0; j < medians_per_interval.size(); j++) {
209         gpr_log(GPR_INFO, "%f", medians_per_interval[j]);
210       }
211     }
212 
213     ClientStats stats;
214     latencies.FillProto(stats.mutable_latencies());
215     for (StatusHistogram::const_iterator it = statuses.begin();
216          it != statuses.end(); ++it) {
217       RequestResultCount* rrc = stats.add_request_results();
218       rrc->set_status_code(it->first);
219       rrc->set_count(it->second);
220     }
221     stats.set_time_elapsed(timer_result.wall);
222     stats.set_time_system(timer_result.system);
223     stats.set_time_user(timer_result.user);
224     stats.set_cq_poll_count(poll_count);
225     return stats;
226   }
227 
228   // Must call AwaitThreadsCompletion before destructor to avoid a race
229   // between destructor and invocation of virtual ThreadFunc
AwaitThreadsCompletion()230   void AwaitThreadsCompletion() {
231     gpr_atm_rel_store(&thread_pool_done_, static_cast<gpr_atm>(true));
232     DestroyMultithreading();
233     std::unique_lock<std::mutex> g(thread_completion_mu_);
234     while (threads_remaining_ != 0) {
235       threads_complete_.wait(g);
236     }
237   }
238 
239   // Returns the interval (in seconds) between collecting latency medians. If 0,
240   // no periodic median latencies will be collected.
GetLatencyCollectionIntervalInSeconds()241   double GetLatencyCollectionIntervalInSeconds() {
242     return median_latency_collection_interval_seconds_;
243   }
244 
GetPollCount()245   virtual int GetPollCount() {
246     // For sync client.
247     return 0;
248   }
249 
IsClosedLoop()250   bool IsClosedLoop() { return closed_loop_; }
251 
NextIssueTime(int thread_idx)252   gpr_timespec NextIssueTime(int thread_idx) {
253     const gpr_timespec result = next_time_[thread_idx];
254     next_time_[thread_idx] =
255         gpr_time_add(next_time_[thread_idx],
256                      gpr_time_from_nanos(interarrival_timer_.next(thread_idx),
257                                          GPR_TIMESPAN));
258     return result;
259   }
260 
ThreadCompleted()261   bool ThreadCompleted() {
262     return static_cast<bool>(gpr_atm_acq_load(&thread_pool_done_));
263   }
264 
265   class Thread {
266    public:
Thread(Client * client,size_t idx)267     Thread(Client* client, size_t idx)
268         : client_(client), idx_(idx), impl_(&Thread::ThreadFunc, this) {}
269 
~Thread()270     ~Thread() { impl_.join(); }
271 
BeginSwap(Histogram * n,StatusHistogram * s)272     void BeginSwap(Histogram* n, StatusHistogram* s) {
273       std::lock_guard<std::mutex> g(mu_);
274       n->Swap(&histogram_);
275       s->swap(statuses_);
276     }
277 
MergeStatsInto(Histogram * hist,StatusHistogram * s)278     void MergeStatsInto(Histogram* hist, StatusHistogram* s) {
279       std::unique_lock<std::mutex> g(mu_);
280       hist->Merge(histogram_);
281       MergeStatusHistogram(statuses_, s);
282     }
283 
GetMedianPerIntervalList()284     std::vector<double> GetMedianPerIntervalList() {
285       return medians_each_interval_list_;
286     }
287 
UpdateHistogram(HistogramEntry * entry)288     void UpdateHistogram(HistogramEntry* entry) {
289       std::lock_guard<std::mutex> g(mu_);
290       if (entry->value_used()) {
291         histogram_.Add(entry->value());
292         if (client_->GetLatencyCollectionIntervalInSeconds() > 0) {
293           histogram_per_interval_.Add(entry->value());
294           double now = UsageTimer::Now();
295           if ((now - interval_start_time_) >=
296               client_->GetLatencyCollectionIntervalInSeconds()) {
297             // Record the median latency of requests from the last interval.
298             // Divide by 1e3 to get microseconds.
299             medians_each_interval_list_.push_back(
300                 histogram_per_interval_.Percentile(50) / 1e3);
301             histogram_per_interval_.Reset();
302             interval_start_time_ = now;
303           }
304         }
305       }
306       if (entry->status_used()) {
307         statuses_[entry->status()]++;
308       }
309     }
310 
311    private:
312     Thread(const Thread&);
313     Thread& operator=(const Thread&);
314 
ThreadFunc()315     void ThreadFunc() {
316       int wait_loop = 0;
317       while (!gpr_event_wait(
318           &client_->start_requests_,
319           gpr_time_add(gpr_now(GPR_CLOCK_REALTIME),
320                        gpr_time_from_seconds(20, GPR_TIMESPAN)))) {
321         gpr_log(GPR_INFO, "%" PRIdPTR ": Waiting for benchmark to start (%d)",
322                 idx_, wait_loop);
323         wait_loop++;
324       }
325 
326       client_->ThreadFunc(idx_, this);
327       client_->CompleteThread();
328     }
329 
330     std::mutex mu_;
331     Histogram histogram_;
332     StatusHistogram statuses_;
333     Client* client_;
334     const size_t idx_;
335     std::thread impl_;
336     // The following are used only if
337     // median_latency_collection_interval_seconds_ is greater than 0
338     Histogram histogram_per_interval_;
339     std::vector<double> medians_each_interval_list_;
340     double interval_start_time_;
341   };
342 
343  protected:
344   bool closed_loop_;
345   gpr_atm thread_pool_done_;
346   double median_latency_collection_interval_seconds_;  // In seconds
347 
StartThreads(size_t num_threads)348   void StartThreads(size_t num_threads) {
349     gpr_atm_rel_store(&thread_pool_done_, static_cast<gpr_atm>(false));
350     threads_remaining_ = num_threads;
351     for (size_t i = 0; i < num_threads; i++) {
352       threads_.emplace_back(new Thread(this, i));
353     }
354   }
355 
EndThreads()356   void EndThreads() {
357     MaybeStartRequests();
358     threads_.clear();
359   }
360 
361   virtual void DestroyMultithreading() = 0;
362 
SetupLoadTest(const ClientConfig & config,size_t num_threads)363   void SetupLoadTest(const ClientConfig& config, size_t num_threads) {
364     // Set up the load distribution based on the number of threads
365     const auto& load = config.load_params();
366 
367     std::unique_ptr<RandomDistInterface> random_dist;
368     switch (load.load_case()) {
369       case LoadParams::kClosedLoop:
370         // Closed-loop doesn't use random dist at all
371         break;
372       case LoadParams::kPoisson:
373         random_dist = std::make_unique<ExpDist>(load.poisson().offered_load() /
374                                                 num_threads);
375         break;
376       default:
377         grpc_core::Crash("unreachable");
378     }
379 
380     // Set closed_loop_ based on whether or not random_dist is set
381     if (!random_dist) {
382       closed_loop_ = true;
383     } else {
384       closed_loop_ = false;
385       // set up interarrival timer according to random dist
386       interarrival_timer_.init(*random_dist, num_threads);
387       const auto now = gpr_now(GPR_CLOCK_MONOTONIC);
388       for (size_t i = 0; i < num_threads; i++) {
389         next_time_.push_back(gpr_time_add(
390             now,
391             gpr_time_from_nanos(interarrival_timer_.next(i), GPR_TIMESPAN)));
392       }
393     }
394   }
395 
NextIssuer(int thread_idx)396   std::function<gpr_timespec()> NextIssuer(int thread_idx) {
397     return closed_loop_ ? std::function<gpr_timespec()>()
398                         : std::bind(&Client::NextIssueTime, this, thread_idx);
399   }
400 
401   virtual void ThreadFunc(size_t thread_idx, Client::Thread* t) = 0;
402 
403   std::vector<std::unique_ptr<Thread>> threads_;
404   std::unique_ptr<UsageTimer> timer_;
405 
406   InterarrivalTimer interarrival_timer_;
407   std::vector<gpr_timespec> next_time_;
408 
409   std::mutex thread_completion_mu_;
410   size_t threads_remaining_;
411   std::condition_variable threads_complete_;
412 
413   gpr_event start_requests_;
414   bool started_requests_;
415 
416   int last_reset_poll_count_;
417 
MaybeStartRequests()418   void MaybeStartRequests() {
419     if (!started_requests_) {
420       started_requests_ = true;
421       gpr_event_set(&start_requests_, reinterpret_cast<void*>(1));
422     }
423   }
424 
CompleteThread()425   void CompleteThread() {
426     std::lock_guard<std::mutex> g(thread_completion_mu_);
427     threads_remaining_--;
428     if (threads_remaining_ == 0) {
429       threads_complete_.notify_all();
430     }
431   }
432 };
433 
434 template <class StubType, class RequestType>
435 class ClientImpl : public Client {
436  public:
ClientImpl(const ClientConfig & config,std::function<std::unique_ptr<StubType> (std::shared_ptr<Channel>)> create_stub)437   ClientImpl(const ClientConfig& config,
438              std::function<std::unique_ptr<StubType>(std::shared_ptr<Channel>)>
439                  create_stub)
440       : cores_(gpr_cpu_num_cores()), create_stub_(create_stub) {
441     for (int i = 0; i < config.client_channels(); i++) {
442       channels_.emplace_back(
443           config.server_targets(i % config.server_targets_size()), config,
444           create_stub_, i);
445     }
446     WaitForChannelsToConnect();
447     median_latency_collection_interval_seconds_ =
448         config.median_latency_collection_interval_millis() / 1e3;
449     ClientRequestCreator<RequestType> create_req(&request_,
450                                                  config.payload_config());
451   }
~ClientImpl()452   ~ClientImpl() override {}
request()453   const RequestType* request() { return &request_; }
454 
WaitForChannelsToConnect()455   void WaitForChannelsToConnect() {
456     int connect_deadline_seconds = 10;
457     // Allow optionally overriding connect_deadline in order
458     // to deal with benchmark environments in which the server
459     // can take a long time to become ready.
460     auto channel_connect_timeout_str =
461         grpc_core::GetEnv("QPS_WORKER_CHANNEL_CONNECT_TIMEOUT");
462     if (channel_connect_timeout_str.has_value() &&
463         !channel_connect_timeout_str->empty()) {
464       connect_deadline_seconds = atoi(channel_connect_timeout_str->c_str());
465     }
466     gpr_log(GPR_INFO,
467             "Waiting for up to %d seconds for all channels to connect",
468             connect_deadline_seconds);
469     gpr_timespec connect_deadline = gpr_time_add(
470         gpr_now(GPR_CLOCK_REALTIME),
471         gpr_time_from_seconds(connect_deadline_seconds, GPR_TIMESPAN));
472     CompletionQueue cq;
473     size_t num_remaining = 0;
474     for (auto& c : channels_) {
475       if (!c.is_inproc()) {
476         Channel* channel = c.get_channel();
477         grpc_connectivity_state last_observed = channel->GetState(true);
478         if (last_observed == GRPC_CHANNEL_READY) {
479           gpr_log(GPR_INFO, "Channel %p connected!", channel);
480         } else {
481           num_remaining++;
482           channel->NotifyOnStateChange(last_observed, connect_deadline, &cq,
483                                        channel);
484         }
485       }
486     }
487     while (num_remaining > 0) {
488       bool ok = false;
489       void* tag = nullptr;
490       cq.Next(&tag, &ok);
491       Channel* channel = static_cast<Channel*>(tag);
492       if (!ok) {
493         grpc_core::Crash(absl::StrFormat(
494             "Channel %p failed to connect within the deadline", channel));
495       } else {
496         grpc_connectivity_state last_observed = channel->GetState(true);
497         if (last_observed == GRPC_CHANNEL_READY) {
498           gpr_log(GPR_INFO, "Channel %p connected!", channel);
499           num_remaining--;
500         } else {
501           channel->NotifyOnStateChange(last_observed, connect_deadline, &cq,
502                                        channel);
503         }
504       }
505     }
506   }
507 
508  protected:
509   const int cores_;
510   RequestType request_;
511 
512   class ClientChannelInfo {
513    public:
ClientChannelInfo(const std::string & target,const ClientConfig & config,std::function<std::unique_ptr<StubType> (std::shared_ptr<Channel>)> create_stub,int shard)514     ClientChannelInfo(
515         const std::string& target, const ClientConfig& config,
516         std::function<std::unique_ptr<StubType>(std::shared_ptr<Channel>)>
517             create_stub,
518         int shard) {
519       ChannelArguments args;
520       args.SetInt("shard_to_ensure_no_subchannel_merges", shard);
521       set_channel_args(config, &args);
522 
523       std::string type;
524       if (config.has_security_params() &&
525           config.security_params().cred_type().empty()) {
526         type = kTlsCredentialsType;
527       } else {
528         type = config.security_params().cred_type();
529       }
530 
531       std::string inproc_pfx(INPROC_NAME_PREFIX);
532       if (!absl::StartsWith(target, inproc_pfx)) {
533         channel_ = CreateTestChannel(
534             target, type, config.security_params().server_host_override(),
535             !config.security_params().use_test_ca(),
536             std::shared_ptr<CallCredentials>(), args);
537         gpr_log(GPR_INFO, "Connecting to %s", target.c_str());
538         is_inproc_ = false;
539       } else {
540         std::string tgt = target;
541         tgt.erase(0, inproc_pfx.length());
542         int srv_num = std::stoi(tgt);
543         channel_ = (*g_inproc_servers)[srv_num]->InProcessChannel(args);
544         is_inproc_ = true;
545       }
546       stub_ = create_stub(channel_);
547     }
get_channel()548     Channel* get_channel() { return channel_.get(); }
get_stub()549     StubType* get_stub() { return stub_.get(); }
is_inproc()550     bool is_inproc() { return is_inproc_; }
551 
552    private:
set_channel_args(const ClientConfig & config,ChannelArguments * args)553     void set_channel_args(const ClientConfig& config, ChannelArguments* args) {
554       for (const auto& channel_arg : config.channel_args()) {
555         if (channel_arg.value_case() == ChannelArg::kStrValue) {
556           args->SetString(channel_arg.name(), channel_arg.str_value());
557         } else if (channel_arg.value_case() == ChannelArg::kIntValue) {
558           args->SetInt(channel_arg.name(), channel_arg.int_value());
559         } else {
560           gpr_log(GPR_ERROR, "Empty channel arg value.");
561         }
562       }
563     }
564 
565     std::shared_ptr<Channel> channel_;
566     std::unique_ptr<StubType> stub_;
567     bool is_inproc_;
568   };
569   std::vector<ClientChannelInfo> channels_;
570   std::function<std::unique_ptr<StubType>(const std::shared_ptr<Channel>&)>
571       create_stub_;
572 };
573 
574 std::unique_ptr<Client> CreateSynchronousClient(const ClientConfig& config);
575 std::unique_ptr<Client> CreateAsyncClient(const ClientConfig& config);
576 std::unique_ptr<Client> CreateCallbackClient(const ClientConfig& config);
577 std::unique_ptr<Client> CreateGenericAsyncStreamingClient(
578     const ClientConfig& config);
579 
580 }  // namespace testing
581 }  // namespace grpc
582 
583 #endif  // GRPC_TEST_CPP_QPS_CLIENT_H
584