Imported Upstream version 5.3.21
[platform/upstream/libdb.git] / build_vxworks / test / micro / b_recover.c
1 /*
2  * See the file LICENSE for redistribution information.
3  *
4  * Copyright (c) 2005, 2012 Oracle and/or its affiliates.  All rights reserved.
5  *
6  * $Id$
7  */
8 #include "bench.h"
9
10 static int b_recover_usage(void);
11
12 int
13 b_recover(int argc, char *argv[])
14 {
15         extern char *optarg;
16         extern int optind, __db_getopt_reset;
17         DB *dbp;
18         DBT key, data;
19         DB_ENV *dbenv;
20         DB_TXN *txn;
21         u_int32_t cachesize;
22         int ch, i, count;
23
24         /*
25          * Recover was too slow before release 4.0 that it's not worth
26          * running the test.
27          */
28 #if DB_VERSION_MAJOR < 4
29         return (0);
30 #endif
31         cachesize = MEGABYTE;
32         count = 1000;
33         __db_getopt_reset = 1;
34         while ((ch = getopt(argc, argv, "C:c:")) != EOF)
35                 switch (ch) {
36                 case 'C':
37                         cachesize = (u_int32_t)atoi(optarg);
38                         break;
39                 case 'c':
40                         count = atoi(optarg);
41                         break;
42                 case '?':
43                 default:
44                         return (b_recover_usage());
45                 }
46         argc -= optind;
47         argv += optind;
48         if (argc != 0)
49                 return (b_recover_usage());
50
51         /* Create the environment. */
52         DB_BENCH_ASSERT(db_env_create(&dbenv, 0) == 0);
53         dbenv->set_errfile(dbenv, stderr);
54         DB_BENCH_ASSERT(dbenv->set_cachesize(dbenv, 0, cachesize, 0) == 0);
55
56 #define OFLAGS                                                          \
57         (DB_CREATE | DB_INIT_LOCK |                                     \
58         DB_INIT_LOG | DB_INIT_MPOOL | DB_INIT_TXN | DB_PRIVATE)
59 #if DB_VERSION_MAJOR == 3 && DB_VERSION_MINOR == 0
60         DB_BENCH_ASSERT(dbenv->open(dbenv, TESTDIR, NULL, OFLAGS, 0666) == 0);
61 #endif
62 #if DB_VERSION_MAJOR == 3 && DB_VERSION_MINOR == 1
63         DB_BENCH_ASSERT(dbenv->open(dbenv, TESTDIR, OFLAGS, 0666) == 0);
64 #endif
65 #if DB_VERSION_MAJOR > 3 || DB_VERSION_MINOR > 1
66         DB_BENCH_ASSERT(dbenv->open(dbenv, TESTDIR, OFLAGS, 0666) == 0);
67 #endif
68
69         /* Create the database. */
70         DB_BENCH_ASSERT(db_create(&dbp, dbenv, 0) == 0);
71 #if DB_VERSION_MAJOR > 4 || (DB_VERSION_MAJOR == 4 && DB_VERSION_MINOR >= 1)
72         DB_BENCH_ASSERT(dbp->open(dbp, NULL,
73             TESTFILE, NULL, DB_BTREE, DB_CREATE | DB_AUTO_COMMIT, 0666) == 0);
74 #else
75         DB_BENCH_ASSERT(
76             dbp->open(dbp, TESTFILE, NULL, DB_BTREE, DB_CREATE, 0666) == 0);
77 #endif
78
79         /* Initialize the data. */
80         memset(&key, 0, sizeof(key));
81         memset(&data, 0, sizeof(data));
82         key.size = data.size = 20;
83         key.data = data.data = "01234567890123456789";
84
85         /* Start/commit a transaction count times. */
86         for (i = 0; i < count; ++i) {
87 #if DB_VERSION_MAJOR < 4
88                 DB_BENCH_ASSERT(
89                     txn_begin(dbenv, NULL, &txn, DB_TXN_NOSYNC) == 0);
90                 DB_BENCH_ASSERT(dbp->put(dbp, txn, &key, &data, 0) == 0);
91                 DB_BENCH_ASSERT(txn_commit(txn, 0) == 0);
92 #else
93                 DB_BENCH_ASSERT(
94                     dbenv->txn_begin(dbenv, NULL, &txn, DB_TXN_NOSYNC) == 0);
95                 DB_BENCH_ASSERT(dbp->put(dbp, txn, &key, &data, 0) == 0);
96                 DB_BENCH_ASSERT(txn->commit(txn, 0) == 0);
97 #endif
98         }
99
100         DB_BENCH_ASSERT(dbp->close(dbp, 0) == 0);
101         DB_BENCH_ASSERT(dbenv->close(dbenv, 0) == 0);
102
103         /* Create a new DB_ENV handle. */
104         DB_BENCH_ASSERT(db_env_create(&dbenv, 0) == 0);
105         dbenv->set_errfile(dbenv, stderr);
106         DB_BENCH_ASSERT(
107             dbenv->set_cachesize(dbenv, 0, 1048576 /* 1MB */, 0) == 0);
108
109         /* Now run recovery. */
110         TIMER_START;
111 #if DB_VERSION_MAJOR == 3 && DB_VERSION_MINOR == 0
112         DB_BENCH_ASSERT(dbenv->open(
113             dbenv, TESTDIR, NULL, OFLAGS | DB_RECOVER, 0666) == 0);
114 #endif
115 #if DB_VERSION_MAJOR == 3 && DB_VERSION_MINOR == 1
116         DB_BENCH_ASSERT(
117             dbenv->open(dbenv, TESTDIR, OFLAGS | DB_RECOVER, 0666) == 0);
118 #endif
119 #if DB_VERSION_MAJOR > 3 || DB_VERSION_MINOR > 1
120         DB_BENCH_ASSERT(
121             dbenv->open(dbenv, TESTDIR, OFLAGS | DB_RECOVER, 0666) == 0);
122 #endif
123         TIMER_STOP;
124
125         /*
126          * We divide the time by the number of transactions, so an "operation"
127          * is the recovery of a single transaction.
128          */
129         printf("# recovery after %d transactions\n", count);
130         TIMER_DISPLAY(count);
131
132         DB_BENCH_ASSERT(dbenv->close(dbenv, 0) == 0);
133
134         return (0);
135 }
136
137 static int
138 b_recover_usage()
139 {
140         (void)fprintf(stderr, "usage: b_recover [-C cachesz] [-c count]\n");
141         return (EXIT_FAILURE);
142 }