1/* 2 * See the file LICENSE for redistribution information. 3 * 4 * Copyright (c) 2005-2009 Oracle. All rights reserved. 5 * 6 * $Id$ 7 */ 8#include "bench.h" 9 10static int b_open_usage(void); 11 12int 13b_open(int argc, char *argv[]) 14{ 15 extern char *optarg; 16 extern int optind, __db_getopt_reset; 17 DB_ENV *dbenv; 18 DB *dbp; 19 DBTYPE type; 20 int ch, i, count; 21 char *fname, *dbname, *ts; 22 23 type = DB_BTREE; 24 count = 1000; 25 fname = dbname = NULL; 26 ts = "Btree"; 27 __db_getopt_reset = 1; 28 while ((ch = getopt(argc, argv, "c:dft:")) != EOF) 29 switch (ch) { 30 case 'c': 31 count = atoi(optarg); 32 break; 33 case 'd': 34 dbname = "dbname"; 35 break; 36 case 'f': 37 fname = "filename"; 38 break; 39 case 't': 40 switch (optarg[0]) { 41 case 'B': case 'b': 42 ts = "Btree"; 43 type = DB_BTREE; 44 break; 45 case 'H': case 'h': 46 if (b_util_have_hash()) 47 return (0); 48 ts = "Hash"; 49 type = DB_HASH; 50 break; 51 case 'Q': case 'q': 52 if (b_util_have_queue()) 53 return (0); 54 ts = "Queue"; 55 type = DB_QUEUE; 56 break; 57 case 'R': case 'r': 58 ts = "Recno"; 59 type = DB_RECNO; 60 break; 61 default: 62 return (b_open_usage()); 63 } 64 break; 65 case '?': 66 default: 67 return (b_open_usage()); 68 } 69 argc -= optind; 70 argv += optind; 71 if (argc != 0) 72 return (b_open_usage()); 73 74#if DB_VERSION_MAJOR < 4 75 /* 76 * Don't run in-memory database tests on versions less than 3, it 77 * takes forever and eats memory. 78 */ 79 if (fname == NULL && dbname == NULL) 80 return (0); 81#endif 82#if DB_VERSION_MAJOR < 4 || DB_VERSION_MAJOR == 4 && DB_VERSION_MINOR < 4 83 /* 84 * Named in-memory databases weren't available until 4.4. 85 */ 86 if (fname == NULL && dbname != NULL) 87 return (0); 88#endif 89 90 /* Create the environment. */ 91 DB_BENCH_ASSERT(db_env_create(&dbenv, 0) == 0); 92 dbenv->set_errfile(dbenv, stderr); 93#if DB_VERSION_MAJOR == 3 && DB_VERSION_MINOR == 0 94 DB_BENCH_ASSERT(dbenv->open(dbenv, TESTDIR, 95 NULL, DB_CREATE | DB_INIT_MPOOL | DB_PRIVATE, 0666) == 0); 96#else 97 DB_BENCH_ASSERT(dbenv->open(dbenv, TESTDIR, 98 DB_CREATE | DB_INIT_MPOOL | DB_PRIVATE, 0666) == 0); 99#endif 100 101 /* Create the database. */ 102 DB_BENCH_ASSERT(db_create(&dbp, dbenv, 0) == 0); 103 104#if DB_VERSION_MAJOR >= 4 && DB_VERSION_MINOR >= 1 105 DB_BENCH_ASSERT(dbp->open( 106 dbp, NULL, fname, dbname, type, DB_CREATE, 0666) == 0); 107#else 108 DB_BENCH_ASSERT(dbp->open( 109 dbp, fname, dbname, type, DB_CREATE, 0666) == 0); 110#endif 111 DB_BENCH_ASSERT(dbp->close(dbp, 0) == 0); 112 113 /* Open the database count times. */ 114 TIMER_START; 115 for (i = 0; i < count; ++i) { 116 DB_BENCH_ASSERT(db_create(&dbp, dbenv, 0) == 0); 117#if DB_VERSION_MAJOR >= 4 && DB_VERSION_MINOR >= 1 118 DB_BENCH_ASSERT(dbp->open( 119 dbp, NULL, fname, dbname, type, DB_CREATE, 0666) == 0); 120#else 121 DB_BENCH_ASSERT(dbp->open( 122 dbp, fname, dbname, type, DB_CREATE, 0666) == 0); 123#endif 124 DB_BENCH_ASSERT(dbp->close(dbp, 0) == 0); 125 } 126 TIMER_STOP; 127 128 printf("# %d %s %sdatabase open/close pairs\n", 129 count, ts, 130 fname == NULL ? 131 (dbname == NULL ? "in-memory " : "named in-memory ") : 132 (dbname == NULL ? "" : "sub-")); 133 TIMER_DISPLAY(count); 134 135 DB_BENCH_ASSERT(dbenv->close(dbenv, 0) == 0); 136 137 return (0); 138} 139 140static int 141b_open_usage() 142{ 143 (void)fprintf(stderr, "usage: b_open [-df] [-c count] [-t type]\n"); 144 return (EXIT_FAILURE); 145} 146