1/*
2 * $Id: b_txn_write.c,v 1.13 2007/12/21 13:58:30 bostic Exp $
3 */
4#include "bench.h"
5
6static int usage __P((void));
7
8#ifdef DB_INIT_REP
9static int b_txn_write_send __P((DB_ENV *,
10    const DBT *, const DBT *, const DB_LSN *, int, u_int32_t));
11
12/*
13 * b_txn_write_send --
14 *	A stubbed-out replication message function.
15 */
16static int
17b_txn_write_send(dbenv, control, rec, lsn, eid, flags)
18	DB_ENV *dbenv;
19	const DBT *control, *rec;
20	const DB_LSN *lsn;
21	int eid;
22	u_int32_t flags;
23{
24	COMPQUIET(dbenv, NULL);
25	COMPQUIET(control, NULL);
26	COMPQUIET(rec, NULL);
27	COMPQUIET(lsn, NULL);
28	COMPQUIET(eid, 0);
29	COMPQUIET(flags, 0);
30	return (0);
31}
32#endif
33
34int
35b_txn_write(int argc, char *argv[])
36{
37	extern char *optarg;
38	extern int optind;
39	DB *dbp;
40	DBT key, data;
41	DB_ENV *dbenv;
42	DB_TXN *txn;
43	u_int32_t flags, oflags;
44	int ch, i, count, rep_stub;
45	char *config;
46
47	count = 1000;
48	oflags = flags = 0;
49	rep_stub = 0;
50	config = "synchronous";
51	while ((ch = getopt(argc, argv, "ac:rw")) != EOF)
52		switch (ch) {
53		case 'a':
54			config = "nosync";
55			flags = DB_TXN_NOSYNC;
56			break;
57		case 'c':
58			count = atoi(optarg);
59			break;
60		case 'r':
61#ifdef DB_INIT_REP
62			rep_stub = 1;
63#else
64			exit(0);
65#endif
66			break;
67		case 'w':
68			config = "write-nosync";
69#ifdef DB_TXN_WRITE_NOSYNC
70			flags = DB_TXN_WRITE_NOSYNC;
71#else
72			exit(0);
73#endif
74			break;
75		case '?':
76		default:
77			return (usage());
78		}
79	argc -= optind;
80	argv += optind;
81	if (argc != 0)
82		return (usage());
83
84	/* Create the environment. */
85	DB_BENCH_ASSERT(db_env_create(&dbenv, 0) == 0);
86	dbenv->set_errfile(dbenv, stderr);
87
88#ifdef DB_INIT_REP
89	if (rep_stub) {
90#if DB_VERSION_MAJOR == 4 && DB_VERSION_MINOR >= 5 || DB_VERSION_MAJOR > 4
91		DB_BENCH_ASSERT(
92		    dbenv->rep_set_transport(dbenv, 1, b_txn_write_send) == 0);
93#else
94		DB_BENCH_ASSERT(
95		    dbenv->set_rep_transport(dbenv, 1, b_txn_write_send) == 0);
96#endif
97		oflags |= DB_INIT_REP;
98	}
99#endif
100	oflags |= DB_CREATE | DB_INIT_LOCK |
101	    DB_INIT_LOG | DB_INIT_MPOOL | DB_INIT_TXN | DB_PRIVATE;
102#if DB_VERSION_MAJOR == 3 && DB_VERSION_MINOR == 0
103	DB_BENCH_ASSERT(
104	    dbenv->open(dbenv, TESTDIR, NULL, flags | oflags, 0666) == 0);
105#endif
106#if DB_VERSION_MAJOR == 3 && DB_VERSION_MINOR == 1
107	DB_BENCH_ASSERT(
108	    dbenv->open(dbenv, TESTDIR, flags | oflags, 0666) == 0);
109#endif
110#if DB_VERSION_MAJOR > 3 || DB_VERSION_MINOR > 1
111	if (flags != 0)
112		DB_BENCH_ASSERT(dbenv->set_flags(dbenv, flags, 1) == 0);
113	DB_BENCH_ASSERT(dbenv->open(dbenv, TESTDIR, oflags, 0666) == 0);
114#endif
115
116#ifdef DB_INIT_REP
117	if (rep_stub)
118		DB_BENCH_ASSERT(
119		    dbenv->rep_start(dbenv, NULL, DB_REP_MASTER) == 0);
120#endif
121
122	/* Create the database. */
123	DB_BENCH_ASSERT(db_create(&dbp, dbenv, 0) == 0);
124#if DB_VERSION_MAJOR >= 4 && DB_VERSION_MINOR >= 1
125	DB_BENCH_ASSERT(dbp->open(dbp, NULL,
126	    TESTFILE, NULL, DB_BTREE, DB_CREATE | DB_AUTO_COMMIT, 0666) == 0);
127#else
128	DB_BENCH_ASSERT(
129	    dbp->open(dbp, TESTFILE, NULL, DB_BTREE, DB_CREATE, 0666) == 0);
130#endif
131
132	/* Initialize the data. */
133	memset(&key, 0, sizeof(key));
134	memset(&data, 0, sizeof(data));
135	key.size = data.size = 20;
136	key.data = data.data = "01234567890123456789";
137
138	/* Start/commit a transaction count times. */
139	TIMER_START;
140	for (i = 0; i < count; ++i) {
141#if DB_VERSION_MAJOR < 4
142		DB_BENCH_ASSERT(txn_begin(dbenv, NULL, &txn, 0) == 0);
143		DB_BENCH_ASSERT(dbp->put(dbp, txn, &key, &data, 0) == 0);
144		DB_BENCH_ASSERT(txn_commit(txn, 0) == 0);
145#else
146		DB_BENCH_ASSERT(dbenv->txn_begin(dbenv, NULL, &txn, 0) == 0);
147		DB_BENCH_ASSERT(dbp->put(dbp, txn, &key, &data, 0) == 0);
148		DB_BENCH_ASSERT(txn->commit(txn, 0) == 0);
149#endif
150	}
151	TIMER_STOP;
152
153	printf("# %d %stransactions write %s commit pairs\n",
154	    count, rep_stub ? "replicated ": "", config);
155	TIMER_DISPLAY(count);
156
157	DB_BENCH_ASSERT(dbp->close(dbp, 0) == 0);
158	DB_BENCH_ASSERT(dbenv->close(dbenv, 0) == 0);
159
160	return (0);
161}
162
163static int
164usage()
165{
166	(void)fprintf(stderr, "usage: b_txn_write [-arw] [-c count]\n");
167	return (EXIT_FAILURE);
168}
169