recover: finish pending trans3 commits when a recovery is finished.
[sahlberg/ctdb.git] / tests / src / rb_perftest.c
1 /* 
2    simple rb vs dlist benchmark
3
4    Copyright (C) Ronnie Sahlberg 2007
5
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 3 of the License, or
9    (at your option) any later version.
10    
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15    
16    You should have received a copy of the GNU General Public License
17    along with this program; if not, see <http://www.gnu.org/licenses/>.
18 */
19
20 #include "includes.h"
21 #include "lib/events/events.h"
22 #include "lib/util/dlinklist.h"
23 #include "system/filesys.h"
24 #include "popt.h"
25 #include "cmdline.h"
26
27 #include <sys/time.h>
28 #include <time.h>
29 #include "common/rb_tree.h"
30
31 static struct timeval tp1,tp2;
32
33 static void start_timer(void)
34 {
35         gettimeofday(&tp1,NULL);
36 }
37
38 static double end_timer(void)
39 {
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));
43 }
44
45
46 static int num_records = 1000;
47
48
49 struct list_node {
50         struct list_node *prev, *next;
51 };
52
53 /*
54   main program
55 */
56 int main(int argc, const char *argv[])
57 {
58         struct poptOption popt_options[] = {
59                 POPT_AUTOHELP
60                 POPT_CTDB_CMDLINE
61                 { "num-records", 'r', POPT_ARG_INT, &num_records, 0, "num_records", "integer" },
62                 POPT_TABLEEND
63         };
64         int opt;
65         const char **extra_argv;
66         int extra_argc = 0;
67         int ret;
68         poptContext pc;
69         struct event_context *ev;
70         double elapsed;
71         int i;
72         trbt_tree_t *tree;
73         struct list_node *list, *list_new, *list_head=NULL;
74
75         pc = poptGetContext(argv[0], argc, argv, popt_options, POPT_CONTEXT_KEEP_FIRST);
76
77         while ((opt = poptGetNextOpt(pc)) != -1) {
78                 switch (opt) {
79                 default:
80                         fprintf(stderr, "Invalid option %s: %s\n", 
81                                 poptBadOption(pc, 0), poptStrerror(opt));
82                         exit(1);
83                 }
84         }
85
86         /* setup the remaining options for the main program to use */
87         extra_argv = poptGetArgs(pc);
88         if (extra_argv) {
89                 extra_argv++;
90                 while (extra_argv[extra_argc]) extra_argc++;
91         }
92
93         ev = event_context_init(NULL);
94
95
96         printf("testing tree insert for %d records\n", num_records);
97         tree = trbt_create(NULL);
98         start_timer();
99         for (i=0;i<num_records;i++) {
100                 trbt_insert32(tree, i, NULL);
101         }
102         elapsed=end_timer();
103         printf("%f seconds\n",(float)elapsed);
104
105
106         printf("testing dlist (worst case) add to tail for %d records\n", num_records);
107         list_new=talloc(NULL, struct list_node);
108         DLIST_ADD(list_head, list_new);
109         start_timer();
110         for (i=0;i<num_records;i++) {
111                 for(list=list_head;list->next;list=list->next) {
112                         /* the events code does a timeval_compare */
113                         timeval_compare(&tp1, &tp2);
114                 }
115
116                 list_new=talloc(NULL, struct list_node);
117                 DLIST_ADD_AFTER(list_head, list_new, list);
118         }
119         elapsed=end_timer();
120         printf("%f seconds\n",(float)elapsed);
121
122         return 0;
123 }