perf_datacache.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. /*
  2. This file is part of GNUnet.
  3. Copyright (C) 2006, 2009, 2010 GNUnet e.V.
  4. GNUnet is free software: you can redistribute it and/or modify it
  5. under the terms of the GNU Affero General Public License as published
  6. by the Free Software Foundation, either version 3 of the License,
  7. or (at your option) any later version.
  8. GNUnet is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Affero General Public License for more details.
  12. You should have received a copy of the GNU Affero General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. SPDX-License-Identifier: AGPL3.0-or-later
  15. */
  16. /*
  17. * @file datacache/perf_datacache.c
  18. * @brief Performance evaluation for the datacache implementations.
  19. * @author Nils Durner
  20. */
  21. #include "platform.h"
  22. #include "gnunet_util_lib.h"
  23. #include "gnunet_datacache_lib.h"
  24. #include "gnunet_testing_lib.h"
  25. #include <gauger.h>
  26. #define ASSERT(x) do { if (! (x)) { printf ("Error at %s:%d\n", __FILE__, \
  27. __LINE__); goto FAILURE; \
  28. } } while (0)
  29. #define ITERATIONS 10000
  30. static int ok;
  31. static unsigned int found;
  32. /**
  33. * Name of plugin under test.
  34. */
  35. static const char *plugin_name;
  36. static int
  37. checkIt (void *cls,
  38. const struct GNUNET_HashCode *key, size_t size, const char *data,
  39. enum GNUNET_BLOCK_Type type,
  40. struct GNUNET_TIME_Absolute exp,
  41. unsigned int path_len,
  42. const struct GNUNET_PeerIdentity *path)
  43. {
  44. if ((size == sizeof(struct GNUNET_HashCode)) && (0 == memcmp (data, cls,
  45. size)))
  46. found++;
  47. return GNUNET_OK;
  48. }
  49. static void
  50. run (void *cls, char *const *args, const char *cfgfile,
  51. const struct GNUNET_CONFIGURATION_Handle *cfg)
  52. {
  53. struct GNUNET_DATACACHE_Handle *h;
  54. struct GNUNET_HashCode k;
  55. struct GNUNET_HashCode n;
  56. struct GNUNET_TIME_Absolute exp;
  57. struct GNUNET_TIME_Absolute start;
  58. unsigned int i;
  59. char gstr[128];
  60. ok = 0;
  61. h = GNUNET_DATACACHE_create (cfg, "perfcache");
  62. if (h == NULL)
  63. {
  64. fprintf (stderr, "%s",
  65. "Failed to initialize datacache. Database likely not setup, skipping test.\n");
  66. ok = 77; /* mark test as skipped */
  67. return;
  68. }
  69. exp = GNUNET_TIME_relative_to_absolute (GNUNET_TIME_UNIT_HOURS);
  70. start = GNUNET_TIME_absolute_get ();
  71. memset (&k, 0, sizeof(struct GNUNET_HashCode));
  72. for (i = 0; i < ITERATIONS; i++)
  73. {
  74. if (0 == i % (ITERATIONS / 80))
  75. fprintf (stderr, "%s", ".");
  76. GNUNET_CRYPTO_hash (&k, sizeof(struct GNUNET_HashCode), &n);
  77. ASSERT (GNUNET_OK ==
  78. GNUNET_DATACACHE_put (h, &k, sizeof(struct GNUNET_HashCode),
  79. (const char *) &n, 1 + i % 16, exp,
  80. 0, NULL));
  81. k = n;
  82. }
  83. fprintf (stderr, "%s", "\n");
  84. fprintf (stdout, "Stored %u items in %s\n", ITERATIONS,
  85. GNUNET_STRINGS_relative_time_to_string (
  86. GNUNET_TIME_absolute_get_duration (start), GNUNET_YES));
  87. GNUNET_snprintf (gstr, sizeof(gstr), "DATACACHE-%s", plugin_name);
  88. GAUGER (gstr, "Time to PUT item in datacache",
  89. GNUNET_TIME_absolute_get_duration (start).rel_value_us / 1000LL
  90. / ITERATIONS,
  91. "ms/item");
  92. start = GNUNET_TIME_absolute_get ();
  93. memset (&k, 0, sizeof(struct GNUNET_HashCode));
  94. for (i = 0; i < ITERATIONS; i++)
  95. {
  96. if (0 == i % (ITERATIONS / 80))
  97. fprintf (stderr, "%s", ".");
  98. GNUNET_CRYPTO_hash (&k, sizeof(struct GNUNET_HashCode), &n);
  99. GNUNET_DATACACHE_get (h, &k, 1 + i % 16, &checkIt, &n);
  100. k = n;
  101. }
  102. fprintf (stderr, "%s", "\n");
  103. fprintf (stdout,
  104. "Found %u/%u items in %s (%u were deleted during storage processing)\n",
  105. found, ITERATIONS,
  106. GNUNET_STRINGS_relative_time_to_string (
  107. GNUNET_TIME_absolute_get_duration (start), GNUNET_YES),
  108. ITERATIONS - found);
  109. if (found > 0)
  110. GAUGER (gstr, "Time to GET item from datacache",
  111. GNUNET_TIME_absolute_get_duration (start).rel_value_us / 1000LL
  112. / found,
  113. "ms/item");
  114. GNUNET_DATACACHE_destroy (h);
  115. ASSERT (ok == 0);
  116. return;
  117. FAILURE:
  118. if (h != NULL)
  119. GNUNET_DATACACHE_destroy (h);
  120. ok = GNUNET_SYSERR;
  121. }
  122. int
  123. main (int argc, char *argv[])
  124. {
  125. char cfg_name[PATH_MAX];
  126. char *const xargv[] = {
  127. "perf-datacache",
  128. "-c",
  129. cfg_name,
  130. NULL
  131. };
  132. struct GNUNET_GETOPT_CommandLineOption options[] = {
  133. GNUNET_GETOPT_OPTION_END
  134. };
  135. GNUNET_log_setup ("perf-datacache",
  136. "WARNING",
  137. NULL);
  138. plugin_name = GNUNET_TESTING_get_testname_from_underscore (argv[0]);
  139. GNUNET_snprintf (cfg_name, sizeof(cfg_name), "perf_datacache_data_%s.conf",
  140. plugin_name);
  141. GNUNET_PROGRAM_run ((sizeof(xargv) / sizeof(char *)) - 1, xargv,
  142. "perf-datacache", "nohelp", options, &run, NULL);
  143. if ((0 != ok) && (77 != ok))
  144. fprintf (stderr, "Missed some perfcases: %d\n", ok);
  145. return ok;
  146. }
  147. /* end of perf_datacache.c */