static void test_2dash_eq_int(void) { int x = 1; gpr_cmdline *cl; char *args[] = {(char *)__FILE__, "--foo=3"}; LOG_TEST(); cl = gpr_cmdline_create(NULL); gpr_cmdline_add_int(cl, "foo", NULL, &x); GPR_ASSERT(x == 1); gpr_cmdline_parse(cl, GPR_ARRAY_SIZE(args), args); GPR_ASSERT(x == 3); gpr_cmdline_destroy(cl); }
static void test_many(void) { char *str = NULL; int x = 0; int flag = 2; gpr_cmdline *cl; char *args[] = {(char *)__FILE__, "--str", "hello", "-x=4", "-no-flag"}; LOG_TEST(); cl = gpr_cmdline_create(NULL); gpr_cmdline_add_string(cl, "str", NULL, &str); gpr_cmdline_add_int(cl, "x", NULL, &x); gpr_cmdline_add_flag(cl, "flag", NULL, &flag); gpr_cmdline_parse(cl, GPR_ARRAY_SIZE(args), args); GPR_ASSERT(x == 4); GPR_ASSERT(0 == strcmp(str, "hello")); GPR_ASSERT(flag == 0); gpr_cmdline_destroy(cl); }
static void test_usage(void) { gpr_cmdline *cl; char *usage; char *str = NULL; int x = 0; int flag = 2; cl = gpr_cmdline_create(NULL); gpr_cmdline_add_string(cl, "str", NULL, &str); gpr_cmdline_add_int(cl, "x", NULL, &x); gpr_cmdline_add_flag(cl, "flag", NULL, &flag); usage = gpr_cmdline_usage_string(cl, "test"); GPR_ASSERT(0 == strcmp(usage, "Usage: test [--str=string] [--x=int] [--flag|--no-flag]\n")); gpr_free(usage); gpr_cmdline_destroy(cl); }
int main(int argc, char **argv) { gpr_slice slice = gpr_slice_from_copied_string("x"); double start, stop; unsigned i; char *fake_argv[1]; int payload_size = 1; int secure = 0; char *target = "localhost:443"; gpr_cmdline *cl; char *scenario_name = "ping-pong-request"; scenario sc = {NULL, NULL, NULL}; GPR_ASSERT(argc >= 1); fake_argv[0] = argv[0]; grpc_test_init(1, fake_argv); grpc_init(); 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_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; } channel = grpc_channel_create(target, NULL); cq = grpc_completion_queue_create(); the_buffer = grpc_raw_byte_buffer_create(&slice, payload_size); histogram = gpr_histogram_create(0.01, 60e9); sc.init(); for (i = 0; i < 1000; i++) { sc.do_one_step(); } gpr_log(GPR_INFO, "start profiling"); grpc_profiler_start("client.prof"); for (i = 0; i < 100000; i++) { 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); while (grpc_completion_queue_next(cq, gpr_inf_future).type != GRPC_QUEUE_SHUTDOWN) ; grpc_completion_queue_destroy(cq); grpc_byte_buffer_destroy(the_buffer); gpr_slice_unref(slice); 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; }
int main(int argc, char **argv) { grpc_memory_counters_init(); grpc_slice slice = grpc_slice_from_copied_string("x"); char *fake_argv[1]; char *target = "localhost:443"; gpr_cmdline *cl; grpc_event event; grpc_init(); GPR_ASSERT(argc >= 1); fake_argv[0] = argv[0]; grpc_test_init(1, fake_argv); int warmup_iterations = 100; int benchmark_iterations = 1000; cl = gpr_cmdline_create("memory profiling client"); gpr_cmdline_add_string(cl, "target", "Target host:port", &target); gpr_cmdline_add_int(cl, "warmup", "Warmup iterations", &warmup_iterations); gpr_cmdline_add_int(cl, "benchmark", "Benchmark iterations", &benchmark_iterations); gpr_cmdline_parse(cl, argc, argv); gpr_cmdline_destroy(cl); for (size_t k = 0; k < GPR_ARRAY_SIZE(calls); k++) { calls[k].details = grpc_empty_slice(); } cq = grpc_completion_queue_create(NULL); struct grpc_memory_counters client_channel_start = grpc_memory_counters_snapshot(); channel = grpc_insecure_channel_create(target, NULL, NULL); int call_idx = 0; struct grpc_memory_counters before_server_create = send_snapshot_request( 0, grpc_slice_from_static_string("Reflector/GetBeforeSvrCreation")); struct grpc_memory_counters after_server_create = send_snapshot_request( 0, grpc_slice_from_static_string("Reflector/GetAfterSvrCreation")); // warmup period for (call_idx = 0; call_idx < warmup_iterations; ++call_idx) { init_ping_pong_request(call_idx + 1); } struct grpc_memory_counters server_benchmark_calls_start = send_snapshot_request( 0, grpc_slice_from_static_string("Reflector/SimpleSnapshot")); struct grpc_memory_counters client_benchmark_calls_start = grpc_memory_counters_snapshot(); // benchmark period for (; call_idx < warmup_iterations + benchmark_iterations; ++call_idx) { init_ping_pong_request(call_idx + 1); } struct grpc_memory_counters client_calls_inflight = grpc_memory_counters_snapshot(); struct grpc_memory_counters server_calls_inflight = send_snapshot_request( 0, grpc_slice_from_static_string("Reflector/DestroyCalls")); do { event = grpc_completion_queue_next( cq, gpr_time_add(gpr_now(GPR_CLOCK_REALTIME), gpr_time_from_micros(10000, GPR_TIMESPAN)), NULL); } while (event.type != GRPC_QUEUE_TIMEOUT); // second step - recv status and destroy call for (call_idx = 0; call_idx < warmup_iterations + benchmark_iterations; ++call_idx) { finish_ping_pong_request(call_idx + 1); } struct grpc_memory_counters server_calls_end = send_snapshot_request( 0, grpc_slice_from_static_string("Reflector/SimpleSnapshot")); struct grpc_memory_counters client_channel_end = grpc_memory_counters_snapshot(); 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); grpc_slice_unref(slice); grpc_completion_queue_destroy(cq); grpc_shutdown(); gpr_log(GPR_INFO, "---------client stats--------"); gpr_log(GPR_INFO, "client call memory usage: %f bytes per call", (double)(client_calls_inflight.total_size_relative - client_benchmark_calls_start.total_size_relative) / benchmark_iterations); gpr_log(GPR_INFO, "client channel memory usage %zi bytes", client_channel_end.total_size_relative - client_channel_start.total_size_relative); gpr_log(GPR_INFO, "---------server stats--------"); gpr_log(GPR_INFO, "server create: %zi bytes", after_server_create.total_size_relative - before_server_create.total_size_relative); gpr_log(GPR_INFO, "server call memory usage: %f bytes per call", (double)(server_calls_inflight.total_size_relative - server_benchmark_calls_start.total_size_relative) / benchmark_iterations); gpr_log(GPR_INFO, "server channel memory usage %zi bytes", server_calls_end.total_size_relative - after_server_create.total_size_relative); grpc_memory_counters_destroy(); return 0; }
int main(int argc, char **argv) { thread_args *client_args = malloc(sizeof(thread_args)); thread_args *server_args = malloc(sizeof(thread_args)); int msg_size = -1; char *read_strategy = NULL; char *socket_type = NULL; size_t i; const test_strategy *strategy = NULL; int error = 0; gpr_cmdline *cmdline = gpr_cmdline_create("low_level_ping_pong network benchmarking tool"); gpr_cmdline_add_int(cmdline, "msg_size", "Size of sent messages", &msg_size); gpr_cmdline_add_string(cmdline, "read_strategy", read_strategy_usage, &read_strategy); gpr_cmdline_add_string(cmdline, "socket_type", socket_type_usage, &socket_type); gpr_cmdline_parse(cmdline, argc, argv); if (msg_size == -1) { msg_size = 50; } if (read_strategy == NULL) { gpr_log(GPR_INFO, "No strategy specified, running all benchmarks"); return run_all_benchmarks((size_t)msg_size); } if (socket_type == NULL) { socket_type = "tcp"; } if (msg_size <= 0) { fprintf(stderr, "msg_size must be > 0\n"); print_usage(argv[0]); return -1; } for (i = 0; i < GPR_ARRAY_SIZE(test_strategies); ++i) { if (strcmp(test_strategies[i].name, read_strategy) == 0) { strategy = &test_strategies[i]; } } if (strategy == NULL) { fprintf(stderr, "Invalid read strategy %s\n", read_strategy); return -1; } client_args->read_bytes = strategy->read_strategy; client_args->write_bytes = blocking_write_bytes; client_args->setup = strategy->setup; client_args->msg_size = (size_t)msg_size; client_args->strategy_name = read_strategy; server_args->read_bytes = strategy->read_strategy; server_args->write_bytes = blocking_write_bytes; server_args->setup = strategy->setup; server_args->msg_size = (size_t)msg_size; server_args->strategy_name = read_strategy; error = run_benchmark(socket_type, client_args, server_args); gpr_cmdline_destroy(cmdline); return error; }