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 b_txn_write_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, __db_getopt_reset; 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 __db_getopt_reset = 1; 52 while ((ch = getopt(argc, argv, "ac:rw")) != EOF) 53 switch (ch) { 54 case 'a': 55 config = "nosync"; 56 flags = DB_TXN_NOSYNC; 57 break; 58 case 'c': 59 count = atoi(optarg); 60 break; 61 case 'r': 62#ifdef DB_INIT_REP 63 rep_stub = 1; 64#else 65 exit(0); 66#endif 67 break; 68 case 'w': 69 config = "write-nosync"; 70#ifdef DB_TXN_WRITE_NOSYNC 71 flags = DB_TXN_WRITE_NOSYNC; 72#else 73 exit(0); 74#endif 75 break; 76 case '?': 77 default: 78 return (b_txn_write_usage()); 79 } 80 argc -= optind; 81 argv += optind; 82 if (argc != 0) 83 return (b_txn_write_usage()); 84 85 /* Create the environment. */ 86 DB_BENCH_ASSERT(db_env_create(&dbenv, 0) == 0); 87 dbenv->set_errfile(dbenv, stderr); 88 89#ifdef DB_INIT_REP 90 if (rep_stub) { 91#if DB_VERSION_MAJOR == 4 && DB_VERSION_MINOR >= 5 || DB_VERSION_MAJOR > 4 92 DB_BENCH_ASSERT( 93 dbenv->rep_set_transport(dbenv, 1, b_txn_write_send) == 0); 94#else 95 DB_BENCH_ASSERT( 96 dbenv->set_rep_transport(dbenv, 1, b_txn_write_send) == 0); 97#endif 98 oflags |= DB_INIT_REP; 99 } 100#endif 101 oflags |= DB_CREATE | DB_INIT_LOCK | 102 DB_INIT_LOG | DB_INIT_MPOOL | DB_INIT_TXN | DB_PRIVATE; 103#if DB_VERSION_MAJOR == 3 && DB_VERSION_MINOR == 0 104 DB_BENCH_ASSERT( 105 dbenv->open(dbenv, TESTDIR, NULL, flags | oflags, 0666) == 0); 106#endif 107#if DB_VERSION_MAJOR == 3 && DB_VERSION_MINOR == 1 108 DB_BENCH_ASSERT( 109 dbenv->open(dbenv, TESTDIR, flags | oflags, 0666) == 0); 110#endif 111#if DB_VERSION_MAJOR > 3 || DB_VERSION_MINOR > 1 112 if (flags != 0) 113 DB_BENCH_ASSERT(dbenv->set_flags(dbenv, flags, 1) == 0); 114 DB_BENCH_ASSERT(dbenv->open(dbenv, TESTDIR, oflags, 0666) == 0); 115#endif 116 117#ifdef DB_INIT_REP 118 if (rep_stub) 119 DB_BENCH_ASSERT( 120 dbenv->rep_start(dbenv, NULL, DB_REP_MASTER) == 0); 121#endif 122 123 /* Create the database. */ 124 DB_BENCH_ASSERT(db_create(&dbp, dbenv, 0) == 0); 125#if DB_VERSION_MAJOR >= 4 && DB_VERSION_MINOR >= 1 126 DB_BENCH_ASSERT(dbp->open(dbp, NULL, 127 TESTFILE, NULL, DB_BTREE, DB_CREATE | DB_AUTO_COMMIT, 0666) == 0); 128#else 129 DB_BENCH_ASSERT( 130 dbp->open(dbp, TESTFILE, NULL, DB_BTREE, DB_CREATE, 0666) == 0); 131#endif 132 133 /* Initialize the data. */ 134 memset(&key, 0, sizeof(key)); 135 memset(&data, 0, sizeof(data)); 136 key.size = data.size = 20; 137 key.data = data.data = "01234567890123456789"; 138 139 /* Start/commit a transaction count times. */ 140 TIMER_START; 141 for (i = 0; i < count; ++i) { 142#if DB_VERSION_MAJOR < 4 143 DB_BENCH_ASSERT(txn_begin(dbenv, NULL, &txn, 0) == 0); 144 DB_BENCH_ASSERT(dbp->put(dbp, txn, &key, &data, 0) == 0); 145 DB_BENCH_ASSERT(txn_commit(txn, 0) == 0); 146#else 147 DB_BENCH_ASSERT(dbenv->txn_begin(dbenv, NULL, &txn, 0) == 0); 148 DB_BENCH_ASSERT(dbp->put(dbp, txn, &key, &data, 0) == 0); 149 DB_BENCH_ASSERT(txn->commit(txn, 0) == 0); 150#endif 151 } 152 TIMER_STOP; 153 154 printf("# %d %stransactions write %s commit pairs\n", 155 count, rep_stub ? "replicated ": "", config); 156 TIMER_DISPLAY(count); 157 158 DB_BENCH_ASSERT(dbp->close(dbp, 0) == 0); 159 DB_BENCH_ASSERT(dbenv->close(dbenv, 0) == 0); 160 161 return (0); 162} 163 164static int 165b_txn_write_usage() 166{ 167 (void)fprintf(stderr, "usage: b_txn_write [-arw] [-c count]\n"); 168 return (EXIT_FAILURE); 169} 170