int main(void) { char input[999]; char const *envhx = getenv("hx"); if (!envhx) die(": $hx not set"); setvbuf(stdout, 0, _IOLBF, 0); plan_tests(1 + 4*4); HXRET rc = hxbuild(0, 0, 1<<20, 0.0); ok(rc == HXERR_BAD_REQUEST, "hxbuild rejects NULL arg: %s", hxerror(rc)); try("/dev/null", 0); try(strcat(strcpy(input, envhx), "/data.tab"), 88172); try(strcat(strcat(strcpy(input, "cat "), envhx), "/data.tab"), 88172); try(strcat(strcat(strcpy(input, "head -2335 "), envhx), "/data.tab"), 2335); return exit_status(); } static void try(char const *inpfile, int nrecs) { FILE *fp = strchr(inpfile, ' ') ? popen(inpfile, "r") : fopen(inpfile, "r"); ok(fp, "input %s", inpfile); if (!fp) die(": unable to open %s:", inpfile); setvbuf(fp, NULL, _IOFBF, 16384); hxcreate("build_t.hx", 0755, 4096, "", 0); HXFILE *hp = hxopen("build_t.hx", HX_UPDATE); double alpha = tick(); HXRET rc = hxbuild(hp, fp, 1<<20, 0.0); //128MB double omega = tick(); if (strchr(inpfile, ' ')) pclose(fp); else fclose(fp); ok(rc == 0, "hxbuild from (%s) in %.3g secs, returns %s", inpfile, omega - alpha, hxerror(rc)); rc = hxfix(hp,0,0,0,0); ok(rc == (HXRET)HX_UPDATE, "hxcheck returns: %s", hxmode(rc)); HXSTAT st; hxstat(hp, &st); ok(st.nrecs == nrecs, "hxbuild loaded %.0f/%d records", st.nrecs, nrecs); hxclose(hp); unlink("build_t.hx"); }
int main(int argc, char **argv) { char const *hx = getenv("hx"); if (!hx) hx = "."; int mapmode = HX_MMAP; char inpdflt[strlen(hx) + sizeof "/data.tab"]; char const *inpfile = inpdflt; int memsize = 0; strcat(strcpy(inpdflt, hx), "/data.tab"); if (argc == 1) usage("[-d] inpfile [membits | 0]\n" " -d: use disk io, not mmap\n" " membits: size of RAM for hxbuild (default:20)\n" " membits=0 means use the existing perf_x.hx"); if (!strcmp(argv[1], "-d")) mapmode = 0, ++argv, --argc; switch (argc) { case 3: memsize = atoi(argv[2]); case 2: inpfile = argv[1]; case 1: break; } // Ensure that dirname(argv[0]) is in LD_LIBRARY_PATH: char *dir = strrchr(argv[0], '/'), empty[] = ""; char const *llp = getenv("LD_LIBRARY_PATH"); dir = dir ? (*dir = 0, argv[0]) : empty; if (!llp) llp = ""; char llpath[strlen(dir) + strlen(llp) + 2]; setenv("LD_LIBRARY_PATH", strcat(strcat(strcpy(llpath, dir), ":"), llp), /*OVERRIDE*/1); HXFILE *hp; HXSTAT info; HXRET rc; FILE *fp = strcmp(inpfile, "-") ? fopen(inpfile, "r") : stdin; if (!fp) die(": cannot read %s:", inpfile); setvbuf(fp, NULL, _IOFBF, 65536); if (memsize) { memsize = 1 << memsize; hxcreate("perf_x.hx", 0644, 4096, 0, 0); hp = hxopen("perf_x.hx", HX_UPDATE); rc = hxbuild(hp, fp, memsize, 0.0); if (rc < 0) die("hxbuild(%d): %s", memsize, hxerror(rc)); hxclose(hp); } if (hxdebug) system("echo;echo built; chx info perf_x.hx; chx stat perf_x.hx; chx -vdd check perf_x.hx"); hp = hxopen("perf_x.hx", HX_UPDATE | mapmode); if (!hp) die("cannot open perf_x.hx%s:", mapmode ? " with MMAP" : ""); char buf[4096], rec[4096]; int len; double t0 = tick(); rewind(fp); while (fgets(buf, sizeof(buf), fp)) { buf[strlen(buf) - 1] = 0; hx_load(hp, rec, sizeof rec, buf); hxget(hp, rec, sizeof(rec)); } t0 = tick() - t0; double t1 = tick(); rewind(fp); while (fgets(buf, sizeof(buf), fp)) { buf[strlen(buf) - 1] = 0; buf[0] ^= 0x55; hx_load(hp, rec, sizeof rec, buf); hxget(hp, rec, sizeof(rec)); } t1 = tick() - t1; double t2 = tick(); rewind(fp); while (fgets(buf, sizeof(buf), fp)) { buf[strlen(buf) - 1] = 0; strcat(buf, "hello, world"); len = hx_load(hp, rec, sizeof rec, buf); hxput(hp, rec, len); } t2 = tick() - t2; if (hxdebug) system("echo;echo put+12; chx info perf_x.hx; chx stat perf_x.hx; chx -vdd check perf_x.hx"); double t3 = tick(); rewind(fp); while (fgets(buf, sizeof(buf), fp)) { buf[strlen(buf) - 1] = 0; len = hx_load(hp, rec, sizeof rec, buf); hxput(hp, rec, len); } t3 = tick() - t3; if (hxdebug) system("echo;echo put-0; chx info perf_x.hx; chx stat perf_x.hx; chx -vdd check perf_x.hx"); double t4 = tick(); rewind(fp); while (fgets(buf, sizeof(buf), fp)) { strcpy(buf + strlen(buf) - 1, "---------1---------2---------3---------4---------5---------6---------7---------8---------9---------0\n"); len = hx_load(hp, rec, sizeof rec, buf); hxput(hp, rec, len); } t4 = tick() - t4; if (hxdebug) system("echo;echo put+100; chx info perf_x.hx; chx stat perf_x.hx; chx -vdd check perf_x.hx"); double t5 = tick(); rewind(fp); while (fgets(buf, sizeof(buf), fp)) { buf[strlen(buf) - 1] = 0; hx_load(hp, rec, sizeof rec, buf); hxget(hp, rec, sizeof(rec)); } t5 = tick() - t5; double t6 = tick(); rewind(fp); while (fgets(buf, sizeof(buf), fp)) { buf[strlen(buf) - 1] = 0; buf[0] ^= 0x55; hx_load(hp, rec, sizeof rec, buf); hxget(hp, rec, sizeof(rec)); } t6 = tick() - t6; double t7 = tick(); rewind(fp); while (fgets(buf, sizeof(buf), fp)) { buf[strlen(buf) - 1] = 0; buf[0] ^= 0x55; hx_load(hp, rec, sizeof rec, buf); hxput(hp, rec, sizeof(rec)); } t7 = tick() - t7; hxstat(hp, &info); fprintf(stderr, "nrecs: %g build: %.2fM rec/sec\n(usec:)\n" "\tget-y\t%.2f\n\tget-n\t%.2f\n" "\tput+12\t%.2f\n\tput+0\t%.2f\n\tput+100\t%.2f\n" "\tget-y\t%.2f\n\tget-n\t%.2f\n\tput-xx\t%.2f\n", info.nrecs, info.nrecs/1E6/t0, t0*1E6/info.nrecs, t1*1E6/info.nrecs, t2*1E6/info.nrecs, t3*1E6/info.nrecs, t4*1E6/info.nrecs, t5*1E6/info.nrecs, t6*1E6/info.nrecs, t7*1E6/info.nrecs); return 0; }
//--------------|--------------------------------------------- int main(int argc, char **argv) { int ret = 0; int opt; int timed = 0; char cmd[10240]; while (0 < (opt = getopt(argc, argv, "?c:dmp:s:tv-"))) { switch (opt) { case '?': help(); break; case 'c': hxcrash = atoi(optarg); break; case 'd': ++hxdebug; break; case 'm': mmode |= HX_MMAP; break; case 's': mmode |= HX_FSYNC; break; case 't': timed++; break; case 'v': ++verbose; break; } } argc -= optind; argv += optind; setvbuf(stdout, NULL, _IOLBF, 0); setvbuf(stderr, NULL, _IOLBF, 0); errno = 0; //setvbuf sets errno !? int size = 0; HXMODE mode; HXFILE *hp = NULL; FILE *fp = NULL; if (!*argv) die("See 'chx help' for usage"); if (*argv[0] == '?' || !strcmp(argv[0], "help")) help(); double tstart = tick(); if (hxdebug) hxtime = tstart; if (!strcmp(argv[0], "build")) { hp = do_hxopen("build", argv[1], HX_UPDATE); fp = do_fopen("build", argv[2], "r"); int memsize = argc > 3 ? atoi(argv[3]) : 1; int inpsize = argc > 4 ? atoi(argv[4]) : 0; memsize <<= 20; is_hxret("build", hxbuild(hp, fp, memsize, inpsize)); } else if (!strcmp(argv[0], "check")) { char *udata = argc > 3 ? argv[3] : NULL; if (argc < 2) die("%s: requires filename [pgsize [udata]]", argv[0]); if (argc > 2 && !sscanf(argv[2], "%d", &size)) die("%s: invalid pgsize", argv[2]); hp = do_hxopen("check", argv[1], HX_CHECK); mode = hxfix(hp, NULL, size, udata, udata ? strlen(udata) : 0); if (verbose || mode != HX_UPDATE) printf("%s %s\n", hxmode(mode), errno ? strerror(errno) : ""); ret = mode != HX_UPDATE; } else if (!strcmp(argv[0], "create")) { if (argc < 3 || !sscanf(argv[2], "%d", &size)) die("create: requires filename, pgsize, type"); char const *type = argv[3] ? argv[3] : ""; is_hxret("create", hxcreate(argv[1], 0644, size, type, strlen(type))); hp = do_hxopen("create", argv[1], HX_RECOVER); } else if (!strcmp(argv[0], "del")) { hp = do_hxopen("del", argv[1], HX_UPDATE); fp = do_fopen("del", argv[2], "r"); del(hp, fp); } else if (!strcmp(argv[0], "dump")) { hp = do_hxopen("dump", argv[1], HX_READ); dump(hp, stdout); } else if (!strcmp(argv[0], "fix") || !strcmp(argv[0], "repair")) { char *udata = argc > 3 ? argv[3] : NULL; if (argc < 2) die("%s: requires filename [pgsize [udata]]", argv[0]); if (argv[2] && !sscanf(argv[2], "%d", &size)) die("%s: invalid pgsize", argv[2]); hp = do_hxopen("repair", argv[1], HX_UPDATE); fp = tmpfile(); mode = hxfix(hp, fp, size, udata, udata ? strlen(udata) : 0); if (verbose || mode != HX_UPDATE) printf("%s %s\n", hxmode(mode), errno ? strerror(errno) : ""); ret = mode != HX_UPDATE; } else if (!strcmp(argv[0], "hdrs")) { hp = do_hxopen("hdrs", argv[1], HX_READ); fp = do_fopen("hdrs", argv[2], "w"); hdrs(hp); } else if (!strcmp(argv[0], "info")) { hp = do_hxopen("info", argv[1], HX_READ); info(hp); } else if (!strcmp(argv[0], "load")) { hp = do_hxopen("load", argv[1], HX_UPDATE); fp = do_fopen("hdrs", argv[2], "r"); do_load(hp, fp); } else if (!strcmp(argv[0], "lock")) { hp = do_hxopen("lock", argv[1], HX_READ); do_lock(hp); } else if (!strcmp(argv[0], "maps")) { hp = do_hxopen("maps", argv[1], HX_READ); is_hxret("maps", maps(hp)); } else if (!strcmp(argv[0], "pack")) { hp = do_hxopen("load", argv[1], HX_UPDATE); is_hxret("pack", hxpack(hp)); } else if (!strcmp(argv[0], "save")) { hp = do_hxopen("save", argv[1], HX_READ); fp = do_fopen("save", argv[2], "w"); do_save(hp, fp); } else if (!strcmp(argv[0], "shape")) { double density; if (argc != 3 || !sscanf(argv[2], "%lf", &density)) die("%s: requires density arg (0 to 1.0)", argv[2]); hp = do_hxopen("shape", argv[1], HX_UPDATE); is_hxret("shape", hxshape(hp, density)); } else if (!strcmp(argv[0], "stat")) { hp = do_hxopen("stat", argv[1], HX_READ); stats(hp); } else if (!strcmp(argv[0], "types")) { // for each dir in LD_LIBRARY_PATH (and "" -> ".") then /lib then /usr/lib, // find all files of the form "<dir>/hx_<rectype>.so" // and build a file of unique rectypes. // Then call hxlib which returns an exact path and a bitmask of DIFF/LOAD/TEST // "type diff-load-test path" // If the path matches, print the entry. types(getenv("LD_LIBRARY_PATH")); types("lib:/usr/lib"); } else { die("%s: unknown command. See 'chx help'", cmd); } if (fp) fclose(fp); if (hp) hxclose(hp); if (timed) fprintf(stderr, "# chx %s: %.3f secs\n", *argv, tick() - tstart); return ret; }