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

📄 tchmttest.c

📁 高性能嵌入式数据库在高并发的环境下使用最好是64位系统比较好
💻 C
📖 第 1 页 / 共 3 页
字号:
  }  tchdbdel(hdb);  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){  TCHDB *hdb = ((TARGWRITE *)targ)->hdb;  int rnum = ((TARGWRITE *)targ)->rnum;  bool as = ((TARGWRITE *)targ)->as;  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(as){      if(!tchdbputasync(hdb, buf, len, buf, len)){        eprint(hdb, "tchdbputasync");        err = true;        break;      }    } else {      if(!tchdbput(hdb, buf, len, buf, len)){        eprint(hdb, "tchdbput");        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){  TCHDB *hdb = ((TARGREAD *)targ)->hdb;  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){      char vbuf[RECBUFSIZ];      int vsiz = tchdbget3(hdb, kbuf, ksiz, vbuf, RECBUFSIZ);      if(vsiz < 0 && (!rnd || tchdbecode(hdb) != TCENOREC)){        eprint(hdb, "tchdbget3");        err = true;      }    } else {      char *vbuf = tchdbget(hdb, kbuf, ksiz, &vsiz);      if(!vbuf && (!rnd || tchdbecode(hdb) != TCENOREC)){        eprint(hdb, "tchdbget");        err = true;      }      tcfree(vbuf);    }    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 remove function */static void *threadremove(void *targ){  TCHDB *hdb = ((TARGREMOVE *)targ)->hdb;  int rnum = ((TARGREMOVE *)targ)->rnum;  bool rnd = ((TARGREMOVE *)targ)->rnd;  int id = ((TARGREMOVE *)targ)->id;  bool err = false;  int base = id * rnum;  for(int i = 1; i <= rnum; i++){    char kbuf[RECBUFSIZ];    int ksiz = sprintf(kbuf, "%08d", base + (rnd ? myrand(i + 1) : i));    if(!tchdbout(hdb, kbuf, ksiz) && (!rnd || tchdbecode(hdb) != TCENOREC)){      eprint(hdb, "tchdbout");      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 wicked function */static void *threadwicked(void *targ){  TCHDB *hdb = ((TARGWICKED *)targ)->hdb;  int rnum = ((TARGWICKED *)targ)->rnum;  bool nc = ((TARGWICKED *)targ)->nc;  int id = ((TARGWICKED *)targ)->id;  TCMAP *map = ((TARGWICKED *)targ)->map;  bool err = false;  for(int i = 1; i <= rnum && !err; i++){    char kbuf[RECBUFSIZ];    int ksiz = sprintf(kbuf, "%d", myrand(rnum * (id + 1)));    char vbuf[RECBUFSIZ];    int vsiz = myrand(RECBUFSIZ);    memset(vbuf, '*', vsiz);    vbuf[vsiz] = '\0';    char *rbuf;    if(!nc) tcglobalmutexlock();    switch(myrand(16)){    case 0:      if(id == 0) iputchar('0');      if(!tchdbput(hdb, kbuf, ksiz, vbuf, vsiz)){        eprint(hdb, "tchdbput");        err = true;      }      if(!nc) tcmapput(map, kbuf, ksiz, vbuf, vsiz);      break;    case 1:      if(id == 0) iputchar('1');      if(!tchdbput2(hdb, kbuf, vbuf)){        eprint(hdb, "tchdbput2");        err = true;      }      if(!nc) tcmapput2(map, kbuf, vbuf);      break;    case 2:      if(id == 0) iputchar('2');      if(!tchdbputkeep(hdb, kbuf, ksiz, vbuf, vsiz) && tchdbecode(hdb) != TCEKEEP){        eprint(hdb, "tchdbputkeep");        err = true;      }      if(!nc) tcmapputkeep(map, kbuf, ksiz, vbuf, vsiz);      break;    case 3:      if(id == 0) iputchar('3');      if(!tchdbputkeep2(hdb, kbuf, vbuf) && tchdbecode(hdb) != TCEKEEP){        eprint(hdb, "tchdbputkeep2");        err = true;      }      if(!nc) tcmapputkeep2(map, kbuf, vbuf);      break;    case 4:      if(id == 0) iputchar('4');      if(!tchdbputcat(hdb, kbuf, ksiz, vbuf, vsiz)){        eprint(hdb, "tchdbputcat");        err = true;      }      if(!nc) tcmapputcat(map, kbuf, ksiz, vbuf, vsiz);      break;    case 5:      if(id == 0) iputchar('5');      if(!tchdbputcat2(hdb, kbuf, vbuf)){        eprint(hdb, "tchdbputcat2");        err = true;      }      if(!nc) tcmapputcat2(map, kbuf, vbuf);      break;    case 6:      if(id == 0) iputchar('6');      if(i > rnum / 4 * 3){        if(!tchdbputasync(hdb, kbuf, ksiz, vbuf, vsiz)){          eprint(hdb, "tchdbputasync");          err = true;        }      } else {        if(!tchdbput(hdb, kbuf, ksiz, vbuf, vsiz)){          eprint(hdb, "tchdbput");          err = true;        }      }      if(!nc) tcmapput(map, kbuf, ksiz, vbuf, vsiz);      break;    case 7:      if(id == 0) iputchar('7');      if(i > rnum / 4 * 3){        if(!tchdbputasync2(hdb, kbuf, vbuf)){          eprint(hdb, "tchdbputasync2");          err = true;        }      } else {        if(!tchdbput2(hdb, kbuf, vbuf)){          eprint(hdb, "tchdbput2");          err = true;        }      }      if(!nc) tcmapput2(map, kbuf, vbuf);      break;    case 8:      if(id == 0) iputchar('8');      if(myrand(10) == 0){        if(!tchdbout(hdb, kbuf, ksiz) && tchdbecode(hdb) != TCENOREC){          eprint(hdb, "tchdbout");          err = true;        }        if(!nc) tcmapout(map, kbuf, ksiz);      }      break;    case 9:      if(id == 0) iputchar('9');      if(myrand(10) == 0){        if(!tchdbout2(hdb, kbuf) && tchdbecode(hdb) != TCENOREC){          eprint(hdb, "tchdbout2");          err = true;        }        if(!nc) tcmapout2(map, kbuf);      }      break;    case 10:      if(id == 0) iputchar('A');      if(!(rbuf = tchdbget(hdb, kbuf, ksiz, &vsiz))){        if(tchdbecode(hdb) != TCENOREC){          eprint(hdb, "tchdbget");          err = true;        }        rbuf = tcsprintf("[%d]", myrand(i + 1));        vsiz = strlen(rbuf);      }      vsiz += myrand(vsiz);      if(myrand(3) == 0) vsiz += PATH_MAX;      rbuf = tcrealloc(rbuf, vsiz + 1);      for(int j = 0; j < vsiz; j++){        rbuf[j] = myrand(0x100);      }      if(!tchdbput(hdb, kbuf, ksiz, rbuf, vsiz)){        eprint(hdb, "tchdbput");        err = true;      }      if(!nc) tcmapput(map, kbuf, ksiz, rbuf, vsiz);      tcfree(rbuf);      break;    case 11:      if(id == 0) iputchar('B');      if(!(rbuf = tchdbget(hdb, kbuf, ksiz, &vsiz)) && tchdbecode(hdb) != TCENOREC){        eprint(hdb, "tchdbget");        err = true;      }      tcfree(rbuf);      break;    case 12:      if(id == 0) iputchar('C');      if(!(rbuf = tchdbget2(hdb, kbuf)) && tchdbecode(hdb) != TCENOREC){        eprint(hdb, "tchdbget2");        err = true;      }      tcfree(rbuf);      break;    case 13:      if(id == 0) iputchar('D');      if(myrand(1) == 0) vsiz = 1;      if((vsiz = tchdbget3(hdb, kbuf, ksiz, vbuf, vsiz)) < 0 && tchdbecode(hdb) != TCENOREC){        eprint(hdb, "tchdbget3");        err = true;      }      break;    case 14:      if(id == 0) iputchar('E');      if(myrand(rnum / 50) == 0){        if(!tchdbiterinit(hdb)){          eprint(hdb, "tchdbiterinit");          err = true;        }      }      TCXSTR *ikey = tcxstrnew();      TCXSTR *ival = tcxstrnew();      for(int j = myrand(rnum) / 1000 + 1; j >= 0; j--){        if(j % 3 == 0){          if(!tchdbiternext3(hdb, ikey, ival)){            int ecode = tchdbecode(hdb);            if(ecode != TCEINVALID && ecode != TCENOREC){              eprint(hdb, "tchdbiternext3");              err = true;            }          }        } else {          int iksiz;          char *ikbuf = tchdbiternext(hdb, &iksiz);          if(ikbuf){            tcfree(ikbuf);          } else {            int ecode = tchdbecode(hdb);            if(ecode != TCEINVALID && ecode != TCENOREC){              eprint(hdb, "tchdbiternext");              err = true;            }          }        }      }      tcxstrdel(ival);      tcxstrdel(ikey);      break;    default:      if(id == 0) iputchar('@');      if(myrand(10000) == 0) srand((unsigned int)(tctime() * 1000) % UINT_MAX);      break;    }    if(!nc) tcglobalmutexunlock();    if(id == 0){      if(i % 50 == 0) iprintf(" (%08d)\n", i);      if(id == 0 && i == rnum / 4){        if(!tchdboptimize(hdb, rnum / 50, -1, -1, -1)){          eprint(hdb, "tchdboptimize");          err = true;        }        if(!tchdbiterinit(hdb)){          eprint(hdb, "tchdbiterinit");          err = true;        }      }    }  }  return err ? "error" : NULL;}/* thread the typical function */static void *threadtypical(void *targ){  TCHDB *hdb = ((TARGTYPICAL *)targ)->hdb;  int rnum = ((TARGTYPICAL *)targ)->rnum;  bool nc = ((TARGTYPICAL *)targ)->nc;  int rratio = ((TARGTYPICAL *)targ)->rratio;  int id = ((TARGTYPICAL *)targ)->id;  bool err = false;  TCMAP *map = (!nc && id == 0) ? tcmapnew2(rnum + 1) : NULL;  int base = id * rnum;  int mrange = tclmax(50 + rratio, 100);  for(int i = 1; !err && i <= rnum; i++){    char buf[RECBUFSIZ];    int len = sprintf(buf, "%08d", base + myrandnd(i));    int rnd = myrand(mrange);    if(rnd < 10){      if(!tchdbput(hdb, buf, len, buf, len)){        eprint(hdb, "tchdbput");        err = true;      }      if(map) tcmapput(map, buf, len, buf, len);    } else if(rnd < 15){      if(!tchdbputkeep(hdb, buf, len, buf, len) && tchdbecode(hdb) != TCEKEEP){        eprint(hdb, "tchdbputkeep");        err = true;      }      if(map) tcmapputkeep(map, buf, len, buf, len);    } else if(rnd < 20){      if(!tchdbputcat(hdb, buf, len, buf, len)){        eprint(hdb, "tchdbputcat");        err = true;      }      if(map) tcmapputcat(map, buf, len, buf, len);    } else if(rnd < 25){      if(i > rnum / 10 * 9){        if(!tchdbputasync(hdb, buf, len, buf, len)){          eprint(hdb, "tchdbputasync");          err = true;        }      } else {        if(!tchdbput(hdb, buf, len, buf, len)){          eprint(hdb, "tchdbput");          err = true;        }      }      if(map) tcmapput(map, buf, len, buf, len);    } else if(rnd < 30){      if(!tchdbout(hdb, buf, len) && tchdbecode(hdb) && tchdbecode(hdb) != TCENOREC){        eprint(hdb, "tchdbout");        err = true;      }      if(map) tcmapout(map, buf, len);    } else if(rnd < 31){      if(myrand(10) == 0 && !tchdbiterinit(hdb) && tchdbecode(hdb) != TCENOREC){        eprint(hdb, "tchdbiterinit");        err = true;      }      for(int j = 0; !err && j < 10; j++){        int ksiz;        char *kbuf = tchdbiternext(hdb, &ksiz);        if(kbuf){          tcfree(kbuf);        } else if(tchdbecode(hdb) != TCEINVALID && tchdbecode(hdb) != TCENOREC){          eprint(hdb, "tchdbiternext");          err = true;        }      }    } else {      int vsiz;      char *vbuf = tchdbget(hdb, buf, len, &vsiz);      if(vbuf){        if(map){          int msiz;          const char *mbuf = tcmapget(map, buf, len, &msiz);          if(!mbuf || msiz != vsiz || memcmp(mbuf, vbuf, vsiz)){            eprint(hdb, "(validation)");            err = true;          }        }        tcfree(vbuf);      } else {        if(tchdbecode(hdb) != TCENOREC){          eprint(hdb, "tchdbget");          err = true;        }        if(map && tcmapget(map, buf, len, &vsiz)){          eprint(hdb, "(validation)");          err = true;        }      }    }    if(id == 0 && rnum > 250 && i % (rnum / 250) == 0){      iputchar('.');      if(i == rnum || i % (rnum / 10) == 0) iprintf(" (%08d)\n", i);    }  }  if(map){    tcmapiterinit(map);    int ksiz;    const char *kbuf;    while(!err && (kbuf = tcmapiternext(map, &ksiz)) != NULL){      int vsiz;      char *vbuf = tchdbget(hdb, kbuf, ksiz, &vsiz);      if(vbuf){        int msiz;        const char *mbuf = tcmapget(map, kbuf, ksiz, &msiz);        if(!mbuf || msiz != vsiz || memcmp(mbuf, vbuf, vsiz)){          eprint(hdb, "(validation)");          err = true;        }        tcfree(vbuf);      } else {        eprint(hdb, "(validation)");        err = true;      }    }    tcmapdel(map);  }  return err ? "error" : NULL;}// END OF FILE

⌨️ 快捷键说明

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