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

📄 tcumttest.c

📁 Tokyo Cabinet的Tokyo Cabinet 是一个DBM的实现。这里的数据库由一系列key-value对的记录构成。key和value都可以是任意长度的字节序列,既可以是二进制也可以是字符
💻 C
📖 第 1 页 / 共 2 页
字号:
/************************************************************************************************* * The test cases of the on-memory database API *                                                      Copyright (C) 2006-2009 Mikio Hirabayashi * This file is part of Tokyo Cabinet. * Tokyo Cabinet is free software; you can redistribute it and/or modify it under the terms of * the GNU Lesser General Public License as published by the Free Software Foundation; either * version 2.1 of the License or any later version.  Tokyo Cabinet is distributed in the hope * that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public * License for more details. * You should have received a copy of the GNU Lesser General Public License along with Tokyo * Cabinet; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, * Boston, MA 02111-1307 USA. *************************************************************************************************/#include <tcutil.h>#include "myconf.h"#define RECBUFSIZ      48                // buffer for recordstypedef struct {                         // type of structure for combo thread  TCMDB *mdb;  TCNDB *ndb;  int rnum;  bool rnd;  int id;} TARGCOMBO;typedef struct {                         // type of structure for typical thread  TCMDB *mdb;  TCNDB *ndb;  int rnum;  bool nc;  int rratio;  int id;} TARGTYPICAL;/* global variables */const char *g_progname;                  // program nameunsigned int g_randseed;                 // random seed/* function prototypes */int main(int argc, char **argv);static void usage(void);static void iprintf(const char *format, ...);static void iputchar(int c);static void eprint(const char *func);static int myrand(int range);static int myrandnd(int range);static int runcombo(int argc, char **argv);static int runtypical(int argc, char **argv);static int proccombo(int tnum, int rnum, int bnum, bool tr, bool rnd);static int proctypical(int tnum, int rnum, int bnum, bool tr, bool nc, int rratio);static void *threadwrite(void *targ);static void *threadread(void *targ);static void *threadremove(void *targ);static void *threadtypical(void *targ);/* main routine */int main(int argc, char **argv){  g_progname = argv[0];  const char *ebuf = getenv("TCRNDSEED");  g_randseed = ebuf ? tcatoix(ebuf) : tctime() * 1000;  srand(g_randseed);  if(argc < 2) usage();  int rv = 0;  if(!strcmp(argv[1], "combo")){    rv = runcombo(argc, argv);  } else if(!strcmp(argv[1], "typical")){    rv = runtypical(argc, argv);  } else {    usage();  }  return rv;}/* print the usage and exit */static void usage(void){  fprintf(stderr, "%s: test cases of the on-memory database API of Tokyo Cabinet\n", g_progname);  fprintf(stderr, "\n");  fprintf(stderr, "usage:\n");  fprintf(stderr, "  %s combo [-tr] [-rnd] tnum rnum [bnum]\n", g_progname);  fprintf(stderr, "  %s typical [-tr] [-nc] [-rr num] tnum rnum [bnum]\n", g_progname);  fprintf(stderr, "\n");  exit(1);}/* print formatted information string and flush the buffer */static void iprintf(const char *format, ...){  va_list ap;  va_start(ap, format);  vprintf(format, ap);  fflush(stdout);  va_end(ap);}/* print a character and flush the buffer */static void iputchar(int c){  putchar(c);  fflush(stdout);}/* print error message of on-memory database */static void eprint(const char *func){  fprintf(stderr, "%s: %s: error\n", g_progname, func);}/* get a random number */static int myrand(int range){  if(range < 2) return 0;  int high = (unsigned int)rand() >> 4;  int low = range * (rand() / (RAND_MAX + 1.0));  low &= (unsigned int)INT_MAX >> 4;  return (high + low) % range;}/* get a random number based on normal distribution */static int myrandnd(int range){  int num = (int)tcdrandnd(range >> 1, range / 10);  return (num < 0 || num >= range) ? 0 : num;}/* parse arguments of combo command */static int runcombo(int argc, char **argv){  char *tstr = NULL;  char *rstr = NULL;  char *bstr = NULL;  bool tr = false;  bool rnd = false;  for(int i = 2; i < argc; i++){    if(!tstr && argv[i][0] == '-'){      if(!strcmp(argv[i], "-tr")){        tr = true;      } else if(!strcmp(argv[i], "-rnd")){        rnd = true;      } else {        usage();      }    } else if(!tstr){      tstr = argv[i];    } else if(!rstr){      rstr = argv[i];    } else if(!bstr){      bstr = argv[i];    } else {      usage();    }  }  if(!tstr || !rstr) usage();  int tnum = tcatoix(tstr);  int rnum = tcatoix(rstr);  if(tnum < 1 || rnum < 1) usage();  int bnum = bstr ? tcatoix(bstr) : -1;  int rv = proccombo(tnum, rnum, bnum, tr, rnd);  return rv;}/* parse arguments of typical command */static int runtypical(int argc, char **argv){  char *tstr = NULL;  char *rstr = NULL;  char *bstr = NULL;  bool tr = false;  int rratio = -1;  bool nc = false;  for(int i = 2; i < argc; i++){    if(!tstr && argv[i][0] == '-'){      if(!strcmp(argv[i], "-tr")){        tr = true;      } else if(!strcmp(argv[i], "-nc")){        nc = true;      } else if(!strcmp(argv[i], "-rr")){        if(++i >= argc) usage();        rratio = tcatoix(argv[i]);      } else {        usage();      }    } else if(!tstr){      tstr = argv[i];    } else if(!rstr){      rstr = argv[i];    } else if(!bstr){      bstr = argv[i];    } else {      usage();    }  }  if(!tstr || !rstr) usage();  int tnum = tcatoix(tstr);  int rnum = tcatoix(rstr);  if(tnum < 1 || rnum < 1) usage();  int bnum = bstr ? tcatoix(bstr) : -1;  int rv = proctypical(tnum, rnum, bnum, tr, nc, rratio);  return rv;}/* perform combo command */static int proccombo(int tnum, int rnum, int bnum, bool tr, bool rnd){  iprintf("<Combination Test>\n  seed=%u  tnum=%d  rnum=%d  bnum=%d  tr=%d  rnd=%d\n\n",          g_randseed, tnum, rnum, bnum, tr, rnd);  bool err = false;  double stime = tctime();  TCMDB *mdb = (bnum > 0) ? tcmdbnew2(bnum) : tcmdbnew();  TCNDB *ndb = tcndbnew();  TARGCOMBO targs[tnum];  pthread_t threads[tnum];  if(tnum == 1){    targs[0].mdb = mdb;    targs[0].ndb = tr ? ndb : NULL;    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].mdb = mdb;      targs[i].ndb = tr ? ndb : NULL;      targs[i].rnum = rnum;      targs[i].rnd = rnd;      targs[i].id = i;      if(pthread_create(threads + i, NULL, threadwrite, targs + i) != 0){        eprint("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("pthread_join");        err = true;      } else if(rv){        err = true;      }    }  }  if(tnum == 1){    targs[0].mdb = mdb;    targs[0].ndb = tr ? ndb : NULL;    targs[0].rnum = rnum;    targs[0].rnd = rnd;    targs[0].id = 0;    if(threadread(targs) != NULL) err = true;  } else {    for(int i = 0; i < tnum; i++){      targs[i].mdb = mdb;      targs[i].ndb = tr ? ndb : NULL;      targs[i].rnum = rnum;      targs[i].rnd = rnd;      targs[i].id = i;      if(pthread_create(threads + i, NULL, threadread, targs + i) != 0){        eprint("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("pthread_join");        err = true;      } else if(rv){        err = true;      }    }  }  if(tnum == 1){    targs[0].mdb = mdb;    targs[0].ndb = tr ? ndb : NULL;    targs[0].rnum = rnum;    targs[0].rnd = rnd;

⌨️ 快捷键说明

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