Example #1
0
/* Tests census noop stubs in a simulated rpc flow */
void test_census_stubs(void) {
  census_op_id op_id;
  census_rpc_stats* stats = census_rpc_stats_create_empty();
  census_aggregated_rpc_stats data_map = {0, NULL};

  /* Initializes census library at server start up time. */
  census_init();
  /* Starts tracing at the beginning of a rpc. */
  op_id = census_tracing_start_op();
  /* Appends custom annotations on a trace object. */
  census_tracing_print(op_id, "annotation foo");
  census_tracing_print(op_id, "annotation bar");
  /* Appends method tag on the trace object. */
  census_add_method_tag(op_id, "service_foo/method.bar");
  /* Either record client side stats or server side stats associated with the
     op_id. Here for testing purpose, we record both. */
  census_record_rpc_client_stats(op_id, stats);
  census_record_rpc_server_stats(op_id, stats);
  /* Ends a tracing. */
  census_tracing_end_op(op_id);
  /* In process stats queries. */
  census_get_server_stats(&data_map);
  census_aggregated_rpc_stats_set_empty(&data_map);
  census_get_client_stats(&data_map);
  census_aggregated_rpc_stats_set_empty(&data_map);
  gpr_free(stats);
  census_shutdown();
}
Example #2
0
/* Test that record stats is noop when trace store is uninitialized. */
static void test_record_stats_with_trace_store_uninitialized(void) {
  census_rpc_stats stats = {1, 2, 3, 4, 5.1, 6.2, 7.3, 8.4};
  census_op_id id = {0, 0};
  census_aggregated_rpc_stats agg_stats = {0, NULL};

  census_init();
  id = census_tracing_start_op();
  census_add_method_tag(id, "m");
  census_tracing_end_op(id);
  /* shuts down trace store only. */
  census_tracing_shutdown();
  census_record_rpc_client_stats(id, &stats);
  census_get_client_stats(&agg_stats);
  GPR_ASSERT(agg_stats.num_entries == 0);
  census_stats_store_shutdown();
}
Example #3
0
static void test_record_stats_on_unknown_op_id(void) {
  census_op_id unknown_id = {0xDEAD, 0xBEEF};
  census_rpc_stats stats = {1, 2, 3, 4, 5.1, 6.2, 7.3, 8.4};
  census_aggregated_rpc_stats agg_stats = {0, NULL};

  census_init();
  /* Tests that recording stats against unknown id is noop. */
  census_record_rpc_client_stats(unknown_id, &stats);
  census_record_rpc_server_stats(unknown_id, &stats);
  census_get_server_stats(&agg_stats);
  GPR_ASSERT(agg_stats.num_entries == 0);
  GPR_ASSERT(agg_stats.stats == NULL);
  census_get_client_stats(&agg_stats);
  GPR_ASSERT(agg_stats.num_entries == 0);
  GPR_ASSERT(agg_stats.stats == NULL);
  census_aggregated_rpc_stats_set_empty(&agg_stats);
  census_shutdown();
}
Example #4
0
static void test_record_and_get_stats(void) {
  census_rpc_stats stats = {1, 2, 3, 4, 5.1, 6.2, 7.3, 8.4};
  census_op_id id;
  census_aggregated_rpc_stats agg_stats = {0, NULL};

  /* Record client stats twice with the same op_id. */
  census_init();
  id = census_tracing_start_op();
  census_add_method_tag(id, "m1");
  census_record_rpc_client_stats(id, &stats);
  census_record_rpc_client_stats(id, &stats);
  census_tracing_end_op(id);
  /* Server stats expect to be empty */
  census_get_server_stats(&agg_stats);
  GPR_ASSERT(agg_stats.num_entries == 0);
  GPR_ASSERT(agg_stats.stats == NULL);
  /* Client stats expect to have one entry */
  census_get_client_stats(&agg_stats);
  GPR_ASSERT(agg_stats.num_entries == 1);
  GPR_ASSERT(agg_stats.stats != NULL);
  GPR_ASSERT(strcmp(agg_stats.stats[0].method, "m1") == 0);
  GPR_ASSERT(agg_stats.stats[0].minute_stats.cnt == 2 &&
             agg_stats.stats[0].hour_stats.cnt == 2 &&
             agg_stats.stats[0].total_stats.cnt == 2);
  ASSERT_NEAR(agg_stats.stats[0].minute_stats.wire_response_bytes, 16.8);
  ASSERT_NEAR(agg_stats.stats[0].hour_stats.wire_response_bytes, 16.8);
  ASSERT_NEAR(agg_stats.stats[0].total_stats.wire_response_bytes, 16.8);
  /* Get stats again, results should be the same. */
  census_get_client_stats(&agg_stats);
  GPR_ASSERT(agg_stats.num_entries == 1);
  census_aggregated_rpc_stats_set_empty(&agg_stats);
  census_shutdown();

  /* Record both server (once) and client (twice) stats with different op_ids.
   */
  census_init();
  id = census_tracing_start_op();
  census_add_method_tag(id, "m2");
  census_record_rpc_client_stats(id, &stats);
  census_tracing_end_op(id);
  id = census_tracing_start_op();
  census_add_method_tag(id, "m3");
  census_record_rpc_server_stats(id, &stats);
  census_tracing_end_op(id);
  id = census_tracing_start_op();
  census_add_method_tag(id, "m4");
  census_record_rpc_client_stats(id, &stats);
  census_tracing_end_op(id);
  /* Check server stats */
  census_get_server_stats(&agg_stats);
  GPR_ASSERT(agg_stats.num_entries == 1);
  GPR_ASSERT(strcmp(agg_stats.stats[0].method, "m3") == 0);
  GPR_ASSERT(agg_stats.stats[0].minute_stats.app_error_cnt == 3 &&
             agg_stats.stats[0].hour_stats.app_error_cnt == 3 &&
             agg_stats.stats[0].total_stats.app_error_cnt == 3);
  census_aggregated_rpc_stats_set_empty(&agg_stats);
  /* Check client stats */
  census_get_client_stats(&agg_stats);
  GPR_ASSERT(agg_stats.num_entries == 2);
  GPR_ASSERT(agg_stats.stats != NULL);
  GPR_ASSERT((strcmp(agg_stats.stats[0].method, "m2") == 0 &&
              strcmp(agg_stats.stats[1].method, "m4") == 0) ||
             (strcmp(agg_stats.stats[0].method, "m4") == 0 &&
              strcmp(agg_stats.stats[1].method, "m2") == 0));
  GPR_ASSERT(agg_stats.stats[0].minute_stats.cnt == 1 &&
             agg_stats.stats[1].minute_stats.cnt == 1);
  census_aggregated_rpc_stats_set_empty(&agg_stats);
  census_shutdown();
}