36 #pragma GCC diagnostic ignored "-Wpedantic"
39 typedef long unsigned Lu;
41 #define TIMES(_n_, _code_) do { \
43 while (_n--) { _code_; } \
46 #define BENCH(_label_, _code_, _n_, _times_) do { \
48 HT_MEASURE(t1, TIMES(_times_, _code_)); \
49 printf("%16s: %.3fs (%.3fMB/s)\n", \
50 _label_, t1, (_n_) / 1e6 / t1 *(_times_)); \
51 fflush(stdout); fflush(stderr); \
54 #define LOG(_lvl_, _fmt_, ...) if (s_verbosity >= _lvl_) do { \
55 fprintf(stderr, "%s: " _fmt_ "\n", __FUNCTION__, ##__VA_ARGS__); \
58 #define DIE(_fmt_, ...) do { \
59 LOG(0, "fatal: " _fmt_, ##__VA_ARGS__); \
64 #define O_BENCH_HASH (1 << 0)
65 #define O_BENCH_LUT (1 << 1)
66 #define O_CHECK_HASH (1 << 2)
67 #define O_MEMCPY (1 << 3)
68 #define O_HASH_MOD (1 << 4)
69 #define O_HASH_MOD16X2 (1 << 5)
70 #define O_HASH_MASK16X2 (1 << 6)
71 #define O_HASH_MASK (1 << 7)
72 #define O_HASH_MASK32X2 (1 << 8)
73 #define O_DEFAULT (0xffffffff & ~O_CHECK_HASH)
74 #define O_HASHES 0xfffffff0
90 static size_t s_m = 0xffffffff;
91 static size_t s_m1 = 0xffff;
92 static size_t s_m2 = (0xffff - 4);
95 dump_bm(
const char *label,
const char *in,
size_t len) {
99 printf(
"----%s encoded:\n", label);
100 fwrite(in, 1, len, stdout);
103 printf(
"\n----%s dumped:\n", label);
106 LOG(1,
"error: bad encoded data (ret=%d)", ret);
108 puts(
"\n----end-dump");
124 dump_bm(
"mod16x2", out, *len_p);
132 dump_bm(
"mask16x2", out, *len_p);
147 dump_bm(
"mask32x2", out, *len_p);
153 LOG(1,
"\nbm_unpack returned %d, size: %lu\n", ret, (
Lu)*len_p);
156 fwrite(out, 1, *len_p, stdout);
157 puts(
"\nend-decoded");
164 size_t len = 0, size = 0, ret;
166 while ((ret = fread(buf, 1,
sizeof(buf), fp)) > 0) {
170 size = (len + 16) * 3 / 2;
171 data = realloc(data, size);
173 memcpy(data + len - ret, buf, ret);
181 printf(
"%16s: %lx\n", label, (
Lu)h);
208 size_t out_len, out_len0, len2 = len, work_len;
216 buf = malloc(len + out_len);
218 mem = malloc(work_len);
219 LOG(1,
"input length: %lu, out_len %lu, work_len: %lu\n",
220 (
Lu)len, (
Lu)out_len, (
Lu)work_len);
224 BENCH(
"memcpy", memcpy(buf, data, len), len, n);
291 if (opt !=
O_DEFAULT && (opt & 0xf))
return;
298 HT_CHECK(memcmp(data, buf + out_len, len) == 0);
302 memset(buf, 0, out_len); out_len = out_len0;
308 HT_CHECK(memcmp(data, buf + out_len, len) == 0);
312 memset(buf, 0, out_len); out_len = out_len0;
318 HT_CHECK(memcmp(data, buf + out_len, len) == 0);
322 memset(buf, 0, out_len); out_len = out_len0;
327 HT_CHECK(memcmp(data, buf + out_len, len) == 0);
331 memset(buf, 0, out_len); out_len = out_len0;
337 HT_CHECK(memcmp(data, buf + out_len, len) == 0);
350 int fd = open(fname, O_RDONLY, 0);
355 if (fd == -1)
DIE(
"cannot open '%s'", fname);
357 if (fstat(fd, &st) != 0)
DIE(
"stat failed on '%s'", fname);
363 LOG(1,
"mmaping %ld bytes in to memory...", len);
364 data = mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
366 if ((
char *)-1 == data) {
367 LOG(0,
"mmap failed on '%s', trying alternative...", fname);
375 LOG(1,
"reading %ld bytes in to memory...", len);
379 if (!data)
DIE(
"error alloc'ing %ld bytes", len);
381 if ((len = read(fd, data, len)) != st.st_size)
382 DIE(
"error reading %s (expecting %ld bytes, got %ld",
383 fname, (
long)st.st_size, len);
393 printf(
"usage: bmz-test [options] [string...]\n%s%s%s",
394 "--help show this help\n"
395 "--verbose[=level] set verbose level\n"
396 "--file <file> use <file as input\n"
397 "--offset <number> set bm offset\n"
398 "--fp-len <number> set bm fingerprint length\n"
399 "--hash compute hash value only\n"
400 "--b1 <number> hash param b1\n"
401 "--b2 <number> hash param b2\n"
402 "--m <number> hash param m\n",
403 "--m1 <number> hash param m1\n"
404 "--m2 <number> hash param m2\n"
405 "--hash-mod use hash-mod\n"
406 "--hash-mod16x2 use hash-mod16x2\n"
407 "--hash-mask16x2 use hash-mask16x2\n"
408 "--hash-mask use hash-mask\n",
409 "--hash-mask32x2 use hash-mask32x2\n"
410 "--bench-hash bechmarks for builtin hashes\n"
411 "--check-hash verify rolling hashes\n"
412 "--bench-lut benchmarks for lookup table\n"
413 "--times <number> number of repeats for the test\n"
420 char **ia = av + 1, **a_end = av + ac, *ep;
421 const char *fname = NULL;
423 for (; ia < a_end; ++ia) {
424 if (!strcmp(
"--fp-len", *ia))
s_fp_len = atoi(*++ia);
425 else if (!strcmp(
"--offset", *ia))
s_offset = atoi(*++ia);
426 else if (!strcmp(
"--times", *ia))
s_times = atoi(*++ia);
428 else if (!strcmp(
"--b1", *ia))
s_b1 = atoi(*++ia);
429 else if (!strcmp(
"--b2", *ia))
s_b2 = atoi(*++ia);
430 else if (!strcmp(
"--m", *ia))
s_m = strtol(*++ia, &ep, 0);
431 else if (!strcmp(
"--m1", *ia))
s_m1 = strtol(*++ia, &ep, 0);
432 else if (!strcmp(
"--m2", *ia))
s_m2 = strtol(*++ia, &ep, 0);
433 else if (!strcmp(
"--file", *ia)) fname = *++ia;
434 else if (!strcmp(
"--no-mmap", *ia))
s_no_mmap = 1;
435 else if (!strcmp(
"--verbose", *ia))
s_verbosity = 1;
436 else if (!strncmp(
"--verbose=", *ia, 10))
s_verbosity = atoi(*ia + 10);
437 else if (!strcmp(
"--bm-dump", *ia))
s_bm_dump = 1;
446 else if (!strcmp(
"--help", *ia))
show_usage();
447 else if (!strcmp(
"--", *ia)) {
451 else if (
'-' == **ia) {
452 DIE(
"unknown option: %s\n", *ia);
463 else if (ia >= a_end)
465 else for (; ia < a_end; ++ia)
bmz_bm_unpack(const void *in, size_t in_len, void *out, size_t *out_len_p)
static void test_bm_mask32x2(const char *in, size_t len, char *out, size_t *len_p, void *work_mem)
bmz_bm_pack_mod16x2(const void *in, size_t in_len, void *out, size_t *out_len_p, size_t offset, size_t fp_len, void *work_mem, size_t b1, size_t b2, size_t m1, size_t m2)
static size_t s_no_mmap
Copyright (C) 2007-2015 Hypertable, Inc.
bmz_hash_mask32x2(const void *in, size_t in_len, size_t b1, size_t b2)
static void print_hash(const char *label, size_t h)
bmz_hash_mask16x2(const void *in, size_t in_len, size_t b1, size_t b2)
static void test_bm_mask16x2(const char *in, size_t len, char *out, size_t *len_p, void *work_mem)
bmz_bm_pack_worklen(size_t in_len, size_t fp_len)
int main(int ac, char *av[])
bmz_bench_lut_mod(const void *in, size_t in_len, size_t fp_len, void *mem, size_t b, size_t m)
int bmz_set_verbosity(int verbosity)
Set the verbosity of library for testing and debugging.
bmz_hash_mod16x2(const void *in, size_t in_len, size_t b1, size_t b2, size_t m1, size_t m2)
bmz_check_hash_mask32x2(const void *in, size_t in_len, size_t fp_len, size_t b1, size_t b2)
#define BENCH(_label_, _code_, _n_, _times_)
bmz_bench_lut_mask16x2(const void *in, size_t in_len, size_t fp_len, void *mem, size_t b1, size_t b2)
bmz_bench_lut_mask32x2(const void *in, size_t in_len, size_t fp_len, void *mem, size_t b1, size_t b2)
static void test_bm_unpack(const char *in, size_t len, char *out, size_t *len_p)
static void show_hash(const char *data, size_t len)
#define BMZ_HASH_MASK16X2
static void test_bm_mod(const char *in, size_t len, char *out, size_t *len_p, void *work_mem)
bmz_bm_pack_mod(const void *in, size_t in_len, void *out, size_t *out_len_p, size_t offset, size_t fp_len, void *work_mem, size_t b, size_t m)
bmz_bm_pack_mask16x2(const void *in, size_t in_len, void *out, size_t *out_len_p, size_t offset, size_t fp_len, void *work_mem, size_t b1, size_t b2)
bmz_check_hash_mod16x2(const void *in, size_t in_len, size_t fp_len, size_t b1, size_t b2, size_t m1, size_t m2)
bmz_bench_hash(const void *in, size_t in_len, unsigned type)
static void test_bm_mask(const char *in, size_t len, char *out, size_t *len_p, void *work_mem)
bmz_bench_lut_mask(const void *in, size_t in_len, size_t fp_len, void *mem, size_t b)
size_t bmz_pack_buflen(size_t in_len)
Compute bmz compression output buffer length.
static void test_bm_mod16x2(const char *in, size_t len, char *out, size_t *len_p, void *work_mem)
bmz_hash_mask(const void *in, size_t in_len, size_t b)
#define LOG(_lvl_, _fmt_,...)
static void test_from_file(const char *fname)
static void test_from_stdin()
static void dump_bm(const char *label, const char *in, size_t len)
bmz_check_hash_mod(const void *in, size_t in_len, size_t fp_len, size_t b, size_t m)
bmz_bm_dump(const void *in, size_t in_len)
static char * read_from_fp(FILE *fp, size_t *len_p)
bmz_bm_pack_mask32x2(const void *in, size_t in_len, void *out, size_t *out_len_p, size_t offset, size_t fp_len, void *work_mem, size_t b1, size_t b2)
#define BMZ_HASH_MASK32X2
bmz_bench_lut_mod16x2(const void *in, size_t in_len, size_t fp_len, void *mem, size_t b1, size_t b2, size_t m1, size_t m2)
bmz_check_hash_mask16x2(const void *in, size_t in_len, size_t fp_len, size_t b1, size_t b2)
bmz_hash_mod(const void *in, size_t in_len, size_t b, size_t m)
static void test_from_string(const char *data, size_t len)
bmz_bm_pack_mask(const void *in, size_t in_len, void *out, size_t *out_len_p, size_t offset, size_t fp_len, void *work_mem, size_t b)
bmz_check_hash_mask(const void *in, size_t in_len, size_t fp_len, size_t b)