Ejemplo n.º 1
0
static void test_insertion_with_same_key(void) {
  census_ht_option opt = {CENSUS_HT_UINT64, 11, NULL, NULL, NULL, NULL};
  census_ht *ht = census_ht_create(&opt);
  census_ht_key key;
  const char vals[] = {'a', 'b', 'c'};
  char *val_ptr;
  key.val = 3;
  census_ht_insert(ht, key, (void *)&(vals[0]));
  GPR_ASSERT(census_ht_get_size(ht) == 1);
  val_ptr = (char *)census_ht_find(ht, key);
  GPR_ASSERT(val_ptr != NULL);
  GPR_ASSERT(*val_ptr == 'a');
  key.val = 4;
  val_ptr = (char *)census_ht_find(ht, key);
  GPR_ASSERT(val_ptr == NULL);
  key.val = 3;
  census_ht_insert(ht, key, (void *)&(vals[1]));
  GPR_ASSERT(census_ht_get_size(ht) == 1);
  val_ptr = (char *)census_ht_find(ht, key);
  GPR_ASSERT(val_ptr != NULL);
  GPR_ASSERT(*val_ptr == 'b');
  census_ht_insert(ht, key, (void *)&(vals[2]));
  GPR_ASSERT(census_ht_get_size(ht) == 1);
  val_ptr = (char *)census_ht_find(ht, key);
  GPR_ASSERT(val_ptr != NULL);
  GPR_ASSERT(*val_ptr == 'c');
  census_ht_destroy(ht);
}
Ejemplo n.º 2
0
/* Test simple insert and erase operations. */
static void test_simple_add_and_erase(void) {
  census_ht_option opt = {CENSUS_HT_UINT64, 7, NULL, NULL, NULL, NULL};
  census_ht *ht = census_ht_create(&opt);
  GPR_ASSERT(ht != NULL);
  GPR_ASSERT(census_ht_get_size(ht) == 0);
  {
    census_ht_key key;
    int val = 3;
    key.val = 2;
    census_ht_insert(ht, key, (void *)&val);
    GPR_ASSERT(census_ht_get_size(ht) == 1);
    census_ht_erase(ht, key);
    GPR_ASSERT(census_ht_get_size(ht) == 0);
    /* Erasing a key from an empty table should be noop. */
    census_ht_erase(ht, key);
    GPR_ASSERT(census_ht_get_size(ht) == 0);
    /* Erasing a non-existant key from a table should be noop. */
    census_ht_insert(ht, key, (void *)&val);
    key.val = 3;
    census_ht_insert(ht, key, (void *)&val);
    key.val = 9;
    census_ht_insert(ht, key, (void *)&val);
    GPR_ASSERT(census_ht_get_size(ht) == 3);
    key.val = 1;
    census_ht_erase(ht, key);
    /* size unchanged after deleting non-existant key. */
    GPR_ASSERT(census_ht_get_size(ht) == 3);
    /* size decrease by 1 after deleting an existant key. */
    key.val = 2;
    census_ht_erase(ht, key);
    GPR_ASSERT(census_ht_get_size(ht) == 2);
  }
  census_ht_destroy(ht);
}
Ejemplo n.º 3
0
/* Test that there is no memory leak when keys and values are owned by table. */
static void test_value_and_key_deleter(void) {
  census_ht_option opt = {CENSUS_HT_POINTER, 7,          &hash64,
                          &cmp_str_keys,     &free_data, &free_data};
  census_ht *ht = census_ht_create(&opt);
  census_ht_key key;
  char *val = NULL;
  char *val2 = NULL;
  key.ptr = gpr_malloc(100);
  val = gpr_malloc(10);
  strcpy(val, "value");
  strcpy(key.ptr, "some string as a key");
  GPR_ASSERT(ht != NULL);
  GPR_ASSERT(census_ht_get_size(ht) == 0);
  census_ht_insert(ht, key, val);
  GPR_ASSERT(census_ht_get_size(ht) == 1);
  val = census_ht_find(ht, key);
  GPR_ASSERT(val != NULL);
  GPR_ASSERT(strcmp(val, "value") == 0);
  /* Insert same key different value, old value is overwritten. */
  val2 = gpr_malloc(10);
  strcpy(val2, "v2");
  census_ht_insert(ht, key, val2);
  GPR_ASSERT(census_ht_get_size(ht) == 1);
  val2 = census_ht_find(ht, key);
  GPR_ASSERT(val2 != NULL);
  GPR_ASSERT(strcmp(val2, "v2") == 0);
  census_ht_destroy(ht);
}
Ejemplo n.º 4
0
static void test_table_with_string_key(void) {
  census_ht_option opt = {CENSUS_HT_POINTER, 7,    &hash64,
                          &cmp_str_keys,     NULL, NULL};
  census_ht *ht = census_ht_create(&opt);
  const char *keys[] = {
      "k1", "a",   "000", "apple", "banana_a_long_long_long_banana",
      "%$", "111", "foo", "b"};
  const int vals[] = {0, 1, 2, 3, 4, 5, 6, 7, 8};
  int i = 0;
  GPR_ASSERT(ht != NULL);
  GPR_ASSERT(census_ht_get_size(ht) == 0);
  for (i = 0; i < 9; i++) {
    census_ht_key key;
    key.ptr = (void *)(keys[i]);
    census_ht_insert(ht, key, (void *)(vals + i));
  }
  GPR_ASSERT(census_ht_get_size(ht) == 9);
  for (i = 0; i < 9; i++) {
    census_ht_key key;
    int *val_ptr;
    key.ptr = (void *)(keys[i]);
    val_ptr = census_ht_find(ht, key);
    GPR_ASSERT(*val_ptr == vals[i]);
  }
  {
    /* inserts duplicate keys */
    census_ht_key key;
    int *val_ptr = NULL;
    key.ptr = (void *)(keys[2]);
    census_ht_insert(ht, key, (void *)(vals + 8));
    /* expect value to be over written by new insertion */
    GPR_ASSERT(census_ht_get_size(ht) == 9);
    val_ptr = census_ht_find(ht, key);
    GPR_ASSERT(*val_ptr == vals[8]);
  }
  for (i = 0; i < 9; i++) {
    census_ht_key key;
    int *val_ptr;
    uint32_t expected_tbl_sz = 9 - i;
    GPR_ASSERT(census_ht_get_size(ht) == expected_tbl_sz);
    key.ptr = (void *)(keys[i]);
    val_ptr = census_ht_find(ht, key);
    GPR_ASSERT(val_ptr != NULL);
    census_ht_erase(ht, key);
    GPR_ASSERT(census_ht_get_size(ht) == expected_tbl_sz - 1);
    val_ptr = census_ht_find(ht, key);
    GPR_ASSERT(val_ptr == NULL);
  }
  census_ht_destroy(ht);
}
Ejemplo n.º 5
0
static void test_table_with_int_key(void) {
  census_ht_option opt = {CENSUS_HT_UINT64, 7, NULL, NULL, NULL, NULL};
  census_ht *ht = census_ht_create(&opt);
  uint64_t i = 0;
  uint64_t sum_of_keys = 0;
  size_t num_elements;
  census_ht_kv *elements = NULL;
  GPR_ASSERT(ht != NULL);
  GPR_ASSERT(census_ht_get_size(ht) == 0);
  elements = census_ht_get_all_elements(ht, &num_elements);
  GPR_ASSERT(num_elements == 0);
  GPR_ASSERT(elements == NULL);
  for (i = 0; i < 20; ++i) {
    census_ht_key key;
    key.val = i;
    census_ht_insert(ht, key, (void *)(intptr_t)i);
    GPR_ASSERT(census_ht_get_size(ht) == i + 1);
  }
  for (i = 0; i < 20; i++) {
    uint64_t *val = NULL;
    census_ht_key key;
    key.val = i;
    val = census_ht_find(ht, key);
    GPR_ASSERT(val == (void *)(intptr_t)i);
  }
  elements = census_ht_get_all_elements(ht, &num_elements);
  GPR_ASSERT(elements != NULL);
  GPR_ASSERT(num_elements == 20);
  for (i = 0; i < num_elements; i++) {
    sum_of_keys += elements[i].k.val;
  }
  GPR_ASSERT(sum_of_keys == 190);
  gpr_free(elements);
  census_ht_destroy(ht);
}
Ejemplo n.º 6
0
static void record_stats(census_ht* store, census_op_id op_id,
                         const census_rpc_stats* stats) {
  gpr_mu_lock(&g_mu);
  if (store != NULL) {
    census_trace_obj* trace = NULL;
    census_internal_lock_trace_store();
    trace = census_get_trace_obj_locked(op_id);
    if (trace != NULL) {
      const char* method_name = census_get_trace_method_name(trace);
      struct census_window_stats* window_stats = NULL;
      census_ht_key key;
      key.ptr = (void*)method_name;
      window_stats = census_ht_find(store, key);
      census_internal_unlock_trace_store();
      if (window_stats == NULL) {
        window_stats = census_window_stats_create(3, min_hour_total_intervals,
                                                  30, &window_stats_settings);
        key.ptr = gpr_strdup(key.ptr);
        census_ht_insert(store, key, (void*)window_stats);
      }
      census_window_stats_add(window_stats, gpr_now(GPR_CLOCK_REALTIME), stats);
    } else {
      census_internal_unlock_trace_store();
    }
  }
  gpr_mu_unlock(&g_mu);
}
Ejemplo n.º 7
0
census_op_id census_tracing_start_op(void) {
  gpr_mu_lock(&g_mu);
  {
    census_trace_obj* ret = gpr_malloc(sizeof(census_trace_obj));
    memset(ret, 0, sizeof(census_trace_obj));
    g_id++;
    memcpy(&ret->id, &g_id, sizeof(census_op_id));
    ret->rpc_stats.cnt = 1;
    ret->ts = gpr_now(GPR_CLOCK_REALTIME);
    census_ht_insert(g_trace_store, op_id_as_key(&ret->id), (void*)ret);
    gpr_log(GPR_DEBUG, "Start tracing for id %lu", g_id);
    gpr_mu_unlock(&g_mu);
    return ret->id;
  }
}
Ejemplo n.º 8
0
static void test_insertion_and_deletion_with_high_collision_rate(void) {
  census_ht_option opt = {CENSUS_HT_POINTER, 13,   &force_collision,
                          &cmp_str_keys,     NULL, NULL};
  census_ht *ht = census_ht_create(&opt);
  char key_str[1000][GPR_LTOA_MIN_BUFSIZE];
  uint64_t val = 0;
  unsigned i = 0;
  for (i = 0; i < 1000; i++) {
    census_ht_key key;
    key.ptr = key_str[i];
    gpr_ltoa(i, key_str[i]);
    census_ht_insert(ht, key, (void *)(&val));
    gpr_log(GPR_INFO, "%d\n", i);
    GPR_ASSERT(census_ht_get_size(ht) == (i + 1));
  }
  for (i = 0; i < 1000; i++) {
    census_ht_key key;
    key.ptr = key_str[i];
    census_ht_erase(ht, key);
    GPR_ASSERT(census_ht_get_size(ht) == (999 - i));
  }
  census_ht_destroy(ht);
}