⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 tcfmttest.c

📁 Tokyo Cabinet的Tokyo Cabinet 是一个DBM的实现。这里的数据库由一系列key-value对的记录构成。key和value都可以是任意长度的字节序列,既可以是二进制也可以是字符
💻 C
📖 第 1 页 / 共 3 页
字号:
    } else if(!rstr){      rstr = argv[i];    } else if(!wstr){      wstr = argv[i];    } else if(!lstr){      lstr = argv[i];    } else {      usage();    }  }  if(!path || !tstr || !rstr) usage();  int tnum = tcatoix(tstr);  int rnum = tcatoix(rstr);  if(tnum < 1 || rnum < 1) usage();  int width = wstr ? tcatoix(wstr) : -1;  int64_t limsiz = lstr ? tcatoix(lstr) : -1;  int rv = proctypical(path, tnum, rnum, width, limsiz, omode, nc, rratio);  return rv;}/* perform write command */static int procwrite(const char *path, int tnum, int rnum, int width, int64_t limsiz,                     int omode, bool rnd){  iprintf("<Writing Test>\n  seed=%u  path=%s  tnum=%d  rnum=%d  width=%d  limsiz=%lld"          "  omode=%d  rnd=%d\n\n",          g_randseed, path, tnum, rnum, width, (long long)limsiz, omode, rnd);  bool err = false;  double stime = tctime();  TCFDB *fdb = tcfdbnew();  if(g_dbgfd >= 0) tcfdbsetdbgfd(fdb, g_dbgfd);  if(!tcfdbsetmutex(fdb)){    eprint(fdb, "tcfdbsetmutex");    err = true;  }  if(!tcfdbtune(fdb, width, limsiz)){    eprint(fdb, "tcfdbtune");    err = true;  }  if(!tcfdbopen(fdb, path, FDBOWRITER | FDBOCREAT | FDBOTRUNC | omode)){    eprint(fdb, "tcfdbopen");    err = true;  }  TARGWRITE targs[tnum];  pthread_t threads[tnum];  if(tnum == 1){    targs[0].fdb = fdb;    targs[0].rnum = rnum;    targs[0].rnd = rnd;    targs[0].id = 0;    if(threadwrite(targs) != NULL) err = true;  } else {    for(int i = 0; i < tnum; i++){      targs[i].fdb = fdb;      targs[i].rnum = rnum;      targs[i].rnd = rnd;      targs[i].id = i;      if(pthread_create(threads + i, NULL, threadwrite, targs + i) != 0){        eprint(fdb, "pthread_create");        targs[i].id = -1;        err = true;      }    }    for(int i = 0; i < tnum; i++){      if(targs[i].id == -1) continue;      void *rv;      if(pthread_join(threads[i], &rv) != 0){        eprint(fdb, "pthread_join");        err = true;      } else if(rv){        err = true;      }    }  }  iprintf("record number: %llu\n", (unsigned long long)tcfdbrnum(fdb));  iprintf("size: %llu\n", (unsigned long long)tcfdbfsiz(fdb));  mprint(fdb);  sysprint();  if(!tcfdbclose(fdb)){    eprint(fdb, "tcfdbclose");    err = true;  }  tcfdbdel(fdb);  iprintf("time: %.3f\n", tctime() - stime);  iprintf("%s\n\n", err ? "error" : "ok");  return err ? 1 : 0;}/* perform read command */static int procread(const char *path, int tnum, int omode, bool wb, bool rnd){  iprintf("<Reading Test>\n  seed=%u  path=%s  tnum=%d  omode=%d  wb=%d  rnd=%d\n\n",          g_randseed, path, tnum, omode, wb, rnd);  bool err = false;  double stime = tctime();  TCFDB *fdb = tcfdbnew();  if(g_dbgfd >= 0) tcfdbsetdbgfd(fdb, g_dbgfd);  if(!tcfdbsetmutex(fdb)){    eprint(fdb, "tcfdbsetmutex");    err = true;  }  if(!tcfdbopen(fdb, path, FDBOREADER | omode)){    eprint(fdb, "tcfdbopen");    err = true;  }  int rnum = tcfdbrnum(fdb) / tnum;  TARGREAD targs[tnum];  pthread_t threads[tnum];  if(tnum == 1){    targs[0].fdb = fdb;    targs[0].rnum = rnum;    targs[0].wb = wb;    targs[0].rnd = rnd;    targs[0].id = 0;    if(threadread(targs) != NULL) err = true;  } else {    for(int i = 0; i < tnum; i++){      targs[i].fdb = fdb;      targs[i].rnum = rnum;      targs[i].wb = wb;      targs[i].rnd = rnd;      targs[i].id = i;      if(pthread_create(threads + i, NULL, threadread, targs + i) != 0){        eprint(fdb, "pthread_create");        targs[i].id = -1;        err = true;      }    }    for(int i = 0; i < tnum; i++){      if(targs[i].id == -1) continue;      void *rv;      if(pthread_join(threads[i], &rv) != 0){        eprint(fdb, "pthread_join");        err = true;      } else if(rv){        err = true;      }    }  }  iprintf("record number: %llu\n", (unsigned long long)tcfdbrnum(fdb));  iprintf("size: %llu\n", (unsigned long long)tcfdbfsiz(fdb));  mprint(fdb);  sysprint();  if(!tcfdbclose(fdb)){    eprint(fdb, "tcfdbclose");    err = true;  }  tcfdbdel(fdb);  iprintf("time: %.3f\n", tctime() - stime);  iprintf("%s\n\n", err ? "error" : "ok");  return err ? 1 : 0;}/* perform remove command */static int procremove(const char *path, int tnum, int omode, bool rnd){  iprintf("<Removing Test>\n  seed=%u  path=%s  tnum=%d  omode=%d  rnd=%d\n\n",          g_randseed, path, tnum, omode, rnd);  bool err = false;  double stime = tctime();  TCFDB *fdb = tcfdbnew();  if(g_dbgfd >= 0) tcfdbsetdbgfd(fdb, g_dbgfd);  if(!tcfdbsetmutex(fdb)){    eprint(fdb, "tcfdbsetmutex");    err = true;  }  if(!tcfdbopen(fdb, path, FDBOWRITER | omode)){    eprint(fdb, "tcfdbopen");    err = true;  }  int rnum = tcfdbrnum(fdb) / tnum;  TARGREMOVE targs[tnum];  pthread_t threads[tnum];  if(tnum == 1){    targs[0].fdb = fdb;    targs[0].rnum = rnum;    targs[0].rnd = rnd;    targs[0].id = 0;    if(threadremove(targs) != NULL) err = true;  } else {    for(int i = 0; i < tnum; i++){      targs[i].fdb = fdb;      targs[i].rnum = rnum;      targs[i].rnd = rnd;      targs[i].id = i;      if(pthread_create(threads + i, NULL, threadremove, targs + i) != 0){        eprint(fdb, "pthread_create");        targs[i].id = -1;        err = true;      }    }    for(int i = 0; i < tnum; i++){      if(targs[i].id == -1) continue;      void *rv;      if(pthread_join(threads[i], &rv) != 0){        eprint(fdb, "pthread_join");        err = true;      } else if(rv){        err = true;      }    }  }  iprintf("record number: %llu\n", (unsigned long long)tcfdbrnum(fdb));  iprintf("size: %llu\n", (unsigned long long)tcfdbfsiz(fdb));  mprint(fdb);  sysprint();  if(!tcfdbclose(fdb)){    eprint(fdb, "tcfdbclose");    err = true;  }  tcfdbdel(fdb);  iprintf("time: %.3f\n", tctime() - stime);  iprintf("%s\n\n", err ? "error" : "ok");  return err ? 1 : 0;}/* perform wicked command */static int procwicked(const char *path, int tnum, int rnum, int omode, bool nc){  iprintf("<Writing Test>\n  seed=%u  path=%s  tnum=%d  rnum=%d  omode=%d  nc=%d\n\n",          g_randseed, path, tnum, rnum, omode, nc);  bool err = false;  double stime = tctime();  TCFDB *fdb = tcfdbnew();  if(g_dbgfd >= 0) tcfdbsetdbgfd(fdb, g_dbgfd);  if(!tcfdbsetmutex(fdb)){    eprint(fdb, "tcfdbsetmutex");    err = true;  }  if(!tcfdbtune(fdb, RECBUFSIZ * 2, EXHEADSIZ + (RECBUFSIZ * 2 + sizeof(int)) * rnum * tnum)){    eprint(fdb, "tcfdbtune");    err = true;  }  if(!tcfdbopen(fdb, path, FDBOWRITER | FDBOCREAT | FDBOTRUNC | omode)){    eprint(fdb, "tcfdbopen");    err = true;  }  if(!tcfdbiterinit(fdb)){    eprint(fdb, "tcfdbiterinit");    err = true;  }  TARGWICKED targs[tnum];  pthread_t threads[tnum];  TCMAP *map = tcmapnew();  if(tnum == 1){    targs[0].fdb = fdb;    targs[0].rnum = rnum;    targs[0].nc = nc;    targs[0].id = 0;    targs[0].map = map;    if(threadwicked(targs) != NULL) err = true;  } else {    for(int i = 0; i < tnum; i++){      targs[i].fdb = fdb;      targs[i].rnum = rnum;      targs[i].nc = nc;      targs[i].id = i;      targs[i].map = map;      if(pthread_create(threads + i, NULL, threadwicked, targs + i) != 0){        eprint(fdb, "pthread_create");        targs[i].id = -1;        err = true;      }    }    for(int i = 0; i < tnum; i++){      if(targs[i].id == -1) continue;      void *rv;      if(pthread_join(threads[i], &rv) != 0){        eprint(fdb, "pthread_join");        err = true;      } else if(rv){        err = true;      }    }  }  if(!nc){    if(!tcfdbsync(fdb)){      eprint(fdb, "tcfdbsync");      err = true;    }    if(tcfdbrnum(fdb) != tcmaprnum(map)){      eprint(fdb, "(validation)");      err = true;    }    int end = rnum * tnum;    for(int i = 1; i <= end && !err; i++){      char kbuf[RECBUFSIZ];      int ksiz = sprintf(kbuf, "%d", i);      int vsiz;      const char *vbuf = tcmapget(map, kbuf, ksiz, &vsiz);      int rsiz;      char *rbuf = tcfdbget2(fdb, kbuf, ksiz, &rsiz);      if(vbuf){        iputchar('.');        if(vsiz > tcfdbwidth(fdb)) vsiz = tcfdbwidth(fdb);        if(!rbuf){          eprint(fdb, "tcfdbget");          err = true;        } else if(rsiz != vsiz || memcmp(rbuf, vbuf, rsiz)){          eprint(fdb, "(validation)");          err = true;        }      } else {        iputchar('*');        if(rbuf || tcfdbecode(fdb) != TCENOREC){          eprint(fdb, "(validation)");          err = true;        }      }      tcfree(rbuf);      if(i % 50 == 0) iprintf(" (%08d)\n", i);    }    if(rnum % 50 > 0) iprintf(" (%08d)\n", rnum);  }  tcmapdel(map);  iprintf("record number: %llu\n", (unsigned long long)tcfdbrnum(fdb));  iprintf("size: %llu\n", (unsigned long long)tcfdbfsiz(fdb));  mprint(fdb);  sysprint();  if(!tcfdbclose(fdb)){    eprint(fdb, "tcfdbclose");    err = true;  }  tcfdbdel(fdb);  iprintf("time: %.3f\n", tctime() - stime);  iprintf("%s\n\n", err ? "error" : "ok");  return err ? 1 : 0;}/* perform typical command */static int proctypical(const char *path, int tnum, int rnum, int width, int64_t limsiz,                       int omode, bool nc, int rratio){  iprintf("<Typical Access Test>\n  seed=%u  path=%s  tnum=%d  rnum=%d  width=%d  limsiz=%lld"          "  omode=%d  nc=%d  rratio=%d\n\n",          g_randseed, path, tnum, rnum, width, (long long)limsiz, omode, nc, rratio);  bool err = false;  double stime = tctime();  TCFDB *fdb = tcfdbnew();  if(g_dbgfd >= 0) tcfdbsetdbgfd(fdb, g_dbgfd);  if(!tcfdbsetmutex(fdb)){    eprint(fdb, "tcfdbsetmutex");    err = true;  }  if(!tcfdbtune(fdb, width, limsiz)){    eprint(fdb, "tcfdbtune");    err = true;  }  if(!tcfdbopen(fdb, path, FDBOWRITER | FDBOCREAT | FDBOTRUNC | omode)){    eprint(fdb, "tcfdbopen");    err = true;  }  TARGTYPICAL targs[tnum];  pthread_t threads[tnum];  if(tnum == 1){    targs[0].fdb = fdb;    targs[0].rnum = rnum;    targs[0].nc = nc;    targs[0].rratio = rratio;    targs[0].id = 0;    if(threadtypical(targs) != NULL) err = true;  } else {    for(int i = 0; i < tnum; i++){      targs[i].fdb = fdb;      targs[i].rnum = rnum;      targs[i].nc = nc;      targs[i].rratio= rratio;      targs[i].id = i;      if(pthread_create(threads + i, NULL, threadtypical, targs + i) != 0){        eprint(fdb, "pthread_create");        targs[i].id = -1;        err = true;      }    }    for(int i = 0; i < tnum; i++){      if(targs[i].id == -1) continue;      void *rv;      if(pthread_join(threads[i], &rv) != 0){        eprint(fdb, "pthread_join");        err = true;      } else if(rv){        err = true;      }    }  }  iprintf("record number: %llu\n", (unsigned long long)tcfdbrnum(fdb));  iprintf("size: %llu\n", (unsigned long long)tcfdbfsiz(fdb));  mprint(fdb);  sysprint();  if(!tcfdbclose(fdb)){    eprint(fdb, "tcfdbclose");    err = true;  }  tcfdbdel(fdb);  iprintf("time: %.3f\n", tctime() - stime);  iprintf("%s\n\n", err ? "error" : "ok");  return err ? 1 : 0;}/* thread the write function */static void *threadwrite(void *targ){  TCFDB *fdb = ((TARGWRITE *)targ)->fdb;  int rnum = ((TARGWRITE *)targ)->rnum;  bool rnd = ((TARGWRITE *)targ)->rnd;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -