static char* test_recorded_values() { load_histograms(); struct hdr_recorded_iter iter; int index; // Raw Histogram hdr_recorded_iter_init(&iter, raw_histogram); index = 0; while (hdr_recorded_iter_next(&iter)) { int64_t count_added_in_this_bucket = iter.count_added_in_this_iteration_step; if (index == 0) { mu_assert("Value at 0 is not 10000", count_added_in_this_bucket == 10000); } else { mu_assert("Value at 1 is not 1", count_added_in_this_bucket == 1); } index++; } mu_assert("Should have encountered 2 values", index == 2); // Corrected Histogram hdr_recorded_iter_init(&iter, cor_histogram); index = 0; int64_t total_added_count = 0; while (hdr_recorded_iter_next(&iter)) { int64_t count_added_in_this_bucket = iter.count_added_in_this_iteration_step; if (index == 0) { mu_assert("Count at 0 is not 10000", count_added_in_this_bucket == 10000); } mu_assert("Count should not be 0", iter.iter.count_at_index != 0); mu_assert("Count at value iterated to should be count added in this step", iter.iter.count_at_index == count_added_in_this_bucket); total_added_count += count_added_in_this_bucket; index++; } mu_assert("Total counts should be 20000", total_added_count == 20000); return 0; }
static char* log_reader_aggregates_into_single_histogram() { const char* file_name = "histogram.log"; struct timespec timestamp; struct timespec interval; hdr_gettime(×tamp); interval.tv_sec = 5; interval.tv_nsec = 2000000; struct hdr_log_writer writer; struct hdr_log_reader reader; hdr_log_writer_init(&writer); hdr_log_reader_init(&reader); int rc = 0; FILE* log_file = fopen(file_name, "w+"); hdr_log_write_header(&writer, log_file, "Test log", ×tamp); hdr_log_write(&writer, log_file, ×tamp, &interval, cor_histogram); hdr_log_write(&writer, log_file, ×tamp, &interval, raw_histogram); fflush(log_file); fclose(log_file); log_file = fopen(file_name, "r"); struct hdr_histogram* histogram; hdr_alloc(3600L * 1000 * 1000, 3, &histogram); rc = hdr_log_read_header(&reader, log_file); mu_assert("Failed header read", validate_return_code(rc)); rc = hdr_log_read(&reader, log_file, &histogram, NULL, NULL); mu_assert("Failed corrected read", validate_return_code(rc)); rc = hdr_log_read(&reader, log_file, &histogram, NULL, NULL); mu_assert("Failed raw read", validate_return_code(rc)); struct hdr_recorded_iter iter; hdr_recorded_iter_init(&iter, histogram); int64_t expected_total_count = raw_histogram->total_count + cor_histogram->total_count; mu_assert( "Total counts incorrect", compare_int64_t(histogram->total_count, expected_total_count)); while (hdr_recorded_iter_next(&iter)) { int64_t count = iter.iter.count_at_index; int64_t value = iter.iter.value_from_index; int64_t expected_count = hdr_count_at_value(raw_histogram, value) + hdr_count_at_value(cor_histogram, value); mu_assert("Incorrect count", compare_int64_t(count, expected_count)); } fclose(log_file); remove(file_name); free(histogram); return 0; }
ERL_NIF_TERM _hi_init(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[]) { hi_ctx_t* ctx = NULL; hh_ctx_t* hdr = NULL; hi_opts_t* opts = NULL; ErlNifResourceType* hh_ctx_type = get_hh_ctx_type(env); ErlNifResourceType* hi_ctx_type = get_hi_ctx_type(env); if (argc != 3 || hh_ctx_type == NULL || hi_ctx_type == NULL || !enif_get_resource(env, argv[0], hi_ctx_type, (void **)&ctx) || !enif_get_resource(env, argv[1], hh_ctx_type, (void **)&hdr) || !enif_is_list(env, argv[2])) { return enif_make_badarg(env); } opts = (hi_opts_t *)enif_alloc(sizeof(hi_opts_t)); parse_opts(env, argv[2], opts, (void *)parse_opt); uint32_t iterator_type = ctx->type; void* it = NULL; if (iterator_type == HDR_ITER_REC) { struct hdr_recorded_iter * iter = enif_alloc(sizeof(struct hdr_recorded_iter)); hdr_recorded_iter_init(iter, hdr->data); it = iter; } if (iterator_type == HDR_ITER_LIN) { if (opts->linear_value_units_per_bucket <= 0) { return make_error(env, "bad_linear_value_unit"); } struct hdr_linear_iter * iter = enif_alloc(sizeof(struct hdr_linear_iter)); hdr_linear_iter_init( iter, hdr->data, opts->linear_value_units_per_bucket); it = iter; } if (iterator_type == HDR_ITER_LOG) { if (opts->log_value_units_first_bucket <= 0) { return make_error(env, "bad_log_value_unit"); } if (opts->log_base <= 0) { return make_error(env, "bad_log_base"); } struct hdr_log_iter * iter = enif_alloc(sizeof(struct hdr_log_iter)); hdr_log_iter_init( iter, hdr->data, opts->log_value_units_first_bucket, opts->log_base); it = iter; } if (iterator_type == HDR_ITER_PCT) { if (opts->percentile_ticks_per_half_distance <= 0) { return make_error(env, "bad_percentile_half_ticks"); } struct hdr_percentile_iter * iter = enif_alloc(sizeof(struct hdr_percentile_iter)); hdr_percentile_iter_init( iter, hdr->data, opts->percentile_ticks_per_half_distance); it = iter; } ctx->type = iterator_type; ctx->opts = opts; ctx->iter = it; return ATOM_OK; }