client.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. /*
  2. *
  3. * Copyright 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. #include <grpc/grpc.h>
  34. #include <stdio.h>
  35. #include <string.h>
  36. #include <grpc/byte_buffer.h>
  37. #include <grpc/byte_buffer_reader.h>
  38. #include <grpc/support/alloc.h>
  39. #include <grpc/support/cmdline.h>
  40. #include <grpc/support/log.h>
  41. #include <grpc/support/time.h>
  42. #include <grpc/support/useful.h>
  43. #include "src/core/lib/support/string.h"
  44. #include "test/core/util/memory_counters.h"
  45. #include "test/core/util/test_config.h"
  46. static grpc_channel *channel;
  47. static grpc_completion_queue *cq;
  48. static grpc_op metadata_ops[2];
  49. static grpc_op status_ops[2];
  50. static grpc_op snapshot_ops[6];
  51. static grpc_op *op;
  52. typedef struct {
  53. grpc_call *call;
  54. grpc_metadata_array initial_metadata_recv;
  55. grpc_status_code status;
  56. grpc_slice details;
  57. grpc_metadata_array trailing_metadata_recv;
  58. } fling_call;
  59. // Statically allocate call data structs. Enough to accomodate 10000 ping-pong
  60. // calls and 1 extra for the snapshot calls.
  61. static fling_call calls[10001];
  62. static void *tag(intptr_t t) { return (void *)t; }
  63. // A call is intentionally divided into two steps. First step is to initiate a
  64. // call (i.e send and recv metadata). A call is outstanding after we initated,
  65. // so we can measure the call memory usage.
  66. static void init_ping_pong_request(int call_idx) {
  67. grpc_metadata_array_init(&calls[call_idx].initial_metadata_recv);
  68. memset(metadata_ops, 0, sizeof(metadata_ops));
  69. op = metadata_ops;
  70. op->op = GRPC_OP_SEND_INITIAL_METADATA;
  71. op->data.send_initial_metadata.count = 0;
  72. op->flags = GRPC_INITIAL_METADATA_WAIT_FOR_READY;
  73. op++;
  74. op->op = GRPC_OP_RECV_INITIAL_METADATA;
  75. op->data.recv_initial_metadata = &calls[call_idx].initial_metadata_recv;
  76. op++;
  77. grpc_slice hostname = grpc_slice_from_static_string("localhost");
  78. calls[call_idx].call = grpc_channel_create_call(
  79. channel, NULL, GRPC_PROPAGATE_DEFAULTS, cq,
  80. grpc_slice_from_static_string("/Reflector/reflectUnary"), &hostname,
  81. gpr_inf_future(GPR_CLOCK_REALTIME), NULL);
  82. GPR_ASSERT(GRPC_CALL_OK == grpc_call_start_batch(calls[call_idx].call,
  83. metadata_ops,
  84. (size_t)(op - metadata_ops),
  85. tag(call_idx), NULL));
  86. grpc_completion_queue_next(cq, gpr_inf_future(GPR_CLOCK_REALTIME), NULL);
  87. }
  88. // Second step is to finish the call (i.e recv status) and destroy the call.
  89. static void finish_ping_pong_request(int call_idx) {
  90. grpc_metadata_array_init(&calls[call_idx].trailing_metadata_recv);
  91. memset(status_ops, 0, sizeof(status_ops));
  92. op = status_ops;
  93. op->op = GRPC_OP_RECV_STATUS_ON_CLIENT;
  94. op->data.recv_status_on_client.trailing_metadata =
  95. &calls[call_idx].trailing_metadata_recv;
  96. op->data.recv_status_on_client.status = &calls[call_idx].status;
  97. op->data.recv_status_on_client.status_details = &calls[call_idx].details;
  98. op++;
  99. GPR_ASSERT(GRPC_CALL_OK == grpc_call_start_batch(calls[call_idx].call,
  100. status_ops,
  101. (size_t)(op - status_ops),
  102. tag(call_idx), NULL));
  103. grpc_completion_queue_next(cq, gpr_inf_future(GPR_CLOCK_REALTIME), NULL);
  104. grpc_metadata_array_destroy(&calls[call_idx].initial_metadata_recv);
  105. grpc_metadata_array_destroy(&calls[call_idx].trailing_metadata_recv);
  106. grpc_slice_unref(calls[call_idx].details);
  107. grpc_call_destroy(calls[call_idx].call);
  108. calls[call_idx].call = NULL;
  109. }
  110. static struct grpc_memory_counters send_snapshot_request(int call_idx,
  111. grpc_slice call_type) {
  112. grpc_metadata_array_init(&calls[call_idx].initial_metadata_recv);
  113. grpc_metadata_array_init(&calls[call_idx].trailing_metadata_recv);
  114. grpc_byte_buffer *response_payload_recv = NULL;
  115. memset(snapshot_ops, 0, sizeof(snapshot_ops));
  116. op = snapshot_ops;
  117. op->op = GRPC_OP_SEND_INITIAL_METADATA;
  118. op->data.send_initial_metadata.count = 0;
  119. op->flags = GRPC_INITIAL_METADATA_WAIT_FOR_READY;
  120. op++;
  121. op->op = GRPC_OP_SEND_CLOSE_FROM_CLIENT;
  122. op++;
  123. op->op = GRPC_OP_RECV_INITIAL_METADATA;
  124. op->data.recv_initial_metadata = &calls[call_idx].initial_metadata_recv;
  125. op++;
  126. op->op = GRPC_OP_RECV_MESSAGE;
  127. op->data.recv_message = &response_payload_recv;
  128. op++;
  129. op->op = GRPC_OP_RECV_STATUS_ON_CLIENT;
  130. op->data.recv_status_on_client.trailing_metadata =
  131. &calls[call_idx].trailing_metadata_recv;
  132. op->data.recv_status_on_client.status = &calls[call_idx].status;
  133. op->data.recv_status_on_client.status_details = &calls[call_idx].details;
  134. op++;
  135. grpc_slice hostname = grpc_slice_from_static_string("localhost");
  136. calls[call_idx].call = grpc_channel_create_call(
  137. channel, NULL, GRPC_PROPAGATE_DEFAULTS, cq, call_type, &hostname,
  138. gpr_inf_future(GPR_CLOCK_REALTIME), NULL);
  139. GPR_ASSERT(GRPC_CALL_OK == grpc_call_start_batch(
  140. calls[call_idx].call, snapshot_ops,
  141. (size_t)(op - snapshot_ops), (void *)0, NULL));
  142. grpc_completion_queue_next(cq, gpr_inf_future(GPR_CLOCK_REALTIME), NULL);
  143. grpc_byte_buffer_reader reader;
  144. grpc_byte_buffer_reader_init(&reader, response_payload_recv);
  145. grpc_slice response = grpc_byte_buffer_reader_readall(&reader);
  146. struct grpc_memory_counters snapshot;
  147. snapshot.total_size_absolute =
  148. ((struct grpc_memory_counters *)GRPC_SLICE_START_PTR(response))
  149. ->total_size_absolute;
  150. snapshot.total_allocs_absolute =
  151. ((struct grpc_memory_counters *)GRPC_SLICE_START_PTR(response))
  152. ->total_allocs_absolute;
  153. snapshot.total_size_relative =
  154. ((struct grpc_memory_counters *)GRPC_SLICE_START_PTR(response))
  155. ->total_size_relative;
  156. snapshot.total_allocs_relative =
  157. ((struct grpc_memory_counters *)GRPC_SLICE_START_PTR(response))
  158. ->total_allocs_relative;
  159. grpc_metadata_array_destroy(&calls[call_idx].initial_metadata_recv);
  160. grpc_metadata_array_destroy(&calls[call_idx].trailing_metadata_recv);
  161. grpc_slice_unref(response);
  162. grpc_byte_buffer_reader_destroy(&reader);
  163. grpc_byte_buffer_destroy(response_payload_recv);
  164. grpc_slice_unref(calls[call_idx].details);
  165. calls[call_idx].details = grpc_empty_slice();
  166. grpc_call_destroy(calls[call_idx].call);
  167. calls[call_idx].call = NULL;
  168. return snapshot;
  169. }
  170. int main(int argc, char **argv) {
  171. grpc_memory_counters_init();
  172. grpc_slice slice = grpc_slice_from_copied_string("x");
  173. char *fake_argv[1];
  174. char *target = "localhost:443";
  175. gpr_cmdline *cl;
  176. grpc_event event;
  177. grpc_init();
  178. GPR_ASSERT(argc >= 1);
  179. fake_argv[0] = argv[0];
  180. grpc_test_init(1, fake_argv);
  181. int warmup_iterations = 100;
  182. int benchmark_iterations = 1000;
  183. cl = gpr_cmdline_create("memory profiling client");
  184. gpr_cmdline_add_string(cl, "target", "Target host:port", &target);
  185. gpr_cmdline_add_int(cl, "warmup", "Warmup iterations", &warmup_iterations);
  186. gpr_cmdline_add_int(cl, "benchmark", "Benchmark iterations",
  187. &benchmark_iterations);
  188. gpr_cmdline_parse(cl, argc, argv);
  189. gpr_cmdline_destroy(cl);
  190. for (size_t k = 0; k < GPR_ARRAY_SIZE(calls); k++) {
  191. calls[k].details = grpc_empty_slice();
  192. }
  193. cq = grpc_completion_queue_create(NULL);
  194. struct grpc_memory_counters client_channel_start =
  195. grpc_memory_counters_snapshot();
  196. channel = grpc_insecure_channel_create(target, NULL, NULL);
  197. int call_idx = 0;
  198. struct grpc_memory_counters before_server_create = send_snapshot_request(
  199. 0, grpc_slice_from_static_string("Reflector/GetBeforeSvrCreation"));
  200. struct grpc_memory_counters after_server_create = send_snapshot_request(
  201. 0, grpc_slice_from_static_string("Reflector/GetAfterSvrCreation"));
  202. // warmup period
  203. for (call_idx = 0; call_idx < warmup_iterations; ++call_idx) {
  204. init_ping_pong_request(call_idx + 1);
  205. }
  206. struct grpc_memory_counters server_benchmark_calls_start =
  207. send_snapshot_request(
  208. 0, grpc_slice_from_static_string("Reflector/SimpleSnapshot"));
  209. struct grpc_memory_counters client_benchmark_calls_start =
  210. grpc_memory_counters_snapshot();
  211. // benchmark period
  212. for (; call_idx < warmup_iterations + benchmark_iterations; ++call_idx) {
  213. init_ping_pong_request(call_idx + 1);
  214. }
  215. struct grpc_memory_counters client_calls_inflight =
  216. grpc_memory_counters_snapshot();
  217. struct grpc_memory_counters server_calls_inflight = send_snapshot_request(
  218. 0, grpc_slice_from_static_string("Reflector/DestroyCalls"));
  219. do {
  220. event = grpc_completion_queue_next(
  221. cq, gpr_time_add(gpr_now(GPR_CLOCK_REALTIME),
  222. gpr_time_from_micros(10000, GPR_TIMESPAN)),
  223. NULL);
  224. } while (event.type != GRPC_QUEUE_TIMEOUT);
  225. // second step - recv status and destroy call
  226. for (call_idx = 0; call_idx < warmup_iterations + benchmark_iterations;
  227. ++call_idx) {
  228. finish_ping_pong_request(call_idx + 1);
  229. }
  230. struct grpc_memory_counters server_calls_end = send_snapshot_request(
  231. 0, grpc_slice_from_static_string("Reflector/SimpleSnapshot"));
  232. struct grpc_memory_counters client_channel_end =
  233. grpc_memory_counters_snapshot();
  234. grpc_channel_destroy(channel);
  235. grpc_completion_queue_shutdown(cq);
  236. do {
  237. event = grpc_completion_queue_next(cq, gpr_inf_future(GPR_CLOCK_REALTIME),
  238. NULL);
  239. } while (event.type != GRPC_QUEUE_SHUTDOWN);
  240. grpc_slice_unref(slice);
  241. grpc_completion_queue_destroy(cq);
  242. grpc_shutdown();
  243. gpr_log(GPR_INFO, "---------client stats--------");
  244. gpr_log(GPR_INFO, "client call memory usage: %f bytes per call",
  245. (double)(client_calls_inflight.total_size_relative -
  246. client_benchmark_calls_start.total_size_relative) /
  247. benchmark_iterations);
  248. gpr_log(GPR_INFO, "client channel memory usage %zi bytes",
  249. client_channel_end.total_size_relative -
  250. client_channel_start.total_size_relative);
  251. gpr_log(GPR_INFO, "---------server stats--------");
  252. gpr_log(GPR_INFO, "server create: %zi bytes",
  253. after_server_create.total_size_relative -
  254. before_server_create.total_size_relative);
  255. gpr_log(GPR_INFO, "server call memory usage: %f bytes per call",
  256. (double)(server_calls_inflight.total_size_relative -
  257. server_benchmark_calls_start.total_size_relative) /
  258. benchmark_iterations);
  259. gpr_log(GPR_INFO, "server channel memory usage %zi bytes",
  260. server_calls_end.total_size_relative -
  261. after_server_create.total_size_relative);
  262. grpc_memory_counters_destroy();
  263. return 0;
  264. }