2 create a lot of random records, both current records and deleted records
4 Copyright (C) Andrew Tridgell 2008
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, see <http://www.gnu.org/licenses/>.
22 #include "lib/events/events.h"
23 #include "system/filesys.h"
26 #include "include/ctdb_protocol.h"
31 static struct timeval tp1,tp2;
33 static void start_timer(void)
35 gettimeofday(&tp1,NULL);
38 static double end_timer(void)
40 gettimeofday(&tp2,NULL);
41 return (tp2.tv_sec + (tp2.tv_usec*1.0e-6)) -
42 (tp1.tv_sec + (tp1.tv_usec*1.0e-6));
45 static int num_records = 10;
46 static int delete_pct = 75;
49 static void store_records(struct ctdb_context *ctdb, struct event_context *ev)
52 struct ctdb_db_context *ctdb_db;
53 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
55 struct ctdb_record_handle *h;
58 ctdb_db = ctdb_db_handle(ctdb, "test.tdb");
60 srandom(time(NULL) ^ getpid());
64 printf("working with %d records\n", num_records);
66 unsigned r = random() % num_records;
67 key.dptr = (uint8_t *)&r;
68 key.dsize = sizeof(r);
70 h = ctdb_fetch_lock(ctdb_db, tmp_ctx, key, &data);
72 printf("Failed to fetch record '%s' on node %d\n",
73 (const char *)key.dptr, ctdb_get_pnn(ctdb));
78 if (random() % 100 < delete_pct) {
82 data.dptr = talloc_zero_size(h, data.dsize + sizeof(r));
83 data.dsize += sizeof(r);
86 ret = ctdb_record_store(h, data);
89 printf("Failed to store record\n");
92 printf("%7.0f recs/second %u total\r", 1000.0 / end_timer(), i);
105 int main(int argc, const char *argv[])
107 struct ctdb_context *ctdb;
108 struct ctdb_db_context *ctdb_db;
110 struct poptOption popt_options[] = {
113 { "num-records", 'r', POPT_ARG_INT, &num_records, 0, "num_records", "integer" },
114 { "base-rec", 'b', POPT_ARG_INT, &base_rec, 0, "base_rec", "integer" },
115 { "delete-pct", 'p', POPT_ARG_INT, &delete_pct, 0, "delete_pct", "integer" },
119 const char **extra_argv;
122 struct event_context *ev;
124 pc = poptGetContext(argv[0], argc, argv, popt_options, POPT_CONTEXT_KEEP_FIRST);
126 while ((opt = poptGetNextOpt(pc)) != -1) {
129 fprintf(stderr, "Invalid option %s: %s\n",
130 poptBadOption(pc, 0), poptStrerror(opt));
135 /* setup the remaining options for the main program to use */
136 extra_argv = poptGetArgs(pc);
139 while (extra_argv[extra_argc]) extra_argc++;
142 ev = event_context_init(NULL);
144 ctdb = ctdb_cmdline_client(ev);
146 /* attach to a specific database */
147 ctdb_db = ctdb_attach(ctdb, "test.tdb", false, 0);
149 printf("ctdb_attach failed - %s\n", ctdb_errstr(ctdb));
153 store_records(ctdb, ev);