static bool compare_histogram(struct hdr_histogram* a, struct hdr_histogram* b) { size_t a_size = hdr_get_memory_size(a); size_t b_size = hdr_get_memory_size(b); if (a_size == b_size && memcmp(a, b, a_size) == 0) { return true; } printf("Sizes a: %zu, b: %zu\n", a_size, b_size); struct hdr_iter iter_a; struct hdr_iter iter_b; hdr_iter_init(&iter_a, a); hdr_iter_init(&iter_b, b); while (hdr_iter_next(&iter_a) && hdr_iter_next(&iter_b)) { if (iter_a.count_at_index != iter_b.count_at_index || iter_a.value_from_index != iter_b.value_from_index) { printf( "A - value: %"PRIu64", count: %"PRIu64", B - value: %"PRIu64", count: %"PRIu64"\n", iter_a.value_from_index, iter_a.count_at_index, iter_b.value_from_index, iter_b.count_at_index); } } return false; }
static char* test_encode_and_decode_compressed() { load_histograms(); size_t raw_histogram_size = hdr_get_memory_size(raw_histogram); uint8_t* buffer = (uint8_t*) malloc(hdr_get_memory_size(raw_histogram)); size_t encode_result = hdr_encode_compressed(raw_histogram, buffer, raw_histogram_size); mu_assert("Did not encode", encode_result == 0); int32_t compressed_length = hdr_get_compressed_length(buffer); struct hdr_histogram* loaded_histogram = NULL; int decode_result = hdr_decode_compressed(buffer, compressed_length, &loaded_histogram); if (decode_result != 0) { printf("%s\n", hdr_strerror(decode_result)); } mu_assert("Did not decode", decode_result == 0); mu_assert("Loaded histogram is null", loaded_histogram != NULL); int compare_result = memcmp(raw_histogram, loaded_histogram, raw_histogram_size); mu_assert("Comparison did not match", compare_result == 0); return 0; }
static char* test_encode_and_decode() { load_histograms(); size_t raw_histogram_size = hdr_get_memory_size(cor_histogram); uint8_t* buffer = (uint8_t*) malloc(hdr_get_memory_size(cor_histogram)); size_t encode_result = hdr_encode(cor_histogram, buffer, raw_histogram_size); mu_assert("Did not encode", encode_result != 0); mu_assert("Incorrect size", encode_result <= raw_histogram_size); struct hdr_histogram* loaded_histogram = NULL; hdr_decode(buffer, raw_histogram_size, &loaded_histogram); int compare_result = memcmp(cor_histogram, loaded_histogram, raw_histogram_size); if (compare_result != 0) { uint8_t* a = (uint8_t*) cor_histogram; uint8_t* b = (uint8_t*) loaded_histogram; for (int i = 0; i < raw_histogram_size; i++) { if (a[i] != b[i]) { printf("Mismatch at %d: %x - %x\n", i, a[i] & 0xFF, b[i] & 0xFF); } } } mu_assert("Comparison did not match", compare_result == 0); return 0; }
static char* test_encode_and_decode_compressed() { load_histograms(); uint8_t* buffer = NULL; int len = 0; int rc = 0; struct hdr_histogram* actual = NULL; struct hdr_histogram* expected = raw_histogram; size_t histogram_size = hdr_get_memory_size(expected); rc = hdr_encode_compressed(expected, &buffer, &len); mu_assert("Did not encode", validate_return_code(rc)); rc = hdr_decode_compressed(buffer, len, &actual); mu_assert("Did not decode", validate_return_code(rc)); mu_assert("Loaded histogram is null", actual != NULL); mu_assert( "Comparison did not match", compare_binary(expected, actual, histogram_size)); free(actual); return 0; }
static char* test_create() { struct hdr_histogram* h = NULL; int r = hdr_alloc(36000000, 4, &h); size_t s = hdr_get_memory_size(h); mu_assert("Failed to allocate hdr_histogram", r == 0); mu_assert("Failed to allocate hdr_histogram", h != NULL); mu_assert("Size is incorrect", s == 1704008); free(h); return 0; }
static char* test_create() { struct hdr_histogram* h = NULL; int r = hdr_init(1, 3600000000, 3, &h); size_t s = hdr_get_memory_size(h); mu_assert("Failed to allocate hdr_histogram", r == 0); mu_assert("Failed to allocate hdr_histogram", h != NULL); mu_assert("Incorrect array length", compare_int64(h->counts_len, 23552)); mu_assert("Size is incorrect", compare_int64(s, 188520)); free(h); return 0; }
ERL_NIF_TERM _hh_get_memory_size(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[]) { hh_ctx_t* ctx = NULL; ErlNifResourceType* ctx_type = get_hh_ctx_type(env); if (ctx_type != NULL && !enif_get_resource(env, argv[0], ctx_type, (void **)&ctx)) { return enif_make_badarg(env); } if (ctx != NULL) { return enif_make_ulong(env,hdr_get_memory_size(ctx->data)); } return make_error(env, "bad_hdr_histogram_nif_impl"); }
static bool compare_histogram(struct hdr_histogram* a, struct hdr_histogram* b) { if (a->counts_len != b->counts_len) { printf( "a.counts_len = %"PRIu32", b.counts_len = %"PRIu32"\n", a->counts_len, b->counts_len); return false; } int64_t a_max = hdr_max(a); int64_t b_max = hdr_max(b); if (a_max != b_max) { printf("a.max = %"PRIu64", b.max = %"PRIu64"\n", a_max, b_max); // return false; } int64_t a_min = hdr_min(a); int64_t b_min = hdr_min(b); if (a_min != b_min) { printf("a.min = %"PRIu64", b.min = %"PRIu64"\n", a_min, b_min); // return false; } size_t a_size = hdr_get_memory_size(a); size_t b_size = hdr_get_memory_size(b); if (a_size != b_size) { printf("a.size: %zu, b.size: %zu\n", a_size, b_size); return false; } size_t counts_size = a->counts_len * sizeof(int64_t); if (memcmp(a->counts, b->counts, counts_size) == 0) { return true; } printf("%s\n", "Counts incorrect"); struct hdr_iter iter_a; struct hdr_iter iter_b; hdr_iter_init(&iter_a, a); hdr_iter_init(&iter_b, b); while (hdr_iter_next(&iter_a) && hdr_iter_next(&iter_b)) { if (iter_a.count != iter_b.count || iter_a.value != iter_b.value) { printf( "A - value: %"PRIu64", count: %"PRIu64", B - value: %"PRIu64", count: %"PRIu64"\n", iter_a.value, iter_a.count, iter_b.value, iter_b.count); } } return false; }