Exemple #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();
}
Exemple #2
0
static void server_init_call_elem(grpc_call_element* elem,
                                  const void* server_transport_data) {
  call_data* d = elem->call_data;
  GPR_ASSERT(d != NULL);
  init_rpc_stats(&d->stats);
  d->start_ts = gpr_now();
  d->op_id = census_tracing_start_op();
}
Exemple #3
0
static void test_get_trace_method_name(void) {
  census_op_id id;
  const char write_name[] = "service/method";
  census_tracing_init();
  id = census_tracing_start_op();
  census_add_method_tag(id, write_name);
  census_internal_lock_trace_store();
  {
    const char* read_name =
        census_get_trace_method_name(census_get_trace_obj_locked(id));
    GPR_ASSERT(strcmp(read_name, write_name) == 0);
  }
  census_internal_unlock_trace_store();
  census_tracing_shutdown();
}
Exemple #4
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();
}
Exemple #5
0
static void test_start_op_generates_locally_unique_ids(void) {
/* Check that ids generated within window size of 1000 are unique.
   TODO(hongyu): Replace O(n^2) duplicate detection algorithm with O(nlogn)
   algorithm. Enhance the test to larger window size (>10^6) */
#define WINDOW_SIZE 1000
  census_op_id ids[WINDOW_SIZE];
  int i;
  census_init();
  for (i = 0; i < WINDOW_SIZE; i++) {
    ids[i] = census_tracing_start_op();
    census_tracing_end_op(ids[i]);
  }
  for (i = 0; i < WINDOW_SIZE - 1; i++) {
    int j;
    for (j = i + 1; j < WINDOW_SIZE; j++) {
      GPR_ASSERT(ids[i].upper != ids[j].upper || ids[i].lower != ids[j].lower);
    }
  }
#undef WINDOW_SIZE
  census_shutdown();
}
Exemple #6
0
static void test_trace_print(void) {
  census_op_id id;
  int i;
  const char* annotation_txt[4] = {"abc", "", "$%^ *()_"};
  char long_txt[CENSUS_MAX_ANNOTATION_LENGTH + 10];

  memset(long_txt, 'a', GPR_ARRAY_SIZE(long_txt));
  long_txt[CENSUS_MAX_ANNOTATION_LENGTH + 9] = '\0';
  annotation_txt[3] = long_txt;

  census_tracing_init();
  id = census_tracing_start_op();
  /* Adds large number of annotations to each trace */
  for (i = 0; i < 1000; i++) {
    census_tracing_print(id,
                         annotation_txt[i % GPR_ARRAY_SIZE(annotation_txt)]);
  }
  census_tracing_end_op(id);

  census_tracing_shutdown();
}
Exemple #7
0
static void mimic_trace_op_sequences(void* arg) {
  census_op_id id;
  const char* method_name = "service_foo/method_bar";
  int i = 0;
  const int num_iter = 200;
  thd_arg* args = (thd_arg*)arg;
  GPR_ASSERT(args != NULL);
  gpr_log(GPR_INFO, "Start trace op sequence thread.");
  for (i = 0; i < num_iter; i++) {
    id = census_tracing_start_op();
    census_add_method_tag(id, method_name);
    /* pretend doing 1us work. */
    gpr_sleep_until(GRPC_TIMEOUT_MICROS_TO_DEADLINE(1));
    census_tracing_end_op(id);
  }
  gpr_log(GPR_INFO, "End trace op sequence thread.");
  gpr_mu_lock(&args->mu);
  args->num_done += 1;
  gpr_cv_broadcast(&args->done);
  gpr_mu_unlock(&args->mu);
}
Exemple #8
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();
}
Exemple #9
0
static void test_get_active_ops(void) {
  census_op_id id_1, id_2, id_3;
  census_trace_obj** active_ops;
  const char* annotation_txt[] = {"annotation 1", "a2"};
  int i = 0;
  int n = 0;

  gpr_log(GPR_INFO, "test_get_active_ops");
  census_tracing_init();
  /* No active ops before calling start_op(). */
  active_ops = census_get_active_ops(&n);
  GPR_ASSERT(active_ops == NULL);
  GPR_ASSERT(n == 0);

  /* Starts one op */
  id_1 = census_tracing_start_op();
  census_add_method_tag(id_1, "foo_1");
  active_ops = census_get_active_ops(&n);
  GPR_ASSERT(active_ops != NULL);
  GPR_ASSERT(n == 1);
  GPR_ASSERT(ids_equal(active_ops[0]->id, id_1));
  census_trace_obj_destroy(active_ops[0]);
  gpr_free(active_ops);
  active_ops = NULL;

  /* Start the second and the third ops */
  id_2 = census_tracing_start_op();
  census_add_method_tag(id_2, "foo_2");
  id_3 = census_tracing_start_op();
  census_add_method_tag(id_3, "foo_3");

  active_ops = census_get_active_ops(&n);
  GPR_ASSERT(n == 3);
  for (i = 0; i < 3; i++) {
    census_trace_obj_destroy(active_ops[i]);
  }
  gpr_free(active_ops);
  active_ops = NULL;

  /* End the second op  and add annotations to the third ops*/
  census_tracing_end_op(id_2);
  census_tracing_print(id_3, annotation_txt[0]);
  census_tracing_print(id_3, annotation_txt[1]);

  active_ops = census_get_active_ops(&n);
  GPR_ASSERT(active_ops != NULL);
  GPR_ASSERT(n == 2);
  for (i = 0; i < 2; i++) {
    census_trace_obj_destroy(active_ops[i]);
  }
  gpr_free(active_ops);
  active_ops = NULL;

  /* End all ops. */
  census_tracing_end_op(id_1);
  census_tracing_end_op(id_3);
  active_ops = census_get_active_ops(&n);
  GPR_ASSERT(active_ops == NULL);
  GPR_ASSERT(n == 0);

  census_tracing_shutdown();
}