OLD | NEW |
(Empty) | |
| 1 /* |
| 2 * |
| 3 * Copyright 2015-2016, Google Inc. |
| 4 * All rights reserved. |
| 5 * |
| 6 * Redistribution and use in source and binary forms, with or without |
| 7 * modification, are permitted provided that the following conditions are |
| 8 * met: |
| 9 * |
| 10 * * Redistributions of source code must retain the above copyright |
| 11 * notice, this list of conditions and the following disclaimer. |
| 12 * * Redistributions in binary form must reproduce the above |
| 13 * copyright notice, this list of conditions and the following disclaimer |
| 14 * in the documentation and/or other materials provided with the |
| 15 * distribution. |
| 16 * * Neither the name of Google Inc. nor the names of its |
| 17 * contributors may be used to endorse or promote products derived from |
| 18 * this software without specific prior written permission. |
| 19 * |
| 20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 23 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 24 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 25 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 26 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 30 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 31 * |
| 32 */ |
| 33 |
| 34 #include <grpc/grpc.h> |
| 35 |
| 36 #include <stdio.h> |
| 37 #include <string.h> |
| 38 |
| 39 #include <grpc/support/cmdline.h> |
| 40 #include <grpc/support/histogram.h> |
| 41 #include <grpc/support/log.h> |
| 42 #include <grpc/support/time.h> |
| 43 #include <grpc/support/useful.h> |
| 44 #include "src/core/profiling/timers.h" |
| 45 #include "test/core/util/grpc_profiler.h" |
| 46 #include "test/core/util/test_config.h" |
| 47 |
| 48 static gpr_histogram *histogram; |
| 49 static grpc_byte_buffer *the_buffer; |
| 50 static grpc_channel *channel; |
| 51 static grpc_completion_queue *cq; |
| 52 static grpc_call *call; |
| 53 static grpc_op ops[6]; |
| 54 static grpc_op stream_init_ops[2]; |
| 55 static grpc_op stream_step_ops[2]; |
| 56 static grpc_metadata_array initial_metadata_recv; |
| 57 static grpc_metadata_array trailing_metadata_recv; |
| 58 static grpc_byte_buffer *response_payload_recv = NULL; |
| 59 static grpc_status_code status; |
| 60 static char *details = NULL; |
| 61 static size_t details_capacity = 0; |
| 62 static grpc_op *op; |
| 63 |
| 64 static void init_ping_pong_request(void) { |
| 65 grpc_metadata_array_init(&initial_metadata_recv); |
| 66 grpc_metadata_array_init(&trailing_metadata_recv); |
| 67 |
| 68 op = ops; |
| 69 |
| 70 op->op = GRPC_OP_SEND_INITIAL_METADATA; |
| 71 op->data.send_initial_metadata.count = 0; |
| 72 op++; |
| 73 op->op = GRPC_OP_SEND_MESSAGE; |
| 74 op->data.send_message = the_buffer; |
| 75 op++; |
| 76 op->op = GRPC_OP_SEND_CLOSE_FROM_CLIENT; |
| 77 op++; |
| 78 op->op = GRPC_OP_RECV_INITIAL_METADATA; |
| 79 op->data.recv_initial_metadata = &initial_metadata_recv; |
| 80 op++; |
| 81 op->op = GRPC_OP_RECV_MESSAGE; |
| 82 op->data.recv_message = &response_payload_recv; |
| 83 op++; |
| 84 op->op = GRPC_OP_RECV_STATUS_ON_CLIENT; |
| 85 op->data.recv_status_on_client.trailing_metadata = &trailing_metadata_recv; |
| 86 op->data.recv_status_on_client.status = &status; |
| 87 op->data.recv_status_on_client.status_details = &details; |
| 88 op->data.recv_status_on_client.status_details_capacity = &details_capacity; |
| 89 op++; |
| 90 } |
| 91 |
| 92 static void step_ping_pong_request(void) { |
| 93 GPR_TIMER_BEGIN("ping_pong", 1); |
| 94 call = grpc_channel_create_call(channel, NULL, GRPC_PROPAGATE_DEFAULTS, cq, |
| 95 "/Reflector/reflectUnary", "localhost", |
| 96 gpr_inf_future(GPR_CLOCK_REALTIME), NULL); |
| 97 GPR_ASSERT(GRPC_CALL_OK == grpc_call_start_batch(call, ops, |
| 98 (size_t)(op - ops), |
| 99 (void *)1, NULL)); |
| 100 grpc_completion_queue_next(cq, gpr_inf_future(GPR_CLOCK_REALTIME), NULL); |
| 101 grpc_call_destroy(call); |
| 102 grpc_byte_buffer_destroy(response_payload_recv); |
| 103 call = NULL; |
| 104 GPR_TIMER_END("ping_pong", 1); |
| 105 } |
| 106 |
| 107 static void init_ping_pong_stream(void) { |
| 108 grpc_metadata_array_init(&initial_metadata_recv); |
| 109 |
| 110 grpc_call_error error; |
| 111 call = grpc_channel_create_call(channel, NULL, GRPC_PROPAGATE_DEFAULTS, cq, |
| 112 "/Reflector/reflectStream", "localhost", |
| 113 gpr_inf_future(GPR_CLOCK_REALTIME), NULL); |
| 114 stream_init_ops[0].op = GRPC_OP_SEND_INITIAL_METADATA; |
| 115 stream_init_ops[0].data.send_initial_metadata.count = 0; |
| 116 stream_init_ops[1].op = GRPC_OP_RECV_INITIAL_METADATA; |
| 117 stream_init_ops[1].data.recv_initial_metadata = &initial_metadata_recv; |
| 118 error = grpc_call_start_batch(call, stream_init_ops, 2, (void *)1, NULL); |
| 119 GPR_ASSERT(GRPC_CALL_OK == error); |
| 120 grpc_completion_queue_next(cq, gpr_inf_future(GPR_CLOCK_REALTIME), NULL); |
| 121 |
| 122 grpc_metadata_array_init(&initial_metadata_recv); |
| 123 |
| 124 stream_step_ops[0].op = GRPC_OP_SEND_MESSAGE; |
| 125 stream_step_ops[0].data.send_message = the_buffer; |
| 126 stream_step_ops[1].op = GRPC_OP_RECV_MESSAGE; |
| 127 stream_step_ops[1].data.recv_message = &response_payload_recv; |
| 128 } |
| 129 |
| 130 static void step_ping_pong_stream(void) { |
| 131 grpc_call_error error; |
| 132 GPR_TIMER_BEGIN("ping_pong", 1); |
| 133 error = grpc_call_start_batch(call, stream_step_ops, 2, (void *)1, NULL); |
| 134 GPR_ASSERT(GRPC_CALL_OK == error); |
| 135 grpc_completion_queue_next(cq, gpr_inf_future(GPR_CLOCK_REALTIME), NULL); |
| 136 grpc_byte_buffer_destroy(response_payload_recv); |
| 137 GPR_TIMER_END("ping_pong", 1); |
| 138 } |
| 139 |
| 140 static double now(void) { |
| 141 gpr_timespec tv = gpr_now(GPR_CLOCK_REALTIME); |
| 142 return 1e9 * (double)tv.tv_sec + tv.tv_nsec; |
| 143 } |
| 144 |
| 145 typedef struct { |
| 146 const char *name; |
| 147 void (*init)(); |
| 148 void (*do_one_step)(); |
| 149 } scenario; |
| 150 |
| 151 static const scenario scenarios[] = { |
| 152 {"ping-pong-request", init_ping_pong_request, step_ping_pong_request}, |
| 153 {"ping-pong-stream", init_ping_pong_stream, step_ping_pong_stream}, |
| 154 }; |
| 155 |
| 156 int main(int argc, char **argv) { |
| 157 gpr_slice slice = gpr_slice_from_copied_string("x"); |
| 158 double start, stop; |
| 159 unsigned i; |
| 160 |
| 161 char *fake_argv[1]; |
| 162 |
| 163 int payload_size = 1; |
| 164 int secure = 0; |
| 165 char *target = "localhost:443"; |
| 166 gpr_cmdline *cl; |
| 167 grpc_event event; |
| 168 char *scenario_name = "ping-pong-request"; |
| 169 scenario sc = {NULL, NULL, NULL}; |
| 170 |
| 171 gpr_timers_set_log_filename("latency_trace.fling_client.txt"); |
| 172 |
| 173 grpc_init(); |
| 174 |
| 175 GPR_ASSERT(argc >= 1); |
| 176 fake_argv[0] = argv[0]; |
| 177 grpc_test_init(1, fake_argv); |
| 178 |
| 179 cl = gpr_cmdline_create("fling client"); |
| 180 gpr_cmdline_add_int(cl, "payload_size", "Size of the payload to send", |
| 181 &payload_size); |
| 182 gpr_cmdline_add_string(cl, "target", "Target host:port", &target); |
| 183 gpr_cmdline_add_flag(cl, "secure", "Run with security?", &secure); |
| 184 gpr_cmdline_add_string(cl, "scenario", "Scenario", &scenario_name); |
| 185 gpr_cmdline_parse(cl, argc, argv); |
| 186 gpr_cmdline_destroy(cl); |
| 187 |
| 188 for (i = 0; i < GPR_ARRAY_SIZE(scenarios); i++) { |
| 189 if (0 == strcmp(scenarios[i].name, scenario_name)) { |
| 190 sc = scenarios[i]; |
| 191 } |
| 192 } |
| 193 if (!sc.name) { |
| 194 fprintf(stderr, "unsupported scenario '%s'. Valid are:", scenario_name); |
| 195 for (i = 0; i < GPR_ARRAY_SIZE(scenarios); i++) { |
| 196 fprintf(stderr, " %s", scenarios[i].name); |
| 197 } |
| 198 return 1; |
| 199 } |
| 200 |
| 201 channel = grpc_insecure_channel_create(target, NULL, NULL); |
| 202 cq = grpc_completion_queue_create(NULL); |
| 203 the_buffer = grpc_raw_byte_buffer_create(&slice, (size_t)payload_size); |
| 204 histogram = gpr_histogram_create(0.01, 60e9); |
| 205 |
| 206 sc.init(); |
| 207 |
| 208 gpr_timespec end_warmup = GRPC_TIMEOUT_SECONDS_TO_DEADLINE(3); |
| 209 gpr_timespec end_profiling = GRPC_TIMEOUT_SECONDS_TO_DEADLINE(30); |
| 210 |
| 211 while (gpr_time_cmp(gpr_now(end_warmup.clock_type), end_warmup) < 0) { |
| 212 sc.do_one_step(); |
| 213 } |
| 214 |
| 215 gpr_log(GPR_INFO, "start profiling"); |
| 216 grpc_profiler_start("client.prof"); |
| 217 while (gpr_time_cmp(gpr_now(end_profiling.clock_type), end_profiling) < 0) { |
| 218 start = now(); |
| 219 sc.do_one_step(); |
| 220 stop = now(); |
| 221 gpr_histogram_add(histogram, stop - start); |
| 222 } |
| 223 grpc_profiler_stop(); |
| 224 |
| 225 if (call) { |
| 226 grpc_call_destroy(call); |
| 227 } |
| 228 |
| 229 grpc_channel_destroy(channel); |
| 230 grpc_completion_queue_shutdown(cq); |
| 231 do { |
| 232 event = grpc_completion_queue_next(cq, gpr_inf_future(GPR_CLOCK_REALTIME), |
| 233 NULL); |
| 234 } while (event.type != GRPC_QUEUE_SHUTDOWN); |
| 235 grpc_completion_queue_destroy(cq); |
| 236 grpc_byte_buffer_destroy(the_buffer); |
| 237 gpr_slice_unref(slice); |
| 238 |
| 239 gpr_log(GPR_INFO, "latency (50/95/99/99.9): %f/%f/%f/%f", |
| 240 gpr_histogram_percentile(histogram, 50), |
| 241 gpr_histogram_percentile(histogram, 95), |
| 242 gpr_histogram_percentile(histogram, 99), |
| 243 gpr_histogram_percentile(histogram, 99.9)); |
| 244 gpr_histogram_destroy(histogram); |
| 245 |
| 246 grpc_shutdown(); |
| 247 |
| 248 return 0; |
| 249 } |
OLD | NEW |