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