static void test_merge(void) { gpr_histogram *h1, *h2; double last; double i; double cur; LOG_TEST("test_merge"); h1 = gpr_histogram_create(0.05, 1e9); gpr_histogram_add(h1, 2.5); gpr_histogram_add(h1, 2.5); gpr_histogram_add(h1, 8); gpr_histogram_add(h1, 4); h2 = gpr_histogram_create(0.01, 1e9); GPR_ASSERT(gpr_histogram_merge(h1, h2) == 0); gpr_histogram_destroy(h2); h2 = gpr_histogram_create(0.05, 1e10); GPR_ASSERT(gpr_histogram_merge(h1, h2) == 0); gpr_histogram_destroy(h2); h2 = gpr_histogram_create(0.05, 1e9); GPR_ASSERT(gpr_histogram_merge(h1, h2) == 1); GPR_ASSERT(gpr_histogram_count(h1) == 4); GPR_ASSERT(gpr_histogram_minimum(h1) == 2.5); GPR_ASSERT(gpr_histogram_maximum(h1) == 8); GPR_ASSERT(gpr_histogram_sum(h1) == 17); GPR_ASSERT(gpr_histogram_sum_of_squares(h1) == 92.5); GPR_ASSERT(gpr_histogram_mean(h1) == 4.25); GPR_ASSERT(gpr_histogram_variance(h1) == 5.0625); GPR_ASSERT(gpr_histogram_stddev(h1) == 2.25); gpr_histogram_destroy(h2); h2 = gpr_histogram_create(0.05, 1e9); gpr_histogram_add(h2, 7.0); gpr_histogram_add(h2, 17.0); gpr_histogram_add(h2, 1.0); GPR_ASSERT(gpr_histogram_merge(h1, h2) == 1); GPR_ASSERT(gpr_histogram_count(h1) == 7); GPR_ASSERT(gpr_histogram_minimum(h1) == 1.0); GPR_ASSERT(gpr_histogram_maximum(h1) == 17.0); GPR_ASSERT(gpr_histogram_sum(h1) == 42.0); GPR_ASSERT(gpr_histogram_sum_of_squares(h1) == 431.5); GPR_ASSERT(gpr_histogram_mean(h1) == 6.0); /* test monotonicity */ last = 0.0; for (i = 0; i < 100.0; i += 0.01) { cur = gpr_histogram_percentile(h1, i); GPR_ASSERT(cur >= last); last = cur; } gpr_histogram_destroy(h1); gpr_histogram_destroy(h2); }
static void client_thread(thread_args *args) { char *buf = calloc(args->msg_size, sizeof(char)); gpr_histogram *histogram = gpr_histogram_create(0.01, 60e9); double start_time; double end_time; double interval; const int kNumIters = 100000; int i; if (args->setup(args) < 0) { gpr_log(GPR_ERROR, "Setup failed"); } for (i = 0; i < kNumIters; ++i) { start_time = now(); if (args->write_bytes(args, buf) < 0) { gpr_log(GPR_ERROR, "Client write failed"); goto error; } if (args->read_bytes(args, buf) < 0) { gpr_log(GPR_ERROR, "Client read failed"); goto error; } end_time = now(); if (i > kNumIters / 2) { interval = end_time - start_time; gpr_histogram_add(histogram, interval); } } print_histogram(histogram); error: free(buf); gpr_histogram_destroy(histogram); }
static void test_simple(void) { gpr_histogram *h; LOG_TEST("test_simple"); h = gpr_histogram_create(0.01, 60e9); gpr_histogram_add(h, 10000); gpr_histogram_add(h, 10000); gpr_histogram_add(h, 11000); gpr_histogram_add(h, 11000); expect_percentile(h, 50, 10001, 10999); GPR_ASSERT(gpr_histogram_mean(h) == 10500); gpr_histogram_destroy(h); }
static void test_percentile(void) { gpr_histogram *h; double last; double i; double cur; LOG_TEST("test_percentile"); h = gpr_histogram_create(0.05, 1e9); gpr_histogram_add(h, 2.5); gpr_histogram_add(h, 2.5); gpr_histogram_add(h, 8); gpr_histogram_add(h, 4); GPR_ASSERT(gpr_histogram_count(h) == 4); GPR_ASSERT(gpr_histogram_minimum(h) == 2.5); GPR_ASSERT(gpr_histogram_maximum(h) == 8); GPR_ASSERT(gpr_histogram_sum(h) == 17); GPR_ASSERT(gpr_histogram_sum_of_squares(h) == 92.5); GPR_ASSERT(gpr_histogram_mean(h) == 4.25); GPR_ASSERT(gpr_histogram_variance(h) == 5.0625); GPR_ASSERT(gpr_histogram_stddev(h) == 2.25); expect_percentile(h, -10, 2.5, 2.5); expect_percentile(h, 0, 2.5, 2.5); expect_percentile(h, 12.5, 2.5, 2.5); expect_percentile(h, 25, 2.5, 2.5); expect_percentile(h, 37.5, 2.5, 2.8); expect_percentile(h, 50, 3.0, 3.5); expect_percentile(h, 62.5, 3.5, 4.5); expect_percentile(h, 75, 5, 7.9); expect_percentile(h, 100, 8, 8); expect_percentile(h, 110, 8, 8); /* test monotonicity */ last = 0.0; for (i = 0; i < 100.0; i += 0.01) { cur = gpr_histogram_percentile(h, i); GPR_ASSERT(cur >= last); last = cur; } gpr_histogram_destroy(h); }
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; }
static void test_no_op(void) { gpr_histogram_destroy(gpr_histogram_create(0.01, 60e9)); }