• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
init_ping_pong_request(void)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 
step_ping_pong_request(void)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 
init_ping_pong_stream(void)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, (void*)1, nullptr);
108   GPR_ASSERT(GRPC_CALL_OK == error);
109   grpc_completion_queue_next(cq, gpr_inf_future(GPR_CLOCK_REALTIME), nullptr);
110 
111   grpc_metadata_array_init(&initial_metadata_recv);
112 
113   stream_step_ops[0].op = GRPC_OP_SEND_MESSAGE;
114   stream_step_ops[0].data.send_message.send_message = the_buffer;
115   stream_step_ops[1].op = GRPC_OP_RECV_MESSAGE;
116   stream_step_ops[1].data.recv_message.recv_message = &response_payload_recv;
117 }
118 
step_ping_pong_stream(void)119 static void step_ping_pong_stream(void) {
120   GPR_TIMER_SCOPE("ping_pong", 1);
121   grpc_call_error error;
122   error = grpc_call_start_batch(call, stream_step_ops, 2, (void*)1, nullptr);
123   GPR_ASSERT(GRPC_CALL_OK == error);
124   grpc_completion_queue_next(cq, gpr_inf_future(GPR_CLOCK_REALTIME), nullptr);
125   grpc_byte_buffer_destroy(response_payload_recv);
126 }
127 
now(void)128 static double now(void) {
129   gpr_timespec tv = gpr_now(GPR_CLOCK_REALTIME);
130   return 1e9 * static_cast<double>(tv.tv_sec) + tv.tv_nsec;
131 }
132 
133 typedef struct {
134   const char* name;
135   void (*init)();
136   void (*do_one_step)();
137 } scenario;
138 
139 static const scenario scenarios[] = {
140     {"ping-pong-request", init_ping_pong_request, step_ping_pong_request},
141     {"ping-pong-stream", init_ping_pong_stream, step_ping_pong_stream},
142 };
143 
main(int argc,char ** argv)144 int main(int argc, char** argv) {
145   grpc_slice slice = grpc_slice_from_copied_string("x");
146   double start, stop;
147   unsigned i;
148 
149   char* fake_argv[1];
150 
151   int payload_size = 1;
152   int secure = 0;
153   const char* target = "localhost:443";
154   gpr_cmdline* cl;
155   grpc_event event;
156   const char* scenario_name = "ping-pong-request";
157   scenario sc = {nullptr, nullptr, nullptr};
158 
159   gpr_timers_set_log_filename("latency_trace.fling_client.txt");
160 
161   grpc_init();
162 
163   GPR_ASSERT(argc >= 1);
164   fake_argv[0] = argv[0];
165   grpc_test_init(1, fake_argv);
166 
167   int warmup_seconds = 1;
168   int benchmark_seconds = 5;
169 
170   cl = gpr_cmdline_create("fling client");
171   gpr_cmdline_add_int(cl, "payload_size", "Size of the payload to send",
172                       &payload_size);
173   gpr_cmdline_add_string(cl, "target", "Target host:port", &target);
174   gpr_cmdline_add_flag(cl, "secure", "Run with security?", &secure);
175   gpr_cmdline_add_string(cl, "scenario", "Scenario", &scenario_name);
176   gpr_cmdline_add_int(cl, "warmup", "Warmup seconds", &warmup_seconds);
177   gpr_cmdline_add_int(cl, "benchmark", "Benchmark seconds", &benchmark_seconds);
178   gpr_cmdline_parse(cl, argc, argv);
179   gpr_cmdline_destroy(cl);
180 
181   for (i = 0; i < GPR_ARRAY_SIZE(scenarios); i++) {
182     if (0 == strcmp(scenarios[i].name, scenario_name)) {
183       sc = scenarios[i];
184     }
185   }
186   if (!sc.name) {
187     fprintf(stderr, "unsupported scenario '%s'. Valid are:", scenario_name);
188     fflush(stderr);
189     for (i = 0; i < GPR_ARRAY_SIZE(scenarios); i++) {
190       fprintf(stderr, " %s", scenarios[i].name);
191       fflush(stderr);
192     }
193     return 1;
194   }
195 
196   channel = grpc_insecure_channel_create(target, nullptr, nullptr);
197   cq = grpc_completion_queue_create_for_next(nullptr);
198   the_buffer =
199       grpc_raw_byte_buffer_create(&slice, static_cast<size_t>(payload_size));
200   histogram = grpc_histogram_create(0.01, 60e9);
201 
202   sc.init();
203 
204   gpr_timespec end_warmup = grpc_timeout_seconds_to_deadline(warmup_seconds);
205   gpr_timespec end_profiling =
206       grpc_timeout_seconds_to_deadline(warmup_seconds + benchmark_seconds);
207 
208   while (gpr_time_cmp(gpr_now(end_warmup.clock_type), end_warmup) < 0) {
209     sc.do_one_step();
210   }
211 
212   gpr_log(GPR_INFO, "start profiling");
213   grpc_profiler_start("client.prof");
214   while (gpr_time_cmp(gpr_now(end_profiling.clock_type), end_profiling) < 0) {
215     start = now();
216     sc.do_one_step();
217     stop = now();
218     grpc_histogram_add(histogram, stop - start);
219   }
220   grpc_profiler_stop();
221 
222   if (call) {
223     grpc_call_unref(call);
224   }
225 
226   grpc_channel_destroy(channel);
227   grpc_completion_queue_shutdown(cq);
228   do {
229     event = grpc_completion_queue_next(cq, gpr_inf_future(GPR_CLOCK_REALTIME),
230                                        nullptr);
231   } while (event.type != GRPC_QUEUE_SHUTDOWN);
232   grpc_completion_queue_destroy(cq);
233   grpc_byte_buffer_destroy(the_buffer);
234   grpc_slice_unref(slice);
235 
236   gpr_log(GPR_INFO, "latency (50/95/99/99.9): %f/%f/%f/%f",
237           grpc_histogram_percentile(histogram, 50),
238           grpc_histogram_percentile(histogram, 95),
239           grpc_histogram_percentile(histogram, 99),
240           grpc_histogram_percentile(histogram, 99.9));
241   grpc_histogram_destroy(histogram);
242 
243   grpc_shutdown();
244 
245   return 0;
246 }
247