c9db1951a7c4cbb4d789c9245262b7e5acd3cab6
[platform/upstream/grpc.git] / test / core / fling / client.cc
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 #include <grpc/grpc.h>
20
21 #include <stdio.h>
22 #include <string.h>
23
24 #include <grpc/support/log.h>
25 #include <grpc/support/time.h>
26
27 #include "src/core/lib/gpr/useful.h"
28 #include "src/core/lib/profiling/timers.h"
29 #include "test/core/util/cmdline.h"
30 #include "test/core/util/grpc_profiler.h"
31 #include "test/core/util/histogram.h"
32 #include "test/core/util/test_config.h"
33
34 static grpc_histogram* histogram;
35 static grpc_byte_buffer* the_buffer;
36 static grpc_channel* channel;
37 static grpc_completion_queue* cq;
38 static grpc_call* call;
39 static grpc_op ops[6];
40 static grpc_op stream_init_ops[2];
41 static grpc_op stream_step_ops[2];
42 static grpc_metadata_array initial_metadata_recv;
43 static grpc_metadata_array trailing_metadata_recv;
44 static grpc_byte_buffer* response_payload_recv = nullptr;
45 static grpc_status_code status;
46 static grpc_slice details;
47 static grpc_op* op;
48
49 static void init_ping_pong_request(void) {
50   grpc_metadata_array_init(&initial_metadata_recv);
51   grpc_metadata_array_init(&trailing_metadata_recv);
52
53   memset(ops, 0, sizeof(ops));
54   op = ops;
55
56   op->op = GRPC_OP_SEND_INITIAL_METADATA;
57   op->data.send_initial_metadata.count = 0;
58   op++;
59   op->op = GRPC_OP_SEND_MESSAGE;
60   op->data.send_message.send_message = the_buffer;
61   op++;
62   op->op = GRPC_OP_SEND_CLOSE_FROM_CLIENT;
63   op++;
64   op->op = GRPC_OP_RECV_INITIAL_METADATA;
65   op->data.recv_initial_metadata.recv_initial_metadata = &initial_metadata_recv;
66   op++;
67   op->op = GRPC_OP_RECV_MESSAGE;
68   op->data.recv_message.recv_message = &response_payload_recv;
69   op++;
70   op->op = GRPC_OP_RECV_STATUS_ON_CLIENT;
71   op->data.recv_status_on_client.trailing_metadata = &trailing_metadata_recv;
72   op->data.recv_status_on_client.status = &status;
73   op->data.recv_status_on_client.status_details = &details;
74   op++;
75 }
76
77 static void step_ping_pong_request(void) {
78   GPR_TIMER_SCOPE("ping_pong", 1);
79   grpc_slice host = grpc_slice_from_static_string("localhost");
80   call = grpc_channel_create_call(
81       channel, nullptr, GRPC_PROPAGATE_DEFAULTS, cq,
82       grpc_slice_from_static_string("/Reflector/reflectUnary"), &host,
83       gpr_inf_future(GPR_CLOCK_REALTIME), nullptr);
84   GPR_ASSERT(GRPC_CALL_OK == grpc_call_start_batch(call, ops,
85                                                    (size_t)(op - ops), (void*)1,
86                                                    nullptr));
87   grpc_completion_queue_next(cq, gpr_inf_future(GPR_CLOCK_REALTIME), nullptr);
88   grpc_call_unref(call);
89   grpc_byte_buffer_destroy(response_payload_recv);
90   call = nullptr;
91 }
92
93 static void init_ping_pong_stream(void) {
94   grpc_metadata_array_init(&initial_metadata_recv);
95
96   grpc_call_error error;
97   grpc_slice host = grpc_slice_from_static_string("localhost");
98   call = grpc_channel_create_call(
99       channel, nullptr, GRPC_PROPAGATE_DEFAULTS, cq,
100       grpc_slice_from_static_string("/Reflector/reflectStream"), &host,
101       gpr_inf_future(GPR_CLOCK_REALTIME), nullptr);
102   stream_init_ops[0].op = GRPC_OP_SEND_INITIAL_METADATA;
103   stream_init_ops[0].data.send_initial_metadata.count = 0;
104   stream_init_ops[1].op = GRPC_OP_RECV_INITIAL_METADATA;
105   stream_init_ops[1].data.recv_initial_metadata.recv_initial_metadata =
106       &initial_metadata_recv;
107   error = grpc_call_start_batch(call, stream_init_ops, 2,
108                                 reinterpret_cast<void*>(1), nullptr);
109   GPR_ASSERT(GRPC_CALL_OK == error);
110   grpc_completion_queue_next(cq, gpr_inf_future(GPR_CLOCK_REALTIME), nullptr);
111
112   grpc_metadata_array_init(&initial_metadata_recv);
113
114   stream_step_ops[0].op = GRPC_OP_SEND_MESSAGE;
115   stream_step_ops[0].data.send_message.send_message = the_buffer;
116   stream_step_ops[1].op = GRPC_OP_RECV_MESSAGE;
117   stream_step_ops[1].data.recv_message.recv_message = &response_payload_recv;
118 }
119
120 static void step_ping_pong_stream(void) {
121   GPR_TIMER_SCOPE("ping_pong", 1);
122   grpc_call_error error;
123   error = grpc_call_start_batch(call, stream_step_ops, 2,
124                                 reinterpret_cast<void*>(1), nullptr);
125   GPR_ASSERT(GRPC_CALL_OK == error);
126   grpc_completion_queue_next(cq, gpr_inf_future(GPR_CLOCK_REALTIME), nullptr);
127   grpc_byte_buffer_destroy(response_payload_recv);
128 }
129
130 static double now(void) {
131   gpr_timespec tv = gpr_now(GPR_CLOCK_REALTIME);
132   return 1e9 * static_cast<double>(tv.tv_sec) + tv.tv_nsec;
133 }
134
135 typedef struct {
136   const char* name;
137   void (*init)();
138   void (*do_one_step)();
139 } scenario;
140
141 static const scenario scenarios[] = {
142     {"ping-pong-request", init_ping_pong_request, step_ping_pong_request},
143     {"ping-pong-stream", init_ping_pong_stream, step_ping_pong_stream},
144 };
145
146 int main(int argc, char** argv) {
147   grpc_slice slice = grpc_slice_from_copied_string("x");
148   double start, stop;
149   unsigned i;
150
151   char* fake_argv[1];
152
153   int payload_size = 1;
154   int secure = 0;
155   const char* target = "localhost:443";
156   gpr_cmdline* cl;
157   grpc_event event;
158   const char* scenario_name = "ping-pong-request";
159   scenario sc = {nullptr, nullptr, nullptr};
160
161   gpr_timers_set_log_filename("latency_trace.fling_client.txt");
162
163   GPR_ASSERT(argc >= 1);
164   fake_argv[0] = argv[0];
165   grpc::testing::TestEnvironment env(1, fake_argv);
166
167   grpc_init();
168
169   int warmup_seconds = 1;
170   int benchmark_seconds = 5;
171
172   cl = gpr_cmdline_create("fling client");
173   gpr_cmdline_add_int(cl, "payload_size", "Size of the payload to send",
174                       &payload_size);
175   gpr_cmdline_add_string(cl, "target", "Target host:port", &target);
176   gpr_cmdline_add_flag(cl, "secure", "Run with security?", &secure);
177   gpr_cmdline_add_string(cl, "scenario", "Scenario", &scenario_name);
178   gpr_cmdline_add_int(cl, "warmup", "Warmup seconds", &warmup_seconds);
179   gpr_cmdline_add_int(cl, "benchmark", "Benchmark seconds", &benchmark_seconds);
180   gpr_cmdline_parse(cl, argc, argv);
181   gpr_cmdline_destroy(cl);
182
183   for (i = 0; i < GPR_ARRAY_SIZE(scenarios); i++) {
184     if (0 == strcmp(scenarios[i].name, scenario_name)) {
185       sc = scenarios[i];
186     }
187   }
188   if (!sc.name) {
189     fprintf(stderr, "unsupported scenario '%s'. Valid are:", scenario_name);
190     fflush(stderr);
191     for (i = 0; i < GPR_ARRAY_SIZE(scenarios); i++) {
192       fprintf(stderr, " %s", scenarios[i].name);
193       fflush(stderr);
194     }
195     return 1;
196   }
197
198   channel = grpc_insecure_channel_create(target, nullptr, nullptr);
199   cq = grpc_completion_queue_create_for_next(nullptr);
200   the_buffer =
201       grpc_raw_byte_buffer_create(&slice, static_cast<size_t>(payload_size));
202   histogram = grpc_histogram_create(0.01, 60e9);
203
204   sc.init();
205
206   gpr_timespec end_warmup = grpc_timeout_seconds_to_deadline(warmup_seconds);
207   gpr_timespec end_profiling =
208       grpc_timeout_seconds_to_deadline(warmup_seconds + benchmark_seconds);
209
210   while (gpr_time_cmp(gpr_now(end_warmup.clock_type), end_warmup) < 0) {
211     sc.do_one_step();
212   }
213
214   gpr_log(GPR_INFO, "start profiling");
215   grpc_profiler_start("client.prof");
216   while (gpr_time_cmp(gpr_now(end_profiling.clock_type), end_profiling) < 0) {
217     start = now();
218     sc.do_one_step();
219     stop = now();
220     grpc_histogram_add(histogram, stop - start);
221   }
222   grpc_profiler_stop();
223
224   if (call) {
225     grpc_call_unref(call);
226   }
227
228   grpc_channel_destroy(channel);
229   grpc_completion_queue_shutdown(cq);
230   do {
231     event = grpc_completion_queue_next(cq, gpr_inf_future(GPR_CLOCK_REALTIME),
232                                        nullptr);
233   } while (event.type != GRPC_QUEUE_SHUTDOWN);
234   grpc_completion_queue_destroy(cq);
235   grpc_byte_buffer_destroy(the_buffer);
236   grpc_slice_unref(slice);
237
238   gpr_log(GPR_INFO, "latency (50/95/99/99.9): %f/%f/%f/%f",
239           grpc_histogram_percentile(histogram, 50),
240           grpc_histogram_percentile(histogram, 95),
241           grpc_histogram_percentile(histogram, 99),
242           grpc_histogram_percentile(histogram, 99.9));
243   grpc_histogram_destroy(histogram);
244
245   grpc_shutdown();
246
247   return 0;
248 }