LCOV - code coverage report
Current view: top level - test/core/fling - client.c (source / functions) Hit Total Coverage
Test: tmp.zDYK9MVh93 Lines: 105 109 96.3 %
Date: 2015-10-10 Functions: 6 6 100.0 %

          Line data    Source code
       1             : /*
       2             :  *
       3             :  * Copyright 2015, 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 "test/core/util/grpc_profiler.h"
      45             : #include "test/core/util/test_config.h"
      46             : 
      47             : static gpr_histogram *histogram;
      48             : static grpc_byte_buffer *the_buffer;
      49             : static grpc_channel *channel;
      50             : static grpc_completion_queue *cq;
      51             : static grpc_call *call;
      52             : static grpc_op ops[6];
      53             : static grpc_op stream_init_op;
      54             : static grpc_op stream_step_ops[2];
      55             : static grpc_metadata_array initial_metadata_recv;
      56             : static grpc_metadata_array trailing_metadata_recv;
      57             : static grpc_byte_buffer *response_payload_recv = NULL;
      58             : static grpc_status_code status;
      59             : static char *details = NULL;
      60             : static size_t details_capacity = 0;
      61             : static grpc_op *op;
      62             : 
      63           1 : static void init_ping_pong_request(void) {
      64           1 :   grpc_metadata_array_init(&initial_metadata_recv);
      65           1 :   grpc_metadata_array_init(&trailing_metadata_recv);
      66             : 
      67           1 :   op = ops;
      68             : 
      69           1 :   op->op = GRPC_OP_SEND_INITIAL_METADATA;
      70           1 :   op->data.send_initial_metadata.count = 0;
      71           1 :   op++;
      72           1 :   op->op = GRPC_OP_SEND_MESSAGE;
      73           1 :   op->data.send_message = the_buffer;
      74           1 :   op++;
      75           1 :   op->op = GRPC_OP_SEND_CLOSE_FROM_CLIENT;
      76           1 :   op++;
      77           1 :   op->op = GRPC_OP_RECV_INITIAL_METADATA;
      78           1 :   op->data.recv_initial_metadata = &initial_metadata_recv;
      79           1 :   op++;
      80           1 :   op->op = GRPC_OP_RECV_MESSAGE;
      81           1 :   op->data.recv_message = &response_payload_recv;
      82           1 :   op++;
      83           1 :   op->op = GRPC_OP_RECV_STATUS_ON_CLIENT;
      84           1 :   op->data.recv_status_on_client.trailing_metadata = &trailing_metadata_recv;
      85           1 :   op->data.recv_status_on_client.status = &status;
      86           1 :   op->data.recv_status_on_client.status_details = &details;
      87           1 :   op->data.recv_status_on_client.status_details_capacity = &details_capacity;
      88           1 :   op++;
      89           1 : }
      90             : 
      91      101000 : static void step_ping_pong_request(void) {
      92      101000 :   call = grpc_channel_create_call(channel, NULL, GRPC_PROPAGATE_DEFAULTS, cq,
      93             :                                   "/Reflector/reflectUnary", "localhost",
      94             :                                   gpr_inf_future(GPR_CLOCK_REALTIME), NULL);
      95      101000 :   GPR_ASSERT(GRPC_CALL_OK == grpc_call_start_batch(call, ops,
      96             :                                                    (size_t)(op - ops),
      97             :                                                    (void *)1, NULL));
      98      101000 :   grpc_completion_queue_next(cq, gpr_inf_future(GPR_CLOCK_REALTIME), NULL);
      99      101000 :   grpc_call_destroy(call);
     100      101000 :   grpc_byte_buffer_destroy(response_payload_recv);
     101      101000 :   call = NULL;
     102      101000 : }
     103             : 
     104           1 : static void init_ping_pong_stream(void) {
     105             :   grpc_call_error error;
     106           1 :   call = grpc_channel_create_call(channel, NULL, GRPC_PROPAGATE_DEFAULTS, cq,
     107             :                                   "/Reflector/reflectStream", "localhost",
     108             :                                   gpr_inf_future(GPR_CLOCK_REALTIME), NULL);
     109           1 :   stream_init_op.op = GRPC_OP_SEND_INITIAL_METADATA;
     110           1 :   stream_init_op.data.send_initial_metadata.count = 0;
     111           1 :   error = grpc_call_start_batch(call, &stream_init_op, 1, (void *)1, NULL);
     112           1 :   GPR_ASSERT(GRPC_CALL_OK == error);
     113           1 :   grpc_completion_queue_next(cq, gpr_inf_future(GPR_CLOCK_REALTIME), NULL);
     114             : 
     115           1 :   grpc_metadata_array_init(&initial_metadata_recv);
     116             : 
     117           1 :   stream_step_ops[0].op = GRPC_OP_SEND_MESSAGE;
     118           1 :   stream_step_ops[0].data.send_message = the_buffer;
     119           1 :   stream_step_ops[1].op = GRPC_OP_RECV_MESSAGE;
     120           1 :   stream_step_ops[1].data.recv_message = &response_payload_recv;
     121           1 : }
     122             : 
     123      101000 : static void step_ping_pong_stream(void) {
     124             :   grpc_call_error error;
     125      101000 :   error = grpc_call_start_batch(call, stream_step_ops, 2, (void *)1, NULL);
     126      101000 :   GPR_ASSERT(GRPC_CALL_OK == error);
     127      101000 :   grpc_completion_queue_next(cq, gpr_inf_future(GPR_CLOCK_REALTIME), NULL);
     128      101000 :   grpc_byte_buffer_destroy(response_payload_recv);
     129      101000 : }
     130             : 
     131      400000 : static double now(void) {
     132      400000 :   gpr_timespec tv = gpr_now(GPR_CLOCK_REALTIME);
     133      400000 :   return 1e9 * (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           2 : int main(int argc, char **argv) {
     148           2 :   gpr_slice slice = gpr_slice_from_copied_string("x");
     149             :   double start, stop;
     150             :   unsigned i;
     151             : 
     152             :   char *fake_argv[1];
     153             : 
     154           2 :   int payload_size = 1;
     155           2 :   int secure = 0;
     156           2 :   char *target = "localhost:443";
     157             :   gpr_cmdline *cl;
     158             :   grpc_event event;
     159           2 :   char *scenario_name = "ping-pong-request";
     160           2 :   scenario sc = {NULL, NULL, NULL};
     161             : 
     162           2 :   GPR_ASSERT(argc >= 1);
     163           2 :   fake_argv[0] = argv[0];
     164           2 :   grpc_test_init(1, fake_argv);
     165             : 
     166           2 :   grpc_init();
     167             : 
     168           2 :   cl = gpr_cmdline_create("fling client");
     169           2 :   gpr_cmdline_add_int(cl, "payload_size", "Size of the payload to send",
     170             :                       &payload_size);
     171           2 :   gpr_cmdline_add_string(cl, "target", "Target host:port", &target);
     172           2 :   gpr_cmdline_add_flag(cl, "secure", "Run with security?", &secure);
     173           2 :   gpr_cmdline_add_string(cl, "scenario", "Scenario", &scenario_name);
     174           2 :   gpr_cmdline_parse(cl, argc, argv);
     175           2 :   gpr_cmdline_destroy(cl);
     176             : 
     177           6 :   for (i = 0; i < GPR_ARRAY_SIZE(scenarios); i++) {
     178           4 :     if (0 == strcmp(scenarios[i].name, scenario_name)) {
     179           2 :       sc = scenarios[i];
     180             :     }
     181             :   }
     182           2 :   if (!sc.name) {
     183           0 :     fprintf(stderr, "unsupported scenario '%s'. Valid are:", scenario_name);
     184           0 :     for (i = 0; i < GPR_ARRAY_SIZE(scenarios); i++) {
     185           0 :       fprintf(stderr, " %s", scenarios[i].name);
     186             :     }
     187           0 :     return 1;
     188             :   }
     189             : 
     190           2 :   channel = grpc_insecure_channel_create(target, NULL, NULL);
     191           2 :   cq = grpc_completion_queue_create(NULL);
     192           2 :   the_buffer = grpc_raw_byte_buffer_create(&slice, (size_t)payload_size);
     193           2 :   histogram = gpr_histogram_create(0.01, 60e9);
     194             : 
     195           2 :   sc.init();
     196             : 
     197        2002 :   for (i = 0; i < 1000; i++) {
     198        2000 :     sc.do_one_step();
     199             :   }
     200             : 
     201           2 :   gpr_log(GPR_INFO, "start profiling");
     202           2 :   grpc_profiler_start("client.prof");
     203      200002 :   for (i = 0; i < 100000; i++) {
     204      200000 :     start = now();
     205      200000 :     sc.do_one_step();
     206      200000 :     stop = now();
     207      200000 :     gpr_histogram_add(histogram, stop - start);
     208             :   }
     209           2 :   grpc_profiler_stop();
     210             : 
     211           2 :   if (call) {
     212           1 :     grpc_call_destroy(call);
     213             :   }
     214             : 
     215           2 :   grpc_channel_destroy(channel);
     216           2 :   grpc_completion_queue_shutdown(cq);
     217             :   do {
     218           2 :     event = grpc_completion_queue_next(cq, gpr_inf_future(GPR_CLOCK_REALTIME),
     219             :                                        NULL);
     220           2 :   } while (event.type != GRPC_QUEUE_SHUTDOWN);
     221           2 :   grpc_completion_queue_destroy(cq);
     222           2 :   grpc_byte_buffer_destroy(the_buffer);
     223           2 :   gpr_slice_unref(slice);
     224             : 
     225           2 :   gpr_log(GPR_INFO, "latency (50/95/99/99.9): %f/%f/%f/%f",
     226             :           gpr_histogram_percentile(histogram, 50),
     227             :           gpr_histogram_percentile(histogram, 95),
     228             :           gpr_histogram_percentile(histogram, 99),
     229             :           gpr_histogram_percentile(histogram, 99.9));
     230           2 :   gpr_histogram_destroy(histogram);
     231             : 
     232           2 :   grpc_shutdown();
     233             : 
     234           2 :   return 0;
     235             : }

Generated by: LCOV version 1.10