2
* See the file LICENSE for redistribution information.
4
* Copyright (c) 1997-2002
5
* Sleepycat Software. All rights reserved.
10
#include <sys/types.h>
18
extern int getopt(int, char * const *, const char *);
25
typedef enum { ACCOUNT, BRANCH, TELLER } FTYPE;
27
DB_ENV *db_init __P((const char *, const char *, int, int, u_int32_t));
28
int hpopulate __P((DB *, int, int, int, int));
29
int populate __P((DB *, u_int32_t, u_int32_t, int, const char *));
30
u_int32_t random_id __P((FTYPE, int, int, int));
31
u_int32_t random_int __P((u_int32_t, u_int32_t));
32
int tp_populate __P((DB_ENV *, int, int, int, int, int));
33
int tp_run __P((DB_ENV *, int, int, int, int, int));
34
int tp_txn __P((DB_ENV *, DB *, DB *, DB *, DB *, int, int, int, int));
36
int invarg __P((const char *, int, const char *));
37
int main __P((int, char *[]));
38
int usage __P((const char *));
41
* This program implements a basic TPC/B driver program. To create the
42
* TPC/B database, run with the -i (init) flag. The number of records
43
* with which to populate the account, history, branch, and teller tables
44
* is specified by the a, s, b, and t flags respectively. To run a TPC/B
45
* test, use the n flag to indicate a number of transactions to run (note
46
* that you can run many of these processes in parallel to simulate a
47
* multiuser test run).
49
#define TELLERS_PER_BRANCH 10
50
#define ACCOUNTS_PER_TELLER 10000
51
#define HISTORY_PER_BRANCH 2592000
54
* The default configuration that adheres to TPCB scaling rules requires
55
* nearly 3 GB of space. To avoid requiring that much space for testing,
56
* we set the parameters much lower. If you want to run a valid 10 TPS
57
* configuration, define VALID_SCALING.
60
#define ACCOUNTS 1000000
63
#define HISTORY 25920000
80
#if !defined(VALID_SCALING) && !defined(TINY) && !defined(VERY_TINY)
81
#define ACCOUNTS 100000
84
#define HISTORY 259200
87
#define HISTORY_LEN 100
91
typedef struct _defrec {
94
u_int8_t pad[RECLEN - sizeof(u_int32_t) - sizeof(u_int32_t)];
97
typedef struct _histrec {
102
u_int8_t pad[RECLEN - 4 * sizeof(u_int32_t)];
113
int accounts, branches, seed, tellers, history;
114
int ch, iflag, mpool, ntxns, ret, txn_no_sync, verbose;
115
const char *home, *progname;
118
progname = "ex_tpcb";
119
accounts = branches = history = tellers = 0;
120
iflag = mpool = ntxns = txn_no_sync = verbose = 0;
121
seed = (int)time(NULL);
123
while ((ch = getopt(argc, argv, "a:b:c:fh:in:S:s:t:v")) != EOF)
125
case 'a': /* Number of account records */
126
if ((accounts = atoi(optarg)) <= 0)
127
return (invarg(progname, ch, optarg));
129
case 'b': /* Number of branch records */
130
if ((branches = atoi(optarg)) <= 0)
131
return (invarg(progname, ch, optarg));
133
case 'c': /* Cachesize in bytes */
134
if ((mpool = atoi(optarg)) <= 0)
135
return (invarg(progname, ch, optarg));
137
case 'f': /* Fast mode: no txn sync. */
140
case 'h': /* DB home. */
143
case 'i': /* Initialize the test. */
146
case 'n': /* Number of transactions */
147
if ((ntxns = atoi(optarg)) <= 0)
148
return (invarg(progname, ch, optarg));
150
case 'S': /* Random number seed. */
151
if ((seed = atoi(optarg)) <= 0)
152
return (invarg(progname, ch, optarg));
154
case 's': /* Number of history records */
155
if ((history = atoi(optarg)) <= 0)
156
return (invarg(progname, ch, optarg));
158
case 't': /* Number of teller records */
159
if ((tellers = atoi(optarg)) <= 0)
160
return (invarg(progname, ch, optarg));
162
case 'v': /* Verbose option. */
167
return (usage(progname));
174
/* Initialize the database environment. */
175
if ((dbenv = db_init(home,
176
progname, mpool, iflag, txn_no_sync ? DB_TXN_NOSYNC : 0)) == NULL)
177
return (EXIT_FAILURE);
179
accounts = accounts == 0 ? ACCOUNTS : accounts;
180
branches = branches == 0 ? BRANCHES : branches;
181
tellers = tellers == 0 ? TELLERS : tellers;
182
history = history == 0 ? HISTORY : history;
185
printf("%ld Accounts, %ld Branches, %ld Tellers, %ld History\n",
186
(long)accounts, (long)branches,
187
(long)tellers, (long)history);
191
return (usage(progname));
193
accounts, branches, history, tellers, verbose);
196
return (usage(progname));
197
tp_run(dbenv, ntxns, accounts, branches, tellers, verbose);
200
if ((ret = dbenv->close(dbenv, 0)) != 0) {
201
fprintf(stderr, "%s: dbenv->close failed: %s\n",
202
progname, db_strerror(ret));
203
return (EXIT_FAILURE);
206
return (EXIT_SUCCESS);
210
invarg(progname, arg, str)
211
const char *progname;
215
(void)fprintf(stderr,
216
"%s: invalid argument for -%c: %s\n", progname, arg, str);
217
return (EXIT_FAILURE);
222
const char *progname;
226
a1 = "[-fv] [-a accounts] [-b branches]\n";
227
a2 = "\t[-c cache_size] [-h home] [-S seed] [-s history] [-t tellers]";
228
(void)fprintf(stderr, "usage: %s -i %s %s\n", progname, a1, a2);
229
(void)fprintf(stderr,
230
" %s -n transactions %s %s\n", progname, a1, a2);
231
return (EXIT_FAILURE);
236
* Initialize the environment.
239
db_init(home, prefix, cachesize, initializing, flags)
240
const char *home, *prefix;
241
int cachesize, initializing;
245
u_int32_t local_flags;
248
if ((ret = db_env_create(&dbenv, 0)) != 0) {
249
dbenv->err(dbenv, ret, "db_env_create");
252
dbenv->set_errfile(dbenv, stderr);
253
dbenv->set_errpfx(dbenv, prefix);
254
(void)dbenv->set_cachesize(dbenv, 0,
255
cachesize == 0 ? 4 * 1024 * 1024 : (u_int32_t)cachesize, 0);
257
if (flags & (DB_TXN_NOSYNC))
258
(void)dbenv->set_flags(dbenv, DB_TXN_NOSYNC, 1);
259
flags &= ~(DB_TXN_NOSYNC);
261
local_flags = flags | DB_CREATE | (initializing ? DB_INIT_MPOOL :
262
DB_INIT_TXN | DB_INIT_LOCK | DB_INIT_LOG | DB_INIT_MPOOL);
263
if ((ret = dbenv->open(dbenv, home, local_flags, 0)) != 0) {
264
dbenv->err(dbenv, ret, "DB_ENV->open: %s", home);
265
(void)dbenv->close(dbenv, 0);
272
* Initialize the database to the specified number of accounts, branches,
273
* history records, and tellers.
276
tp_populate(env, accounts, branches, history, tellers, verbose)
278
int accounts, branches, history, tellers, verbose;
281
u_int32_t balance, idnum, oflags;
282
u_int32_t end_anum, end_bnum, end_tnum;
283
u_int32_t start_anum, start_bnum, start_tnum;
288
oflags = DB_CREATE | DB_TRUNCATE;
290
if ((ret = db_create(&dbp, env, 0)) != 0) {
291
env->err(env, ret, "db_create");
294
(void)dbp->set_h_nelem(dbp, (u_int32_t)accounts);
296
if ((ret = dbp->open(dbp, NULL, "account", NULL,
297
DB_HASH, oflags, 0644)) != 0) {
298
env->err(env, ret, "DB->open: account");
303
populate(dbp, idnum, balance, accounts, "account");
305
end_anum = idnum - 1;
306
if ((ret = dbp->close(dbp, 0)) != 0) {
307
env->err(env, ret, "DB->close: account");
311
printf("Populated accounts: %ld - %ld\n",
312
(long)start_anum, (long)end_anum);
315
* Since the number of branches is very small, we want to use very
316
* small pages and only 1 key per page, i.e., key-locking instead
319
if ((ret = db_create(&dbp, env, 0)) != 0) {
320
env->err(env, ret, "db_create");
323
(void)dbp->set_h_ffactor(dbp, 1);
324
(void)dbp->set_h_nelem(dbp, (u_int32_t)branches);
325
(void)dbp->set_pagesize(dbp, 512);
326
if ((ret = dbp->open(dbp, NULL, "branch", NULL,
327
DB_HASH, oflags, 0644)) != 0) {
328
env->err(env, ret, "DB->open: branch");
332
populate(dbp, idnum, balance, branches, "branch");
334
end_bnum = idnum - 1;
335
if ((ret = dbp->close(dbp, 0)) != 0) {
336
env->err(env, ret, "DB->close: branch");
340
printf("Populated branches: %ld - %ld\n",
341
(long)start_bnum, (long)end_bnum);
344
* In the case of tellers, we also want small pages, but we'll let
345
* the fill factor dynamically adjust itself.
347
if ((ret = db_create(&dbp, env, 0)) != 0) {
348
env->err(env, ret, "db_create");
351
(void)dbp->set_h_ffactor(dbp, 0);
352
(void)dbp->set_h_nelem(dbp, (u_int32_t)tellers);
353
(void)dbp->set_pagesize(dbp, 512);
354
if ((ret = dbp->open(dbp, NULL, "teller", NULL,
355
DB_HASH, oflags, 0644)) != 0) {
356
env->err(env, ret, "DB->open: teller");
361
populate(dbp, idnum, balance, tellers, "teller");
363
end_tnum = idnum - 1;
364
if ((ret = dbp->close(dbp, 0)) != 0) {
365
env->err(env, ret, "DB->close: teller");
369
printf("Populated tellers: %ld - %ld\n",
370
(long)start_tnum, (long)end_tnum);
372
if ((ret = db_create(&dbp, env, 0)) != 0) {
373
env->err(env, ret, "db_create");
376
(void)dbp->set_re_len(dbp, HISTORY_LEN);
377
if ((ret = dbp->open(dbp, NULL, "history", NULL,
378
DB_RECNO, oflags, 0644)) != 0) {
379
env->err(env, ret, "DB->open: history");
383
hpopulate(dbp, history, accounts, branches, tellers);
384
if ((ret = dbp->close(dbp, 0)) != 0) {
385
env->err(env, ret, "DB->close: history");
392
populate(dbp, start_id, balance, nrecs, msg)
394
u_int32_t start_id, balance;
403
kdbt.data = &drec.id;
404
kdbt.size = sizeof(u_int32_t);
407
ddbt.size = sizeof(drec);
408
memset(&drec.pad[0], 1, sizeof(drec.pad));
410
for (i = 0; i < nrecs; i++) {
411
drec.id = start_id + (u_int32_t)i;
412
drec.balance = balance;
414
(dbp->put)(dbp, NULL, &kdbt, &ddbt, DB_NOOVERWRITE)) != 0) {
416
ret, "Failure initializing %s file\n", msg);
424
hpopulate(dbp, history, accounts, branches, tellers)
426
int history, accounts, branches, tellers;
433
memset(&kdbt, 0, sizeof(kdbt));
434
memset(&ddbt, 0, sizeof(ddbt));
436
ddbt.size = sizeof(hrec);
438
kdbt.size = sizeof(key);
439
memset(&hrec.pad[0], 1, sizeof(hrec.pad));
442
for (i = 1; i <= history; i++) {
443
hrec.aid = random_id(ACCOUNT, accounts, branches, tellers);
444
hrec.bid = random_id(BRANCH, accounts, branches, tellers);
445
hrec.tid = random_id(TELLER, accounts, branches, tellers);
446
if ((ret = dbp->put(dbp, NULL, &kdbt, &ddbt, DB_APPEND)) != 0) {
447
dbp->err(dbp, ret, "dbp->put");
462
#define RAND_MAX 0x7fffffff
465
ret = (u_int32_t)(((double)t / ((double)(RAND_MAX) + 1)) *
472
random_id(type, accounts, branches, tellers)
474
int accounts, branches, tellers;
476
u_int32_t min, max, num;
493
return (random_int(min, max));
497
tp_run(dbenv, n, accounts, branches, tellers, verbose)
499
int n, accounts, branches, tellers, verbose;
501
DB *adb, *bdb, *hdb, *tdb;
503
int failed, ifailed, ret, txns;
504
time_t starttime, curtime, lasttime;
506
adb = bdb = hdb = tdb = NULL;
510
* Open the database files.
512
if ((ret = db_create(&adb, dbenv, 0)) != 0) {
513
dbenv->err(dbenv, ret, "db_create");
516
if ((ret = adb->open(adb, NULL, "account", NULL, DB_UNKNOWN,
517
DB_AUTO_COMMIT, 0)) != 0) {
518
dbenv->err(dbenv, ret, "DB->open: account");
521
if ((ret = db_create(&bdb, dbenv, 0)) != 0) {
522
dbenv->err(dbenv, ret, "db_create");
525
if ((ret = bdb->open(bdb, NULL, "branch", NULL, DB_UNKNOWN,
526
DB_AUTO_COMMIT, 0)) != 0) {
527
dbenv->err(dbenv, ret, "DB->open: branch");
530
if ((ret = db_create(&hdb, dbenv, 0)) != 0) {
531
dbenv->err(dbenv, ret, "db_create");
534
if ((ret = hdb->open(hdb, NULL, "history", NULL, DB_UNKNOWN,
535
DB_AUTO_COMMIT, 0)) != 0) {
536
dbenv->err(dbenv, ret, "DB->open: history");
539
if ((ret = db_create(&tdb, dbenv, 0)) != 0) {
540
dbenv->err(dbenv, ret, "db_create");
543
if ((ret = tdb->open(tdb, NULL, "teller", NULL, DB_UNKNOWN,
544
DB_AUTO_COMMIT, 0)) != 0) {
545
dbenv->err(dbenv, ret, "DB->open: teller");
549
starttime = time(NULL);
550
lasttime = starttime;
551
for (ifailed = 0; n-- > 0;) {
553
ret = tp_txn(dbenv, adb, bdb, tdb, hdb,
554
accounts, branches, tellers, verbose);
560
curtime = time(NULL);
561
gtps = (double)(txns - failed) / (curtime - starttime);
562
itps = (double)(5000 - ifailed) / (curtime - lasttime);
563
printf("%d txns %d failed ", txns, failed);
564
printf("%6.2f TPS (gross) %6.2f TPS (interval)\n",
571
err: if (adb != NULL)
572
(void)adb->close(adb, 0);
574
(void)bdb->close(bdb, 0);
576
(void)tdb->close(tdb, 0);
578
(void)hdb->close(hdb, 0);
580
printf("%ld transactions begun %ld failed\n", (long)txns, (long)failed);
581
return (ret == 0 ? 0 : 1);
585
* XXX Figure out the appropriate way to pick out IDs.
588
tp_txn(dbenv, adb, bdb, tdb, hdb, accounts, branches, tellers, verbose)
590
DB *adb, *bdb, *tdb, *hdb;
591
int accounts, branches, tellers, verbose;
593
DBC *acurs, *bcurs, *tcurs;
594
DBT d_dbt, d_histdbt, k_dbt, k_histdbt;
599
int account, branch, teller, ret;
602
acurs = bcurs = tcurs = NULL;
605
* XXX We could move a lot of this into the driver to make this
608
account = random_id(ACCOUNT, accounts, branches, tellers);
609
branch = random_id(BRANCH, accounts, branches, tellers);
610
teller = random_id(TELLER, accounts, branches, tellers);
612
memset(&d_histdbt, 0, sizeof(d_histdbt));
614
memset(&k_histdbt, 0, sizeof(k_histdbt));
615
k_histdbt.data = &key;
616
k_histdbt.size = sizeof(key);
618
memset(&k_dbt, 0, sizeof(k_dbt));
619
k_dbt.size = sizeof(int);
621
memset(&d_dbt, 0, sizeof(d_dbt));
622
d_dbt.flags = DB_DBT_USERMEM;
624
d_dbt.ulen = sizeof(rec);
630
/* Request 0 bytes since we're just positioning. */
631
d_histdbt.flags = DB_DBT_PARTIAL;
634
if (dbenv->txn_begin(dbenv, NULL, &t, 0) != 0)
637
if (adb->cursor(adb, t, &acurs, 0) != 0 ||
638
bdb->cursor(bdb, t, &bcurs, 0) != 0 ||
639
tdb->cursor(tdb, t, &tcurs, 0) != 0)
643
k_dbt.data = &account;
644
if (acurs->c_get(acurs, &k_dbt, &d_dbt, DB_SET) != 0)
647
if (acurs->c_put(acurs, &k_dbt, &d_dbt, DB_CURRENT) != 0)
651
k_dbt.data = &branch;
652
if (bcurs->c_get(bcurs, &k_dbt, &d_dbt, DB_SET) != 0)
655
if (bcurs->c_put(bcurs, &k_dbt, &d_dbt, DB_CURRENT) != 0)
659
k_dbt.data = &teller;
660
if (tcurs->c_get(tcurs, &k_dbt, &d_dbt, DB_SET) != 0)
663
if (tcurs->c_put(tcurs, &k_dbt, &d_dbt, DB_CURRENT) != 0)
668
d_histdbt.data = &hrec;
669
d_histdbt.ulen = sizeof(hrec);
670
if (hdb->put(hdb, t, &k_histdbt, &d_histdbt, DB_APPEND) != 0)
673
if (acurs->c_close(acurs) != 0 || bcurs->c_close(bcurs) != 0 ||
674
tcurs->c_close(tcurs) != 0)
677
ret = t->commit(t, 0);
685
err: if (acurs != NULL)
686
(void)acurs->c_close(acurs);
688
(void)bcurs->c_close(bcurs);
690
(void)tcurs->c_close(tcurs);
695
printf("Transaction A=%ld B=%ld T=%ld failed\n",
696
(long)account, (long)branch, (long)teller);