The C based gRPC (C++, Python, Ruby, Objective-C, PHP, C#) https://grpc.io/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

260 lines
8.9 KiB

10 years ago
/*
*
* Copyright 2015, Google Inc.
10 years ago
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following disclaimer
* in the documentation and/or other materials provided with the
* distribution.
* * Neither the name of Google Inc. nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*/
#include <grpc/grpc.h>
#include <stdio.h>
#include <string.h>
#include <grpc/support/cmdline.h>
#include <grpc/support/histogram.h>
#include <grpc/support/log.h>
#include <grpc/support/time.h>
#include <grpc/support/useful.h>
9 years ago
#include "src/core/lib/profiling/timers.h"
10 years ago
#include "test/core/util/grpc_profiler.h"
#include "test/core/util/test_config.h"
static gpr_histogram *histogram;
static grpc_byte_buffer *the_buffer;
static grpc_channel *channel;
static grpc_completion_queue *cq;
static grpc_call *call;
static grpc_op ops[6];
static grpc_op stream_init_ops[2];
static grpc_op stream_step_ops[2];
static grpc_metadata_array initial_metadata_recv;
static grpc_metadata_array trailing_metadata_recv;
static grpc_byte_buffer *response_payload_recv = NULL;
static grpc_status_code status;
static grpc_slice details;
static grpc_op *op;
10 years ago
static void init_ping_pong_request(void) {
grpc_metadata_array_init(&initial_metadata_recv);
grpc_metadata_array_init(&trailing_metadata_recv);
memset(ops, 0, sizeof(ops));
op = ops;
op->op = GRPC_OP_SEND_INITIAL_METADATA;
op->data.send_initial_metadata.count = 0;
op++;
op->op = GRPC_OP_SEND_MESSAGE;
8 years ago
op->data.send_message.send_message = the_buffer;
op++;
op->op = GRPC_OP_SEND_CLOSE_FROM_CLIENT;
op++;
op->op = GRPC_OP_RECV_INITIAL_METADATA;
8 years ago
op->data.recv_initial_metadata.recv_initial_metadata = &initial_metadata_recv;
op++;
op->op = GRPC_OP_RECV_MESSAGE;
8 years ago
op->data.recv_message.recv_message = &response_payload_recv;
op++;
op->op = GRPC_OP_RECV_STATUS_ON_CLIENT;
op->data.recv_status_on_client.trailing_metadata = &trailing_metadata_recv;
op->data.recv_status_on_client.status = &status;
op->data.recv_status_on_client.status_details = &details;
op++;
}
10 years ago
10 years ago
static void step_ping_pong_request(void) {
GPR_TIMER_BEGIN("ping_pong", 1);
grpc_slice host = grpc_slice_from_static_string("localhost");
call = grpc_channel_create_call(
channel, NULL, GRPC_PROPAGATE_DEFAULTS, cq,
grpc_slice_from_static_string("/Reflector/reflectUnary"), &host,
gpr_inf_future(GPR_CLOCK_REALTIME), NULL);
GPR_ASSERT(GRPC_CALL_OK == grpc_call_start_batch(call, ops,
(size_t)(op - ops),
(void *)1, NULL));
grpc_completion_queue_next(cq, gpr_inf_future(GPR_CLOCK_REALTIME), NULL);
10 years ago
grpc_call_destroy(call);
grpc_byte_buffer_destroy(response_payload_recv);
10 years ago
call = NULL;
GPR_TIMER_END("ping_pong", 1);
10 years ago
}
10 years ago
static void init_ping_pong_stream(void) {
grpc_metadata_array_init(&initial_metadata_recv);
grpc_call_error error;
grpc_slice host = grpc_slice_from_static_string("localhost");
call = grpc_channel_create_call(
channel, NULL, GRPC_PROPAGATE_DEFAULTS, cq,
grpc_slice_from_static_string("/Reflector/reflectStream"), &host,
gpr_inf_future(GPR_CLOCK_REALTIME), NULL);
stream_init_ops[0].op = GRPC_OP_SEND_INITIAL_METADATA;
stream_init_ops[0].data.send_initial_metadata.count = 0;
stream_init_ops[1].op = GRPC_OP_RECV_INITIAL_METADATA;
8 years ago
stream_init_ops[1].data.recv_initial_metadata.recv_initial_metadata =
&initial_metadata_recv;
error = grpc_call_start_batch(call, stream_init_ops, 2, (void *)1, NULL);
GPR_ASSERT(GRPC_CALL_OK == error);
grpc_completion_queue_next(cq, gpr_inf_future(GPR_CLOCK_REALTIME), NULL);
grpc_metadata_array_init(&initial_metadata_recv);
stream_step_ops[0].op = GRPC_OP_SEND_MESSAGE;
8 years ago
stream_step_ops[0].data.send_message.send_message = the_buffer;
stream_step_ops[1].op = GRPC_OP_RECV_MESSAGE;
8 years ago
stream_step_ops[1].data.recv_message.recv_message = &response_payload_recv;
10 years ago
}
10 years ago
static void step_ping_pong_stream(void) {
grpc_call_error error;
GPR_TIMER_BEGIN("ping_pong", 1);
error = grpc_call_start_batch(call, stream_step_ops, 2, (void *)1, NULL);
GPR_ASSERT(GRPC_CALL_OK == error);
grpc_completion_queue_next(cq, gpr_inf_future(GPR_CLOCK_REALTIME), NULL);
grpc_byte_buffer_destroy(response_payload_recv);
GPR_TIMER_END("ping_pong", 1);
10 years ago
}
10 years ago
static double now(void) {
gpr_timespec tv = gpr_now(GPR_CLOCK_REALTIME);
return 1e9 * (double)tv.tv_sec + tv.tv_nsec;
10 years ago
}
typedef struct {
const char *name;
void (*init)();
void (*do_one_step)();
} scenario;
static const scenario scenarios[] = {
{"ping-pong-request", init_ping_pong_request, step_ping_pong_request},
10 years ago
{"ping-pong-stream", init_ping_pong_stream, step_ping_pong_stream},
};
10 years ago
int main(int argc, char **argv) {
grpc_slice slice = grpc_slice_from_copied_string("x");
10 years ago
double start, stop;
unsigned i;
10 years ago
char *fake_argv[1];
int payload_size = 1;
int secure = 0;
char *target = "localhost:443";
gpr_cmdline *cl;
grpc_event event;
10 years ago
char *scenario_name = "ping-pong-request";
10 years ago
scenario sc = {NULL, NULL, NULL};
10 years ago
gpr_timers_set_log_filename("latency_trace.fling_client.txt");
grpc_init();
10 years ago
GPR_ASSERT(argc >= 1);
fake_argv[0] = argv[0];
grpc_test_init(1, fake_argv);
int warmup_seconds = 1;
int benchmark_seconds = 5;
10 years ago
cl = gpr_cmdline_create("fling client");
gpr_cmdline_add_int(cl, "payload_size", "Size of the payload to send",
&payload_size);
gpr_cmdline_add_string(cl, "target", "Target host:port", &target);
gpr_cmdline_add_flag(cl, "secure", "Run with security?", &secure);
gpr_cmdline_add_string(cl, "scenario", "Scenario", &scenario_name);
gpr_cmdline_add_int(cl, "warmup", "Warmup seconds", &warmup_seconds);
gpr_cmdline_add_int(cl, "benchmark", "Benchmark seconds", &benchmark_seconds);
10 years ago
gpr_cmdline_parse(cl, argc, argv);
gpr_cmdline_destroy(cl);
for (i = 0; i < GPR_ARRAY_SIZE(scenarios); i++) {
if (0 == strcmp(scenarios[i].name, scenario_name)) {
sc = scenarios[i];
}
}
if (!sc.name) {
fprintf(stderr, "unsupported scenario '%s'. Valid are:", scenario_name);
for (i = 0; i < GPR_ARRAY_SIZE(scenarios); i++) {
fprintf(stderr, " %s", scenarios[i].name);
}
return 1;
}
Merge branch 'master' of github.com:grpc/grpc into the-ultimate-showdown Conflicts: include/grpc/grpc.h src/core/surface/channel.c src/core/surface/channel_create.c src/core/surface/completion_queue.c src/cpp/client/channel.cc src/cpp/client/insecure_credentials.cc src/csharp/ext/grpc_csharp_ext.c src/node/ext/call.cc src/node/ext/channel.cc src/php/ext/grpc/call.c src/php/ext/grpc/channel.c src/python/grpcio/grpc/_adapter/_c/types/channel.c src/ruby/ext/grpc/rb_channel.c test/core/end2end/dualstack_socket_test.c test/core/end2end/fixtures/chttp2_fullstack.c test/core/end2end/fixtures/chttp2_fullstack_compression.c test/core/end2end/fixtures/chttp2_fullstack_uds_posix.c test/core/end2end/fixtures/chttp2_fullstack_with_poll.c test/core/end2end/multiple_server_queues_test.c test/core/end2end/no_server_test.c test/core/end2end/tests/bad_hostname.c test/core/end2end/tests/cancel_after_accept.c test/core/end2end/tests/cancel_after_accept_and_writes_closed.c test/core/end2end/tests/cancel_after_invoke.c test/core/end2end/tests/cancel_before_invoke.c test/core/end2end/tests/cancel_in_a_vacuum.c test/core/end2end/tests/census_simple_request.c test/core/end2end/tests/disappearing_server.c test/core/end2end/tests/early_server_shutdown_finishes_inflight_calls.c test/core/end2end/tests/empty_batch.c test/core/end2end/tests/graceful_server_shutdown.c test/core/end2end/tests/invoke_large_request.c test/core/end2end/tests/max_concurrent_streams.c test/core/end2end/tests/max_message_length.c test/core/end2end/tests/ping_pong_streaming.c test/core/end2end/tests/registered_call.c test/core/end2end/tests/request_response_with_binary_metadata_and_payload.c test/core/end2end/tests/request_response_with_metadata_and_payload.c test/core/end2end/tests/request_response_with_payload.c test/core/end2end/tests/request_response_with_payload_and_call_creds.c test/core/end2end/tests/request_response_with_trailing_metadata_and_payload.c test/core/end2end/tests/request_with_compressed_payload.c test/core/end2end/tests/request_with_flags.c test/core/end2end/tests/request_with_large_metadata.c test/core/end2end/tests/request_with_payload.c test/core/end2end/tests/server_finishes_request.c test/core/end2end/tests/simple_delayed_request.c test/core/end2end/tests/simple_request.c test/core/end2end/tests/simple_request_with_high_initial_sequence_number.c test/core/fling/client.c test/core/fling/server.c test/core/surface/lame_client_test.c
9 years ago
channel = grpc_insecure_channel_create(target, NULL, NULL);
cq = grpc_completion_queue_create(GRPC_CQ_NEXT, DEFAULT_POLLING, NULL);
the_buffer = grpc_raw_byte_buffer_create(&slice, (size_t)payload_size);
10 years ago
histogram = gpr_histogram_create(0.01, 60e9);
10 years ago
sc.init();
gpr_timespec end_warmup = grpc_timeout_seconds_to_deadline(warmup_seconds);
gpr_timespec end_profiling =
grpc_timeout_seconds_to_deadline(warmup_seconds + benchmark_seconds);
9 years ago
while (gpr_time_cmp(gpr_now(end_warmup.clock_type), end_warmup) < 0) {
10 years ago
sc.do_one_step();
}
gpr_log(GPR_INFO, "start profiling");
grpc_profiler_start("client.prof");
while (gpr_time_cmp(gpr_now(end_profiling.clock_type), end_profiling) < 0) {
10 years ago
start = now();
sc.do_one_step();
stop = now();
gpr_histogram_add(histogram, stop - start);
}
grpc_profiler_stop();
if (call) {
grpc_call_destroy(call);
}
grpc_channel_destroy(channel);
grpc_completion_queue_shutdown(cq);
do {
event = grpc_completion_queue_next(cq, gpr_inf_future(GPR_CLOCK_REALTIME),
NULL);
} while (event.type != GRPC_QUEUE_SHUTDOWN);
10 years ago
grpc_completion_queue_destroy(cq);
grpc_byte_buffer_destroy(the_buffer);
grpc_slice_unref(slice);
10 years ago
gpr_log(GPR_INFO, "latency (50/95/99/99.9): %f/%f/%f/%f",
gpr_histogram_percentile(histogram, 50),
gpr_histogram_percentile(histogram, 95),
gpr_histogram_percentile(histogram, 99),
gpr_histogram_percentile(histogram, 99.9));
gpr_histogram_destroy(histogram);
grpc_shutdown();
return 0;
}