client.cc 8.3 KB

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