lowly sparc64 couldn't cope with 5 minutes
[oweals/gnunet.git] / src / datacache / perf_datacache.c
1 /*
2      This file is part of GNUnet.
3      (C) 2006, 2009 Christian Grothoff (and other contributing authors)
4
5      GNUnet is free software; you can redistribute it and/or modify
6      it under the terms of the GNU General Public License as published
7      by the Free Software Foundation; either version 2, or (at your
8      option) any later version.
9
10      GNUnet is distributed in the hope that it will be useful, but
11      WITHOUT ANY WARRANTY; without even the implied warranty of
12      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13      General Public License for more details.
14
15      You should have received a copy of the GNU General Public License
16      along with GNUnet; see the file COPYING.  If not, write to the
17      Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18      Boston, MA 02111-1307, USA.
19 */
20 /*
21  * @file datacache/perf_datacache.c
22  * @brief Performance evaluation for the datacache implementations.
23  * @author Nils Durner
24  */
25 #include "platform.h"
26 #include "gnunet_util_lib.h"
27 #include "gnunet_datacache_lib.h"
28
29 #define VERBOSE GNUNET_NO
30
31 #define ASSERT(x) do { if (! (x)) { printf("Error at %s:%d\n", __FILE__, __LINE__); goto FAILURE;} } while (0)
32
33 #define ITERATIONS 10000
34
35 static int ok;
36
37 static unsigned int found;
38
39 static int
40 checkIt (void *cls,
41          struct GNUNET_TIME_Absolute exp,
42          const GNUNET_HashCode * key,
43          uint32_t size, 
44          const char *data, 
45          uint32_t type)
46 {
47   if ( (size == sizeof (GNUNET_HashCode)) &&
48        (0 == memcmp (data, cls, size)) )
49     found++;
50   return GNUNET_OK;
51 }
52
53
54 static void
55 run (void *cls,
56      struct GNUNET_SCHEDULER_Handle *sched,
57      char *const *args,
58      const char *cfgfile,
59      const struct GNUNET_CONFIGURATION_Handle *cfg)
60 {
61   struct GNUNET_DATACACHE_Handle *h;
62   GNUNET_HashCode k;
63   GNUNET_HashCode n;
64   struct GNUNET_TIME_Absolute exp;
65   struct GNUNET_TIME_Absolute start;
66   unsigned int i;
67   
68   ok = 0;
69   h = GNUNET_DATACACHE_create (sched,
70                                cfg,
71                                "perfcache");
72
73   ASSERT (NULL != h);
74   exp = GNUNET_TIME_absolute_get ();
75   start = exp;
76   exp.value += 5 * 60 * 1000;
77   memset (&k, 0, sizeof (GNUNET_HashCode));
78   for (i = 0; i < ITERATIONS; i++)
79     {
80       if (0 == i % (ITERATIONS / 80))
81         fprintf (stderr, ".");
82       GNUNET_CRYPTO_hash (&k, sizeof (GNUNET_HashCode), &n);
83       ASSERT (GNUNET_OK == GNUNET_DATACACHE_put (h,
84                                                  &k,
85                                                  sizeof (GNUNET_HashCode),
86                                                  (const char *) &n,
87                                                  1+i%16,
88                                                  exp));
89       k = n;
90     }
91   fprintf (stderr, "\n");
92   fprintf (stdout, "Stored %u items in %llums\n",
93            ITERATIONS,
94            (unsigned long long) GNUNET_TIME_absolute_get_duration(start).value);
95   start = GNUNET_TIME_absolute_get ();
96   memset (&k, 0, sizeof (GNUNET_HashCode));
97   for (i = 0; i < ITERATIONS; i++)
98     {
99       if (0 == i % (ITERATIONS / 80))
100         fprintf (stderr, ".");
101       GNUNET_CRYPTO_hash (&k, sizeof (GNUNET_HashCode), &n);
102       GNUNET_DATACACHE_get (h, &k, 1+i%16,
103                             &checkIt, &n);
104       k = n;
105     }
106   fprintf (stderr, "\n");
107   fprintf (stdout, "Found %u/%u items in %llums (%u were deleted during storage processing)\n",
108            found, ITERATIONS,
109            (unsigned long long) GNUNET_TIME_absolute_get_duration(start).value,
110            ITERATIONS - found);
111            
112   GNUNET_DATACACHE_destroy (h);
113   ASSERT (ok == 0);
114   return;
115 FAILURE:
116   if (h != NULL)
117     GNUNET_DATACACHE_destroy (h);
118   ok = GNUNET_SYSERR;
119 }
120
121
122 static int
123 check ()
124 {
125   char *const argv[] = { "perf-datacache-api",
126     "-c",
127     "perf_datacache_data.conf",
128 #if VERBOSE
129     "-L", "DEBUG",
130 #endif
131     NULL
132   };
133   struct GNUNET_GETOPT_CommandLineOption options[] = {
134     GNUNET_GETOPT_OPTION_END
135   };
136   GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1,
137                       argv, "perf-datacache-api", "nohelp",
138                       options, &run, NULL);
139   if (ok != 0)
140     fprintf (stderr, "Missed some perfcases: %d\n", ok);
141   return ok;
142 }
143
144
145 int
146 main (int argc, char *argv[])
147 {
148   int ret;
149   
150   GNUNET_DISK_directory_remove ("/tmp/perf-gnunetd-datacache");
151   GNUNET_log_setup ("perf-datacache-api",
152 #if VERBOSE
153                     "DEBUG",
154 #else
155                     "WARNING",
156 #endif
157                     NULL);
158   ret = check ();
159
160   return ret;
161 }
162
163 /* end of perf_datacache.c */