client.h 19 KB

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