]> git.ozlabs.org Git - ccan/blob - ccan/tal/benchmark/speed.c
tal: fix compilation of speed benchmark.
[ccan] / ccan / tal / benchmark / speed.c
1 /*
2    Taken from samba/lib/talloc/testsuite.c: Unix SMB/CIFS implementation.
3
4    local testing of talloc routines.
5
6    Copyright (C) Andrew Tridgell 2004
7
8      ** NOTE! The following LGPL license applies to the talloc
9      ** library. This does NOT imply that all of Samba is released
10      ** under the LGPL
11
12    This library is free software; you can redistribute it and/or
13    modify it under the terms of the GNU Lesser General Public
14    License as published by the Free Software Foundation; either
15    version 3 of the License, or (at your option) any later version.
16
17    This library is distributed in the hope that it will be useful,
18    but WITHOUT ANY WARRANTY; without even the implied warranty of
19    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
20    Lesser General Public License for more details.
21
22    You should have received a copy of the GNU Lesser General Public
23    License along with this library; if not, see <http://www.gnu.org/licenses/>.
24 */
25 #include <ccan/talloc/talloc.h>
26 #include <ccan/tal/tal.h>
27 #include <ccan/tal/str/str.h>
28 #include <ccan/time/time.h>
29 #include <ccan/err/err.h>
30 #include <string.h>
31
32 #define LOOPS 1024
33
34 int main(int argc, char *argv[])
35 {
36         void *ctx;
37         unsigned count;
38         int i, j;
39         struct timespec tv;
40         void *p1, *p2[100], *p3[100];
41         bool run_talloc = true, run_tal = true, run_malloc = true;
42
43         if (argv[1]) {
44                 if (strcmp(argv[1], "--talloc") == 0)
45                         run_tal = run_malloc = false;
46                 else if (strcmp(argv[1], "--tal") == 0)
47                         run_talloc = run_malloc = false;
48                 else if (strcmp(argv[1], "--malloc") == 0)
49                         run_talloc = run_tal = false;
50                 else
51                         errx(1, "Bad flag %s", argv[1]);
52         }
53
54         if (!run_talloc)
55                 goto after_talloc;
56
57         ctx = talloc_new(NULL);
58         tv = time_now();
59         count = 0;
60         do {
61                 for (i=0;i<LOOPS;i++) {
62                         p1 = talloc_size(ctx, LOOPS % 128);
63                         for (j = 0; j < 100; j++) {
64                                 p2[j] = talloc_strdup(p1, "foo bar");
65                                 p3[j] = talloc_size(p1, 300);
66                         }
67                         talloc_free(p1);
68                 }
69                 count += (1 + 200) * LOOPS;
70         } while (time_sub(time_now(), tv).tv_sec < 5);
71
72         fprintf(stderr, "talloc: %.0f ops/sec\n", count/5.0);
73
74         talloc_free(ctx);
75
76 after_talloc:
77         if (!run_tal)
78                 goto after_tal;
79
80         ctx = tal(NULL, char);
81         tv = time_now();
82         count = 0;
83         do {
84                 for (i=0;i<LOOPS;i++) {
85                         p1 = tal_arr(ctx, char, LOOPS % 128);
86                         for (j = 0; j < 100; j++) {
87                                 p2[j] = tal_strdup(p1, "foo bar");
88                                 p3[j] = tal_arr(p1, char, 300);
89                         }
90                         tal_free(p1);
91                 }
92                 count += (1 + 200) * LOOPS;
93         } while (time_sub(time_now(), tv).tv_sec < 5);
94         fprintf(stderr, "tal:    %.0f ops/sec\n", count/5.0);
95
96         tal_free(ctx);
97
98 after_tal:
99         if (!run_malloc)
100                 goto after_malloc;
101
102         tv = time_now();
103         count = 0;
104         do {
105                 for (i=0;i<LOOPS;i++) {
106                         p1 = malloc(LOOPS % 128);
107                         for (j = 0; j < 100; j++) {
108                                 p2[j] = strdup("foo bar");
109                                 p3[j] = malloc(300);
110                         }
111                         for (j = 0; j < 100; j++) {
112                                 free(p2[j]);
113                                 free(p3[j]);
114                         }
115                         free(p1);
116                 }
117                 count += (1 + 200) * LOOPS;
118         } while (time_sub(time_now(), tv).tv_sec < 5);
119         fprintf(stderr, "malloc: %.0f ops/sec\n", count/5.0);
120
121 after_malloc:
122         printf("success: speed\n");
123
124         return 0;
125 }