aboutsummaryrefslogtreecommitdiffhomepage
path: root/test/cpp/qps/driver.cc
blob: a54aad5631f5680a7839561623c09c778a6da9e5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
/*
 *
 * Copyright 2015, Google Inc.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are
 * met:
 *
 *     * Redistributions of source code must retain the above copyright
 * notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above
 * copyright notice, this list of conditions and the following disclaimer
 * in the documentation and/or other materials provided with the
 * distribution.
 *     * Neither the name of Google Inc. nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 */

#include "test/cpp/qps/driver.h"
#include "src/core/support/env.h"
#include <grpc/support/alloc.h>
#include <grpc/support/log.h>
#include <grpc/support/host_port.h>
#include <grpc++/channel_arguments.h>
#include <grpc++/client_context.h>
#include <grpc++/create_channel.h>
#include <grpc++/stream.h>
#include <list>
#include <thread>
#include <vector>

using std::list;
using std::thread;
using std::unique_ptr;
using std::vector;
using grpc::string;
using grpc::ChannelArguments;
using grpc::ClientContext;
using grpc::ClientReaderWriter;
using grpc::CreateChannelDeprecated;
using grpc::Status;
using grpc::testing::ClientArgs;
using grpc::testing::ClientConfig;
using grpc::testing::ClientResult;
using grpc::testing::Worker;
using grpc::testing::ServerArgs;
using grpc::testing::ServerConfig;
using grpc::testing::ServerStatus;

#if 0
static vector<string> get_hosts(const string& name) {
  char* env = gpr_getenv(name.c_str());
  if (!env) return vector<string>();

  vector<string> out;
  char* p = env;
  for (;;) {
  	char* comma = strchr(p, ',');
  	if (comma) {
  	  out.emplace_back(p, comma);
  	  p = comma + 1;
  	} else {
  	  out.emplace_back(p);
  	  gpr_free(env);
  	  return out;
  	}
  }
}

void RunScenario(const ClientConfig& client_config, size_t num_clients,
                 const ServerConfig& server_config, size_t num_servers) {
  // ClientContext allocator (all are destroyed at scope exit)
  list<ClientContext> contexts;
  auto alloc_context = [&contexts]() {
  	contexts.emplace_back();
  	return &contexts.back();
  };

  // Get client, server lists
  auto workers = get_hosts("QPS_WORKERS");

  GPR_ASSERT(clients.size() >= num_clients);
  GPR_ASSERT(servers.size() >= num_servers);

  // Trim to just what we need
  clients.resize(num_clients);
  servers.resize(num_servers);

  // Start servers
  vector<unique_ptr<QpsServer::Stub>> server_stubs;
  vector<unique_ptr<ClientReaderWriter<ServerArgs, ServerStatus>>> server_streams;
  vector<string> server_targets;
  for (const auto& target : servers) {
  	server_stubs.push_back(QpsServer::NewStub(CreateChannelDeprecated(target, ChannelArguments())));
  	auto* stub = server_stubs.back().get();
  	ServerArgs args;
  	*args.mutable_config() = server_config;
  	server_streams.push_back(stub->RunServer(alloc_context()));
  	auto* stream = server_streams.back().get();
  	if (!stream->Write(args)) {
  	  gpr_log(GPR_ERROR, "Failed starting server");
  	  return;
  	}
  	ServerStatus init_status;
  	if (!stream->Read(&init_status)) {
  	  gpr_log(GPR_ERROR, "Failed starting server");
  	  return;
  	}
  	char* host;
  	char* driver_port;
  	char* cli_target;
  	gpr_split_host_port(target.c_str(), &host, &driver_port);
  	gpr_join_host_port(&cli_target, host, init_status.port());
  	server_targets.push_back(cli_target);
  	gpr_free(host);
  	gpr_free(driver_port);
  	gpr_free(cli_target);
  }

  // Start clients
  class Client {
   public:
   	Client(ClientContext* ctx, const string& target, const ClientArgs& args)
   	  : thread_([ctx, target, args, this]() {
   	  	auto stub = QpsClient::NewStub(CreateChannelDeprecated(target, ChannelArguments()));
   	  	status_ = stub->RunTest(ctx, args, &result_);
   	  }) {}

   	~Client() { join(); }

   	void join() { if (!joined_) { thread_.join(); joined_ = true; } }

   	const Status& status() const { return status_; }
   	const ClientResult& result() const { return result_; }

   private:
   	bool joined_ = false;
   	Status status_;
   	ClientResult result_;
   	thread thread_;
  };
  list<Client> running_clients;
  size_t svr_idx = 0;
  for (const auto& target : clients) {
  	ClientArgs args;
  	*args.mutable_config() = client_config;
  	for (size_t i = 0; i < num_servers; i++) {
  	  args.add_server_targets(server_targets[svr_idx]);
  	  svr_idx = (svr_idx + 1) % num_servers;
  	}

  	running_clients.emplace_back(alloc_context(), target, args);
  }

  // Finish clients
  for (auto& client : running_clients) {
  	client.join();
  	if (!client.status().IsOk()) {
  	  gpr_log(GPR_ERROR, "Client failed");
  	  return;
  	}
  }

  // Finish servers
  for (auto& stream : server_streams) {
  	ServerStatus final_status;
  	ServerStatus dummy;
  	if (!stream->WritesDone() || !stream->Read(&final_status) || stream->Read(&dummy) || !stream->Finish().IsOk()) {
  	  gpr_log(GPR_ERROR, "Server protocol error");
  	}
  }
}
#endif