3 * Copyright 2015 gRPC authors.
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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 #include <grpc/grpc.h>
24 #include <grpc/support/log.h>
25 #include <grpc/support/time.h>
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"
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;
49 static void init_ping_pong_request(void) {
50 grpc_metadata_array_init(&initial_metadata_recv);
51 grpc_metadata_array_init(&trailing_metadata_recv);
53 memset(ops, 0, sizeof(ops));
56 op->op = GRPC_OP_SEND_INITIAL_METADATA;
57 op->data.send_initial_metadata.count = 0;
59 op->op = GRPC_OP_SEND_MESSAGE;
60 op->data.send_message.send_message = the_buffer;
62 op->op = GRPC_OP_SEND_CLOSE_FROM_CLIENT;
64 op->op = GRPC_OP_RECV_INITIAL_METADATA;
65 op->data.recv_initial_metadata.recv_initial_metadata = &initial_metadata_recv;
67 op->op = GRPC_OP_RECV_MESSAGE;
68 op->data.recv_message.recv_message = &response_payload_recv;
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;
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,
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);
93 static void init_ping_pong_stream(void) {
94 grpc_metadata_array_init(&initial_metadata_recv);
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);
112 grpc_metadata_array_init(&initial_metadata_recv);
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;
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);
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;
138 void (*do_one_step)();
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},
146 int main(int argc, char** argv) {
147 grpc_slice slice = grpc_slice_from_copied_string("x");
153 int payload_size = 1;
155 const char* target = "localhost:443";
158 const char* scenario_name = "ping-pong-request";
159 scenario sc = {nullptr, nullptr, nullptr};
161 gpr_timers_set_log_filename("latency_trace.fling_client.txt");
163 GPR_ASSERT(argc >= 1);
164 fake_argv[0] = argv[0];
165 grpc::testing::TestEnvironment env(1, fake_argv);
169 int warmup_seconds = 1;
170 int benchmark_seconds = 5;
172 cl = gpr_cmdline_create("fling client");
173 gpr_cmdline_add_int(cl, "payload_size", "Size of the payload to send",
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);
183 for (i = 0; i < GPR_ARRAY_SIZE(scenarios); i++) {
184 if (0 == strcmp(scenarios[i].name, scenario_name)) {
189 fprintf(stderr, "unsupported scenario '%s'. Valid are:", scenario_name);
191 for (i = 0; i < GPR_ARRAY_SIZE(scenarios); i++) {
192 fprintf(stderr, " %s", scenarios[i].name);
198 channel = grpc_insecure_channel_create(target, nullptr, nullptr);
199 cq = grpc_completion_queue_create_for_next(nullptr);
201 grpc_raw_byte_buffer_create(&slice, static_cast<size_t>(payload_size));
202 histogram = grpc_histogram_create(0.01, 60e9);
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);
210 while (gpr_time_cmp(gpr_now(end_warmup.clock_type), end_warmup) < 0) {
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) {
220 grpc_histogram_add(histogram, stop - start);
222 grpc_profiler_stop();
225 grpc_call_unref(call);
228 grpc_channel_destroy(channel);
229 grpc_completion_queue_shutdown(cq);
231 event = grpc_completion_queue_next(cq, gpr_inf_future(GPR_CLOCK_REALTIME),
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);
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);