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

📄 tcbmttest.c

📁 高性能嵌入式数据库在高并发的环境下使用最好是64位系统比较好
💻 C
📖 第 1 页 / 共 3 页
字号:
  int tnum = tcatoi(tstr);  int rnum = tcatoi(rstr);  if(tnum < 1 || rnum < 1) usage();  int lmemb = lmstr ? tcatoi(lmstr) : -1;  int nmemb = nmstr ? tcatoi(nmstr) : -1;  int bnum = bstr ? tcatoi(bstr) : -1;  int apow = astr ? tcatoi(astr) : -1;  int fpow = fstr ? tcatoi(fstr) : -1;  int rv = proctypical(path, tnum, rnum, lmemb, nmemb, bnum, apow, fpow, opts, omode, nc, rratio);  return rv;}/* perform write command */static int procwrite(const char *path, int tnum, int rnum, int lmemb, int nmemb,                     int bnum, int apow, int fpow, int opts, int omode, bool rnd){  iprintf("<Writing Test>\n  path=%s  tnum=%d  rnum=%d  lmemb=%d  nmemb=%d"          "  bnum=%d  apow=%d  fpow=%d  opts=%d  omode=%d  rnd=%d\n\n",          path, tnum, rnum, lmemb, nmemb, bnum, apow, fpow, opts, omode, rnd);  bool err = false;  double stime = tctime();  TCBDB *bdb = tcbdbnew();  if(g_dbgfd >= 0) tcbdbsetdbgfd(bdb, g_dbgfd);  if(!tcbdbsetmutex(bdb)){    eprint(bdb, "tcbdbsetmutex");    err = true;  }  if(!tcbdbsetcodecfunc(bdb, _tc_recencode, NULL, _tc_recdecode, NULL)){    eprint(bdb, "tcbdbsetcodecfunc");    err = true;  }  if(!tcbdbtune(bdb, lmemb, nmemb, bnum, apow, fpow, opts)){    eprint(bdb, "tcbdbtune");    err = true;  }  if(!tcbdbopen(bdb, path, BDBOWRITER | BDBOCREAT | BDBOTRUNC | omode)){    eprint(bdb, "tcbdbopen");    err = true;  }  TARGWRITE targs[tnum];  pthread_t threads[tnum];  if(tnum == 1){    targs[0].bdb = bdb;    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].bdb = bdb;      targs[i].rnum = rnum;      targs[i].rnd = rnd;      targs[i].id = i;      if(pthread_create(threads + i, NULL, threadwrite, targs + i) != 0){        eprint(bdb, "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(bdb, "pthread_join");        err = true;      } else if(rv){        err = true;      }    }  }  iprintf("record number: %llu\n", (unsigned long long)tcbdbrnum(bdb));  iprintf("size: %llu\n", (unsigned long long)tcbdbfsiz(bdb));  mprint(bdb);  if(!tcbdbclose(bdb)){    eprint(bdb, "tcbdbclose");    err = true;  }  tcbdbdel(bdb);  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  path=%s  tnum=%d  omode=%d  wb=%d  rnd=%d\n\n",          path, tnum, omode, wb, rnd);  bool err = false;  double stime = tctime();  TCBDB *bdb = tcbdbnew();  if(g_dbgfd >= 0) tcbdbsetdbgfd(bdb, g_dbgfd);  if(!tcbdbsetmutex(bdb)){    eprint(bdb, "tcbdbsetmutex");    err = true;  }  if(!tcbdbsetcodecfunc(bdb, _tc_recencode, NULL, _tc_recdecode, NULL)){    eprint(bdb, "tcbdbsetcodecfunc");    err = true;  }  if(!tcbdbopen(bdb, path, BDBOREADER | omode)){    eprint(bdb, "tcbdbopen");    err = true;  }  int rnum = tcbdbrnum(bdb) / tnum;  TARGREAD targs[tnum];  pthread_t threads[tnum];  if(tnum == 1){    targs[0].bdb = bdb;    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].bdb = bdb;      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(bdb, "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(bdb, "pthread_join");        err = true;      } else if(rv){        err = true;      }    }  }  iprintf("record number: %llu\n", (unsigned long long)tcbdbrnum(bdb));  iprintf("size: %llu\n", (unsigned long long)tcbdbfsiz(bdb));  mprint(bdb);  if(!tcbdbclose(bdb)){    eprint(bdb, "tcbdbclose");    err = true;  }  tcbdbdel(bdb);  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  path=%s  tnum=%d  omode=%d  rnd=%d\n\n", path, tnum, omode, rnd);  bool err = false;  double stime = tctime();  TCBDB *bdb = tcbdbnew();  if(g_dbgfd >= 0) tcbdbsetdbgfd(bdb, g_dbgfd);  if(!tcbdbsetmutex(bdb)){    eprint(bdb, "tcbdbsetmutex");    err = true;  }  if(!tcbdbsetcodecfunc(bdb, _tc_recencode, NULL, _tc_recdecode, NULL)){    eprint(bdb, "tcbdbsetcodecfunc");    err = true;  }  if(!tcbdbopen(bdb, path, BDBOWRITER | omode)){    eprint(bdb, "tcbdbopen");    err = true;  }  int rnum = tcbdbrnum(bdb) / tnum;  TARGREMOVE targs[tnum];  pthread_t threads[tnum];  if(tnum == 1){    targs[0].bdb = bdb;    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].bdb = bdb;      targs[i].rnum = rnum;      targs[i].rnd = rnd;      targs[i].id = i;      if(pthread_create(threads + i, NULL, threadremove, targs + i) != 0){        eprint(bdb, "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(bdb, "pthread_join");        err = true;      } else if(rv){        err = true;      }    }  }  iprintf("record number: %llu\n", (unsigned long long)tcbdbrnum(bdb));  iprintf("size: %llu\n", (unsigned long long)tcbdbfsiz(bdb));  mprint(bdb);  if(!tcbdbclose(bdb)){    eprint(bdb, "tcbdbclose");    err = true;  }  tcbdbdel(bdb);  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 opts, int omode, bool nc){  iprintf("<Writing Test>\n  path=%s  tnum=%d  rnum=%d  opts=%d  omode=%d  nc=%d\n\n",          path, tnum, rnum, opts, omode, nc);  bool err = false;  double stime = tctime();  TCBDB *bdb = tcbdbnew();  if(g_dbgfd >= 0) tcbdbsetdbgfd(bdb, g_dbgfd);  if(!tcbdbsetmutex(bdb)){    eprint(bdb, "tcbdbsetmutex");    err = true;  }  if(!tcbdbsetcodecfunc(bdb, _tc_recencode, NULL, _tc_recdecode, NULL)){    eprint(bdb, "tcbdbsetcodecfunc");    err = true;  }  if(!tcbdbtune(bdb, 10, 10, rnum / 50, 10, -1, opts)){    eprint(bdb, "tcbdbtune");    err = true;  }  if(!tcbdbopen(bdb, path, BDBOWRITER | BDBOCREAT | BDBOTRUNC | omode)){    eprint(bdb, "tcbdbopen");    err = true;  }  TARGWICKED targs[tnum];  pthread_t threads[tnum];  TCMAP *map = tcmapnew();  if(tnum == 1){    targs[0].bdb = bdb;    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].bdb = bdb;      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(bdb, "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(bdb, "pthread_join");        err = true;      } else if(rv){        err = true;      }    }  }  if(!nc){    if(!tcbdbsync(bdb)){      eprint(bdb, "tcbdbsync");      err = true;    }    if(tcbdbrnum(bdb) != tcmaprnum(map)){      eprint(bdb, "(validation)");      err = true;    }    int end = rnum * tnum;    for(int i = 1; i <= end && !err; i++){      char kbuf[RECBUFSIZ];      int ksiz = sprintf(kbuf, "%d", i - 1);      int vsiz;      const char *vbuf = tcmapget(map, kbuf, ksiz, &vsiz);      int rsiz;      char *rbuf = tcbdbget(bdb, kbuf, ksiz, &rsiz);      if(vbuf){        iputchar('.');        if(!rbuf){          eprint(bdb, "tcbdbget");          err = true;        } else if(rsiz != vsiz || memcmp(rbuf, vbuf, rsiz)){          eprint(bdb, "(validation)");          err = true;        }      } else {        iputchar('*');        if(rbuf || tcbdbecode(bdb) != TCENOREC){          eprint(bdb, "(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)tcbdbrnum(bdb));  iprintf("size: %llu\n", (unsigned long long)tcbdbfsiz(bdb));  mprint(bdb);  if(!tcbdbclose(bdb)){    eprint(bdb, "tcbdbclose");    err = true;  }  tcbdbdel(bdb);  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 lmemb, int nmemb,                       int bnum, int apow, int fpow, int opts, int omode, bool nc, int rratio){  iprintf("<Typical Access Test>\n  path=%s  tnum=%d  rnum=%d  lmemb=%d  nmemb=%d"          "  bnum=%d  apow=%d  fpow=%d  opts=%d  omode=%d  nc=%d  rratio=%d\n\n",          path, tnum, rnum, lmemb, nmemb, bnum, apow, fpow, opts, omode, nc, rratio);  bool err = false;  double stime = tctime();  TCBDB *bdb = tcbdbnew();  if(g_dbgfd >= 0) tcbdbsetdbgfd(bdb, g_dbgfd);  if(!tcbdbsetmutex(bdb)){    eprint(bdb, "tcbdbsetmutex");    err = true;  }  if(!tcbdbsetcodecfunc(bdb, _tc_recencode, NULL, _tc_recdecode, NULL)){    eprint(bdb, "tcbdbsetcodecfunc");    err = true;  }  if(!tcbdbtune(bdb, lmemb, nmemb, bnum, apow, fpow, opts)){    eprint(bdb, "tcbdbtune");    err = true;  }  if(!tcbdbopen(bdb, path, BDBOWRITER | BDBOCREAT | BDBOTRUNC | omode)){    eprint(bdb, "tcbdbopen");    err = true;  }  TARGTYPICAL targs[tnum];  pthread_t threads[tnum];  if(tnum == 1){    targs[0].bdb = bdb;    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].bdb = bdb;      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(bdb, "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(bdb, "pthread_join");        err = true;      } else if(rv){        err = true;      }    }  }  iprintf("record number: %llu\n", (unsigned long long)tcbdbrnum(bdb));  iprintf("size: %llu\n", (unsigned long long)tcbdbfsiz(bdb));  mprint(bdb);  if(!tcbdbclose(bdb)){    eprint(bdb, "tcbdbclose");    err = true;  }  tcbdbdel(bdb);  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){  TCBDB *bdb = ((TARGWRITE *)targ)->bdb;  int rnum = ((TARGWRITE *)targ)->rnum;  bool rnd = ((TARGWRITE *)targ)->rnd;  int id = ((TARGWRITE *)targ)->id;  bool err = false;  int base = id * rnum;  for(int i = 1; i <= rnum; i++){    char buf[RECBUFSIZ];    int len = sprintf(buf, "%08d", base + (rnd ? myrand(i) : i));    if(!tcbdbput(bdb, buf, len, buf, len)){      eprint(bdb, "tcbdbput");      err = true;      break;    }    if(id <= 0 && rnum > 250 && i % (rnum / 250) == 0){      iputchar('.');      if(i == rnum || i % (rnum / 10) == 0) iprintf(" (%08d)\n", i);    }  }  return err ? "error" : NULL;}/* thread the read function */static void *threadread(void *targ){  TCBDB *bdb = ((TARGREAD *)targ)->bdb;  int rnum = ((TARGREAD *)targ)->rnum;  bool wb = ((TARGREAD *)targ)->wb;  bool rnd = ((TARGREAD *)targ)->rnd;  int id = ((TARGREAD *)targ)->id;  bool err = false;  int base = id * rnum;  for(int i = 1; i <= rnum && !err; i++){    char kbuf[RECBUFSIZ];    int ksiz = sprintf(kbuf, "%08d", base + (rnd ? myrandnd(i) : i));    int vsiz;    if(wb){      int vsiz;      const char *vbuf = tcbdbget3(bdb, kbuf, ksiz, &vsiz);      if(!vbuf && (!rnd || tcbdbecode(bdb) != TCENOREC)){        eprint(bdb, "tcbdbget3");        err = true;      }    } else {      char *vbuf = tcbdbget(bdb, kbuf, ksiz, &vsiz);      if(!vbuf && (!rnd || tcbdbecode(bdb) != TCENOREC)){        eprint(bdb, "tcbdbget");        err = true;      }      tcfree(vbuf);    }

⌨️ 快捷键说明

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