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