lib/tls: Change default supported TLS versions.
[Samba.git] / ctdb / tests / src / ctdb_randrec.c
blob60d233bed81b8cd07b51d318d029a50838a6d0e0
1 /*
2 create a lot of random records, both current records and deleted records
4 Copyright (C) Andrew Tridgell 2008
5 Ronnie sahlberg 2007
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/>.
21 #include "includes.h"
22 #include "system/filesys.h"
23 #include "popt.h"
24 #include "cmdline.h"
25 #include "ctdb_private.h"
27 #include <sys/time.h>
28 #include <time.h>
30 static struct timeval tp1,tp2;
32 static void start_timer(void)
34 gettimeofday(&tp1,NULL);
37 static double end_timer(void)
39 gettimeofday(&tp2,NULL);
40 return (tp2.tv_sec + (tp2.tv_usec*1.0e-6)) -
41 (tp1.tv_sec + (tp1.tv_usec*1.0e-6));
44 static int num_records = 10;
45 static int delete_pct = 75;
46 static int base_rec;
48 static void store_records(struct ctdb_context *ctdb, struct event_context *ev)
50 TDB_DATA key, data;
51 struct ctdb_db_context *ctdb_db;
52 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
53 int ret;
54 struct ctdb_record_handle *h;
55 uint32_t i=0;
57 ctdb_db = ctdb_db_handle(ctdb, "test.tdb");
59 srandom(time(NULL) ^ getpid());
61 start_timer();
63 printf("working with %d records\n", num_records);
64 while (1) {
65 unsigned r = random() % num_records;
66 key.dptr = (uint8_t *)&r;
67 key.dsize = sizeof(r);
69 h = ctdb_fetch_lock(ctdb_db, tmp_ctx, key, &data);
70 if (h == NULL) {
71 printf("Failed to fetch record '%s' on node %d\n",
72 (const char *)key.dptr, ctdb_get_pnn(ctdb));
73 talloc_free(tmp_ctx);
74 return;
77 if (random() % 100 < delete_pct) {
78 data.dptr = NULL;
79 data.dsize = 0;
80 } else {
81 data.dptr = talloc_zero_size(h, data.dsize + sizeof(r));
82 data.dsize += sizeof(r);
85 ret = ctdb_record_store(h, data);
86 if (ret != 0) {
87 printf("Failed to store record\n");
90 if (data.dptr == NULL && data.dsize == 0) {
91 struct ctdb_control_schedule_for_deletion *dd;
92 TDB_DATA indata;
93 int32_t status;
95 indata.dsize = offsetof(struct ctdb_control_schedule_for_deletion, key) + key.dsize;
96 indata.dptr = talloc_zero_array(ctdb, uint8_t, indata.dsize);
97 if (indata.dptr == NULL) {
98 printf("out of memory\n");
99 exit(1);
101 dd = (struct ctdb_control_schedule_for_deletion *)(void *)indata.dptr;
102 dd->db_id = ctdb_db->db_id;
103 dd->hdr = *ctdb_header_from_record_handle(h);
104 dd->keylen = key.dsize;
105 memcpy(dd->key, key.dptr, key.dsize);
107 ret = ctdb_control(ctdb,
108 CTDB_CURRENT_NODE,
109 ctdb_db->db_id,
110 CTDB_CONTROL_SCHEDULE_FOR_DELETION,
111 0, /* flags */
112 indata,
113 NULL, /* mem_ctx */
114 NULL, /* outdata */
115 &status,
116 NULL, /* timeout : NULL == wait forever */
117 NULL); /* error message */
119 talloc_free(indata.dptr);
121 if (ret != 0 || status != 0) {
122 DEBUG(DEBUG_ERR, (__location__ " Error sending "
123 "SCHEDULE_FOR_DELETION "
124 "control.\n"));
128 talloc_free(h);
130 if (i % 1000 == 0) {
131 printf("%7.0f recs/second %u total\r", 1000.0 / end_timer(), i);
132 fflush(stdout);
133 start_timer();
135 i++;
138 talloc_free(tmp_ctx);
142 main program
144 int main(int argc, const char *argv[])
146 struct ctdb_context *ctdb;
147 struct ctdb_db_context *ctdb_db;
149 struct poptOption popt_options[] = {
150 POPT_AUTOHELP
151 POPT_CTDB_CMDLINE
152 { "num-records", 'r', POPT_ARG_INT, &num_records, 0, "num_records", "integer" },
153 { "base-rec", 'b', POPT_ARG_INT, &base_rec, 0, "base_rec", "integer" },
154 { "delete-pct", 'p', POPT_ARG_INT, &delete_pct, 0, "delete_pct", "integer" },
155 POPT_TABLEEND
157 int opt;
158 const char **extra_argv;
159 int extra_argc = 0;
160 poptContext pc;
161 struct event_context *ev;
163 pc = poptGetContext(argv[0], argc, argv, popt_options, POPT_CONTEXT_KEEP_FIRST);
165 while ((opt = poptGetNextOpt(pc)) != -1) {
166 switch (opt) {
167 default:
168 fprintf(stderr, "Invalid option %s: %s\n",
169 poptBadOption(pc, 0), poptStrerror(opt));
170 exit(1);
174 /* setup the remaining options for the main program to use */
175 extra_argv = poptGetArgs(pc);
176 if (extra_argv) {
177 extra_argv++;
178 while (extra_argv[extra_argc]) extra_argc++;
181 ev = event_context_init(NULL);
183 ctdb = ctdb_cmdline_client(ev, timeval_current_ofs(3, 0));
185 if (ctdb == NULL) {
186 printf("failed to connect to daemon\n");
187 exit(1);
190 /* attach to a specific database */
191 ctdb_db = ctdb_attach(ctdb, timeval_current_ofs(2, 0), "test.tdb",
192 false, 0);
193 if (!ctdb_db) {
194 printf("ctdb_attach failed - %s\n", ctdb_errstr(ctdb));
195 exit(1);
198 store_records(ctdb, ev);
200 return 0;