• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //
2 //
3 // Copyright 2020 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 #include <grpcpp/ext/admin_services.h>
20 #include <grpcpp/ext/csm_observability.h>
21 #include <grpcpp/ext/proto_server_reflection_plugin.h>
22 #include <grpcpp/grpcpp.h>
23 #include <grpcpp/server.h>
24 #include <grpcpp/server_builder.h>
25 #include <grpcpp/server_context.h>
26 
27 #include <atomic>
28 #include <chrono>
29 #include <condition_variable>
30 #include <deque>
31 #include <map>
32 #include <memory>
33 #include <mutex>
34 #include <set>
35 #include <sstream>
36 #include <string>
37 #include <thread>
38 #include <utility>
39 #include <vector>
40 
41 #include "absl/algorithm/container.h"
42 #include "absl/flags/flag.h"
43 #include "absl/log/check.h"
44 #include "absl/log/log.h"
45 #include "absl/strings/str_split.h"
46 #include "opentelemetry/exporters/prometheus/exporter_factory.h"
47 #include "opentelemetry/exporters/prometheus/exporter_options.h"
48 #include "opentelemetry/sdk/metrics/meter_provider.h"
49 #include "src/core/lib/channel/status_util.h"
50 #include "src/core/util/env.h"
51 #include "src/proto/grpc/testing/empty.pb.h"
52 #include "src/proto/grpc/testing/messages.pb.h"
53 #include "src/proto/grpc/testing/test.grpc.pb.h"
54 #include "test/core/test_util/test_config.h"
55 #include "test/cpp/interop/rpc_behavior_lb_policy.h"
56 #include "test/cpp/interop/xds_stats_watcher.h"
57 #include "test/cpp/util/test_config.h"
58 
59 ABSL_FLAG(bool, fail_on_failed_rpc, false,
60           "Fail client if any RPCs fail after first successful RPC.");
61 ABSL_FLAG(int32_t, num_channels, 1, "Number of channels.");
62 ABSL_FLAG(bool, print_response, false, "Write RPC response to stdout.");
63 ABSL_FLAG(int32_t, qps, 1, "Qps per channel.");
64 // TODO(Capstan): Consider using absl::Duration
65 ABSL_FLAG(int32_t, rpc_timeout_sec, 30, "Per RPC timeout seconds.");
66 ABSL_FLAG(std::string, server, "localhost:50051", "Address of server.");
67 ABSL_FLAG(int32_t, stats_port, 50052,
68           "Port to expose peer distribution stats service.");
69 ABSL_FLAG(std::string, rpc, "UnaryCall",
70           "a comma separated list of rpc methods.");
71 ABSL_FLAG(int32_t, request_payload_size, 0,
72           "Set the SimpleRequest.payload.body to a string of repeated 0 (zero) "
73           "ASCII characters of the given size in bytes.");
74 ABSL_FLAG(int32_t, response_payload_size, 0,
75           "Ask the server to respond with SimpleResponse.payload.body of the "
76           "given length (may not be implemented on the server).");
77 ABSL_FLAG(std::string, metadata, "", "metadata to send with the RPC.");
78 ABSL_FLAG(std::string, expect_status, "OK",
79           "RPC status for the test RPC to be considered successful");
80 ABSL_FLAG(
81     bool, secure_mode, false,
82     "If true, XdsCredentials are used, InsecureChannelCredentials otherwise");
83 ABSL_FLAG(bool, enable_csm_observability, false,
84           "Whether to enable CSM Observability");
85 ABSL_FLAG(bool, log_rpc_start_and_end, false,
86           "Whether to log when RPCs start and end.");
87 
88 using grpc::Channel;
89 using grpc::ClientAsyncResponseReader;
90 using grpc::ClientContext;
91 using grpc::CompletionQueue;
92 using grpc::Server;
93 using grpc::ServerBuilder;
94 using grpc::ServerContext;
95 using grpc::Status;
96 using grpc::testing::AsyncClientCallResult;
97 using grpc::testing::ClientConfigureRequest;
98 using grpc::testing::ClientConfigureResponse;
99 using grpc::testing::Empty;
100 using grpc::testing::LoadBalancerAccumulatedStatsRequest;
101 using grpc::testing::LoadBalancerAccumulatedStatsResponse;
102 using grpc::testing::LoadBalancerStatsRequest;
103 using grpc::testing::LoadBalancerStatsResponse;
104 using grpc::testing::LoadBalancerStatsService;
105 using grpc::testing::SimpleRequest;
106 using grpc::testing::SimpleResponse;
107 using grpc::testing::StatsWatchers;
108 using grpc::testing::TestService;
109 using grpc::testing::XdsStatsWatcher;
110 using grpc::testing::XdsUpdateClientConfigureService;
111 
112 struct AsyncClientCall {
113   ClientContext context;
114   std::unique_ptr<ClientAsyncResponseReader<Empty>> empty_response_reader;
115   std::unique_ptr<ClientAsyncResponseReader<SimpleResponse>>
116       simple_response_reader;
117 
118   AsyncClientCallResult result;
119 };
120 
121 // Whether at least one RPC has succeeded, indicating xDS resolution
122 // completed.
123 std::atomic<bool> one_rpc_succeeded(false);
124 // RPC configuration detailing how RPC should be sent.
125 struct RpcConfig {
126   ClientConfigureRequest::RpcType type;
127   std::vector<std::pair<std::string, std::string>> metadata;
128   int timeout_sec = 0;
129   std::string request_payload;
130   int request_payload_size = 0;
131   int response_payload_size = 0;
132 };
133 struct RpcConfigurationsQueue {
134   // A queue of RPC configurations detailing how RPCs should be sent.
135   std::deque<std::vector<RpcConfig>> rpc_configs_queue;
136   // Mutex for rpc_configs_queue
137   std::mutex mu_rpc_configs_queue;
138 };
139 
140 class TestClient {
141  public:
TestClient(const std::shared_ptr<Channel> & channel,StatsWatchers * stats_watchers)142   TestClient(const std::shared_ptr<Channel>& channel,
143              StatsWatchers* stats_watchers)
144       : stub_(TestService::NewStub(channel)), stats_watchers_(stats_watchers) {}
145 
AsyncUnaryCall(const RpcConfig & config)146   void AsyncUnaryCall(const RpcConfig& config) {
147     SimpleResponse response;
148     int saved_request_id;
149     {
150       std::lock_guard<std::mutex> lock(stats_watchers_->mu);
151       saved_request_id = ++stats_watchers_->global_request_id;
152       ++stats_watchers_
153             ->global_request_id_by_type[ClientConfigureRequest::UNARY_CALL];
154     }
155     std::chrono::system_clock::time_point deadline =
156         std::chrono::system_clock::now() +
157         std::chrono::seconds(config.timeout_sec != 0
158                                  ? config.timeout_sec
159                                  : absl::GetFlag(FLAGS_rpc_timeout_sec));
160     AsyncClientCall* call = new AsyncClientCall;
161     if (absl::GetFlag(FLAGS_log_rpc_start_and_end)) {
162       LOG(INFO) << "starting async unary call " << static_cast<void*>(call);
163     }
164     for (const auto& data : config.metadata) {
165       call->context.AddMetadata(data.first, data.second);
166     }
167     SimpleRequest request;
168     request.set_response_size(config.response_payload_size);
169     if (config.request_payload_size > 0) {
170       request.mutable_payload()->set_body(config.request_payload.c_str(),
171                                           config.request_payload_size);
172     }
173     call->context.set_deadline(deadline);
174     call->result.saved_request_id = saved_request_id;
175     call->result.rpc_type = ClientConfigureRequest::UNARY_CALL;
176     call->simple_response_reader =
177         stub_->PrepareAsyncUnaryCall(&call->context, request, &cq_);
178     call->simple_response_reader->StartCall();
179     call->simple_response_reader->Finish(&call->result.simple_response,
180                                          &call->result.status, call);
181   }
182 
AsyncEmptyCall(const RpcConfig & config)183   void AsyncEmptyCall(const RpcConfig& config) {
184     Empty response;
185     int saved_request_id;
186     {
187       std::lock_guard<std::mutex> lock(stats_watchers_->mu);
188       saved_request_id = ++stats_watchers_->global_request_id;
189       ++stats_watchers_
190             ->global_request_id_by_type[ClientConfigureRequest::EMPTY_CALL];
191     }
192     std::chrono::system_clock::time_point deadline =
193         std::chrono::system_clock::now() +
194         std::chrono::seconds(config.timeout_sec != 0
195                                  ? config.timeout_sec
196                                  : absl::GetFlag(FLAGS_rpc_timeout_sec));
197     AsyncClientCall* call = new AsyncClientCall;
198     if (absl::GetFlag(FLAGS_log_rpc_start_and_end)) {
199       LOG(INFO) << "starting async empty call " << static_cast<void*>(call);
200     }
201     for (const auto& data : config.metadata) {
202       call->context.AddMetadata(data.first, data.second);
203     }
204     call->context.set_deadline(deadline);
205     call->result.saved_request_id = saved_request_id;
206     call->result.rpc_type = ClientConfigureRequest::EMPTY_CALL;
207     call->empty_response_reader = stub_->PrepareAsyncEmptyCall(
208         &call->context, Empty::default_instance(), &cq_);
209     call->empty_response_reader->StartCall();
210     call->empty_response_reader->Finish(&call->result.empty_response,
211                                         &call->result.status, call);
212   }
213 
AsyncCompleteRpc()214   void AsyncCompleteRpc() {
215     void* got_tag;
216     bool ok = false;
217     while (cq_.Next(&got_tag, &ok)) {
218       AsyncClientCall* call = static_cast<AsyncClientCall*>(got_tag);
219       CHECK(ok);
220       if (absl::GetFlag(FLAGS_log_rpc_start_and_end)) {
221         LOG(INFO) << "completed async call " << static_cast<void*>(call);
222       }
223       {
224         std::lock_guard<std::mutex> lock(stats_watchers_->mu);
225         auto server_initial_metadata = call->context.GetServerInitialMetadata();
226         auto metadata_hostname =
227             call->context.GetServerInitialMetadata().find("hostname");
228         std::string hostname =
229             metadata_hostname != call->context.GetServerInitialMetadata().end()
230                 ? std::string(metadata_hostname->second.data(),
231                               metadata_hostname->second.length())
232                 : call->result.simple_response.hostname();
233         for (auto watcher : stats_watchers_->watchers) {
234           watcher->RpcCompleted(call->result, hostname,
235                                 call->context.GetServerInitialMetadata(),
236                                 call->context.GetServerTrailingMetadata());
237         }
238       }
239 
240       if (!RpcStatusCheckSuccess(call)) {
241         if (absl::GetFlag(FLAGS_print_response) ||
242             absl::GetFlag(FLAGS_fail_on_failed_rpc)) {
243           std::cout << "RPC failed: " << call->result.status.error_code()
244                     << ": " << call->result.status.error_message() << std::endl;
245         }
246         if (absl::GetFlag(FLAGS_fail_on_failed_rpc) &&
247             one_rpc_succeeded.load()) {
248           abort();
249         }
250       } else {
251         if (absl::GetFlag(FLAGS_print_response)) {
252           auto metadata_hostname =
253               call->context.GetServerInitialMetadata().find("hostname");
254           std::string hostname =
255               metadata_hostname !=
256                       call->context.GetServerInitialMetadata().end()
257                   ? std::string(metadata_hostname->second.data(),
258                                 metadata_hostname->second.length())
259                   : call->result.simple_response.hostname();
260           std::cout << "Greeting: Hello world, this is " << hostname
261                     << ", from " << call->context.peer() << std::endl;
262         }
263         one_rpc_succeeded = true;
264       }
265 
266       delete call;
267     }
268   }
269 
270  private:
RpcStatusCheckSuccess(AsyncClientCall * call)271   static bool RpcStatusCheckSuccess(AsyncClientCall* call) {
272     // Determine RPC success based on expected status.
273     grpc_status_code code;
274     CHECK(grpc_status_code_from_string(
275         absl::GetFlag(FLAGS_expect_status).c_str(), &code));
276     return code ==
277            static_cast<grpc_status_code>(call->result.status.error_code());
278   }
279 
280   std::unique_ptr<TestService::Stub> stub_;
281   StatsWatchers* stats_watchers_;
282   CompletionQueue cq_;
283 };
284 
285 class LoadBalancerStatsServiceImpl : public LoadBalancerStatsService::Service {
286  public:
LoadBalancerStatsServiceImpl(StatsWatchers * stats_watchers)287   explicit LoadBalancerStatsServiceImpl(StatsWatchers* stats_watchers)
288       : stats_watchers_(stats_watchers) {}
289 
GetClientStats(ServerContext *,const LoadBalancerStatsRequest * request,LoadBalancerStatsResponse * response)290   Status GetClientStats(ServerContext* /*context*/,
291                         const LoadBalancerStatsRequest* request,
292                         LoadBalancerStatsResponse* response) override {
293     int start_id;
294     int end_id;
295     std::unique_ptr<XdsStatsWatcher> watcher;
296     {
297       std::lock_guard<std::mutex> lock(stats_watchers_->mu);
298       start_id = stats_watchers_->global_request_id + 1;
299       end_id = start_id + request->num_rpcs();
300       watcher = std::make_unique<XdsStatsWatcher>(
301           start_id, end_id,
302           std::vector<std::string>(request->metadata_keys().begin(),
303                                    request->metadata_keys().end()));
304       stats_watchers_->watchers.insert(watcher.get());
305     }
306     *response = watcher->WaitForRpcStatsResponse(request->timeout_sec());
307     {
308       std::lock_guard<std::mutex> lock(stats_watchers_->mu);
309       stats_watchers_->watchers.erase(watcher.get());
310     }
311     return Status::OK;
312   }
313 
GetClientAccumulatedStats(ServerContext *,const LoadBalancerAccumulatedStatsRequest *,LoadBalancerAccumulatedStatsResponse * response)314   Status GetClientAccumulatedStats(
315       ServerContext* /*context*/,
316       const LoadBalancerAccumulatedStatsRequest* /*request*/,
317       LoadBalancerAccumulatedStatsResponse* response) override {
318     std::lock_guard<std::mutex> lock(stats_watchers_->mu);
319     stats_watchers_->global_watcher->GetCurrentRpcStats(response,
320                                                         stats_watchers_);
321     return Status::OK;
322   }
323 
324  private:
325   StatsWatchers* stats_watchers_;
326 };
327 
328 class XdsUpdateClientConfigureServiceImpl
329     : public XdsUpdateClientConfigureService::Service {
330  public:
XdsUpdateClientConfigureServiceImpl(RpcConfigurationsQueue * rpc_configs_queue)331   explicit XdsUpdateClientConfigureServiceImpl(
332       RpcConfigurationsQueue* rpc_configs_queue)
333       : rpc_configs_queue_(rpc_configs_queue) {}
334 
Configure(ServerContext *,const ClientConfigureRequest * request,ClientConfigureResponse *)335   Status Configure(ServerContext* /*context*/,
336                    const ClientConfigureRequest* request,
337                    ClientConfigureResponse* /*response*/) override {
338     std::map<int, std::vector<std::pair<std::string, std::string>>>
339         metadata_map;
340     for (const auto& data : request->metadata()) {
341       metadata_map[data.type()].push_back({data.key(), data.value()});
342     }
343     std::vector<RpcConfig> configs;
344     int request_payload_size = absl::GetFlag(FLAGS_request_payload_size);
345     int response_payload_size = absl::GetFlag(FLAGS_response_payload_size);
346     CHECK_GE(request_payload_size, 0);
347     CHECK_GE(response_payload_size, 0);
348     for (const auto& rpc : request->types()) {
349       RpcConfig config;
350       config.timeout_sec = request->timeout_sec();
351       config.type = static_cast<ClientConfigureRequest::RpcType>(rpc);
352       auto metadata_iter = metadata_map.find(rpc);
353       if (metadata_iter != metadata_map.end()) {
354         config.metadata = metadata_iter->second;
355       }
356       if (request_payload_size > 0 &&
357           config.type == ClientConfigureRequest::EMPTY_CALL) {
358         LOG(ERROR) << "request_payload_size should not be set for EMPTY_CALL";
359       }
360       if (response_payload_size > 0 &&
361           config.type == ClientConfigureRequest::EMPTY_CALL) {
362         LOG(ERROR) << "response_payload_size should not be set for EMPTY_CALL";
363       }
364       config.request_payload_size = request_payload_size;
365       std::string payload(config.request_payload_size, '0');
366       config.request_payload = payload;
367       config.response_payload_size = response_payload_size;
368       configs.push_back(std::move(config));
369     }
370     {
371       std::lock_guard<std::mutex> lock(
372           rpc_configs_queue_->mu_rpc_configs_queue);
373       rpc_configs_queue_->rpc_configs_queue.emplace_back(std::move(configs));
374     }
375     return Status::OK;
376   }
377 
378  private:
379   RpcConfigurationsQueue* rpc_configs_queue_;
380 };
381 
RunTestLoop(std::chrono::duration<double> duration_per_query,StatsWatchers * stats_watchers,RpcConfigurationsQueue * rpc_configs_queue)382 void RunTestLoop(std::chrono::duration<double> duration_per_query,
383                  StatsWatchers* stats_watchers,
384                  RpcConfigurationsQueue* rpc_configs_queue) {
385   grpc::ChannelArguments channel_args;
386   channel_args.SetInt(GRPC_ARG_ENABLE_RETRIES, 1);
387   TestClient client(
388       grpc::CreateCustomChannel(
389           absl::GetFlag(FLAGS_server),
390           absl::GetFlag(FLAGS_secure_mode)
391               ? grpc::XdsCredentials(grpc::InsecureChannelCredentials())
392               : grpc::InsecureChannelCredentials(),
393           channel_args),
394       stats_watchers);
395   std::chrono::time_point<std::chrono::system_clock> start =
396       std::chrono::system_clock::now();
397   std::chrono::duration<double> elapsed;
398 
399   std::thread thread = std::thread(&TestClient::AsyncCompleteRpc, &client);
400 
401   std::vector<RpcConfig> configs;
402   while (true) {
403     {
404       std::lock_guard<std::mutex> lock(rpc_configs_queue->mu_rpc_configs_queue);
405       if (!rpc_configs_queue->rpc_configs_queue.empty()) {
406         configs = std::move(rpc_configs_queue->rpc_configs_queue.front());
407         rpc_configs_queue->rpc_configs_queue.pop_front();
408       }
409     }
410 
411     elapsed = std::chrono::system_clock::now() - start;
412     if (elapsed > duration_per_query) {
413       start = std::chrono::system_clock::now();
414       for (const auto& config : configs) {
415         if (config.type == ClientConfigureRequest::EMPTY_CALL) {
416           client.AsyncEmptyCall(config);
417         } else if (config.type == ClientConfigureRequest::UNARY_CALL) {
418           client.AsyncUnaryCall(config);
419         } else {
420           CHECK(0);
421         }
422       }
423     }
424   }
425   GPR_UNREACHABLE_CODE(thread.join());
426 }
427 
EnableCsmObservability()428 grpc::CsmObservability EnableCsmObservability() {
429   VLOG(2) << "Registering Prometheus exporter";
430   opentelemetry::exporter::metrics::PrometheusExporterOptions opts;
431   // default was "localhost:9464" which causes connection issue across GKE
432   // pods
433   opts.url = "0.0.0.0:9464";
434   opts.without_otel_scope = false;
435   auto prometheus_exporter =
436       opentelemetry::exporter::metrics::PrometheusExporterFactory::Create(opts);
437   auto meter_provider =
438       std::make_shared<opentelemetry::sdk::metrics::MeterProvider>();
439   meter_provider->AddMetricReader(std::move(prometheus_exporter));
440   auto observability = grpc::CsmObservabilityBuilder()
441                            .SetMeterProvider(std::move(meter_provider))
442                            .BuildAndRegister();
443   assert(observability.ok());
444   return *std::move(observability);
445 }
446 
RunServer(const int port,StatsWatchers * stats_watchers,RpcConfigurationsQueue * rpc_configs_queue)447 void RunServer(const int port, StatsWatchers* stats_watchers,
448                RpcConfigurationsQueue* rpc_configs_queue) {
449   CHECK_NE(port, 0);
450   std::ostringstream server_address;
451   server_address << "0.0.0.0:" << port;
452 
453   LoadBalancerStatsServiceImpl stats_service(stats_watchers);
454   XdsUpdateClientConfigureServiceImpl client_config_service(rpc_configs_queue);
455 
456   grpc::reflection::InitProtoReflectionServerBuilderPlugin();
457   ServerBuilder builder;
458   builder.RegisterService(&stats_service);
459   builder.RegisterService(&client_config_service);
460   grpc::AddAdminServices(&builder);
461   builder.AddListeningPort(server_address.str(),
462                            grpc::InsecureServerCredentials());
463   std::unique_ptr<Server> server(builder.BuildAndStart());
464   VLOG(2) << "Server listening on " << server_address.str();
465 
466   server->Wait();
467 }
468 
BuildRpcConfigsFromFlags(RpcConfigurationsQueue * rpc_configs_queue)469 void BuildRpcConfigsFromFlags(RpcConfigurationsQueue* rpc_configs_queue) {
470   // Store Metadata like
471   // "EmptyCall:key1:value1,UnaryCall:key1:value1,UnaryCall:key2:value2" into a
472   // map where the key is the RPC method and value is a vector of key:value
473   // pairs. {EmptyCall, [{key1,value1}],
474   //  UnaryCall, [{key1,value1}, {key2,value2}]}
475   std::vector<std::string> rpc_metadata =
476       absl::StrSplit(absl::GetFlag(FLAGS_metadata), ',', absl::SkipEmpty());
477   std::map<int, std::vector<std::pair<std::string, std::string>>> metadata_map;
478   for (auto& data : rpc_metadata) {
479     std::vector<std::string> metadata =
480         absl::StrSplit(data, ':', absl::SkipEmpty());
481     CHECK_EQ(metadata.size(), 3u);
482     if (metadata[0] == "EmptyCall") {
483       metadata_map[ClientConfigureRequest::EMPTY_CALL].push_back(
484           {metadata[1], metadata[2]});
485     } else if (metadata[0] == "UnaryCall") {
486       metadata_map[ClientConfigureRequest::UNARY_CALL].push_back(
487           {metadata[1], metadata[2]});
488     } else {
489       CHECK(0);
490     }
491   }
492   std::vector<RpcConfig> configs;
493   std::vector<std::string> rpc_methods =
494       absl::StrSplit(absl::GetFlag(FLAGS_rpc), ',', absl::SkipEmpty());
495   int request_payload_size = absl::GetFlag(FLAGS_request_payload_size);
496   int response_payload_size = absl::GetFlag(FLAGS_response_payload_size);
497   CHECK_GE(request_payload_size, 0);
498   CHECK_GE(response_payload_size, 0);
499   for (const std::string& rpc_method : rpc_methods) {
500     RpcConfig config;
501     if (rpc_method == "EmptyCall") {
502       config.type = ClientConfigureRequest::EMPTY_CALL;
503     } else if (rpc_method == "UnaryCall") {
504       config.type = ClientConfigureRequest::UNARY_CALL;
505     } else {
506       CHECK(0);
507     }
508     auto metadata_iter = metadata_map.find(config.type);
509     if (metadata_iter != metadata_map.end()) {
510       config.metadata = metadata_iter->second;
511     }
512     if (request_payload_size > 0 &&
513         config.type == ClientConfigureRequest::EMPTY_CALL) {
514       LOG(ERROR) << "request_payload_size should not be set for EMPTY_CALL";
515     }
516     if (response_payload_size > 0 &&
517         config.type == ClientConfigureRequest::EMPTY_CALL) {
518       LOG(ERROR) << "response_payload_size should not be set for EMPTY_CALL";
519     }
520     config.request_payload_size = request_payload_size;
521     std::string payload(config.request_payload_size, '0');
522     config.request_payload = payload;
523     config.response_payload_size = response_payload_size;
524     configs.push_back(std::move(config));
525   }
526   {
527     std::lock_guard<std::mutex> lock(rpc_configs_queue->mu_rpc_configs_queue);
528     rpc_configs_queue->rpc_configs_queue.emplace_back(std::move(configs));
529   }
530 }
531 
main(int argc,char ** argv)532 int main(int argc, char** argv) {
533   grpc_core::CoreConfiguration::RegisterBuilder(
534       grpc::testing::RegisterRpcBehaviorLbPolicy);
535   grpc::testing::TestEnvironment env(&argc, argv);
536   grpc::testing::InitTest(&argc, &argv, true);
537   // Validate the expect_status flag.
538   grpc_status_code code;
539   CHECK(grpc_status_code_from_string(absl::GetFlag(FLAGS_expect_status).c_str(),
540                                      &code));
541   StatsWatchers stats_watchers;
542   RpcConfigurationsQueue rpc_config_queue;
543 
544   {
545     std::lock_guard<std::mutex> lock(stats_watchers.mu);
546     stats_watchers.global_watcher = new XdsStatsWatcher(0, 0, {});
547     stats_watchers.watchers.insert(stats_watchers.global_watcher);
548   }
549 
550   BuildRpcConfigsFromFlags(&rpc_config_queue);
551   grpc::CsmObservability observability;
552   if (absl::GetFlag(FLAGS_enable_csm_observability)) {
553     observability = EnableCsmObservability();
554   }
555 
556   std::chrono::duration<double> duration_per_query =
557       std::chrono::nanoseconds(std::chrono::seconds(1)) /
558       absl::GetFlag(FLAGS_qps);
559 
560   std::vector<std::thread> test_threads;
561   test_threads.reserve(absl::GetFlag(FLAGS_num_channels));
562   for (int i = 0; i < absl::GetFlag(FLAGS_num_channels); i++) {
563     test_threads.emplace_back(std::thread(&RunTestLoop, duration_per_query,
564                                           &stats_watchers, &rpc_config_queue));
565   }
566 
567   RunServer(absl::GetFlag(FLAGS_stats_port), &stats_watchers,
568             &rpc_config_queue);
569 
570   for (auto it = test_threads.begin(); it != test_threads.end(); it++) {
571     it->join();
572   }
573 
574   return 0;
575 }
576