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