perf_namestore_api_zone_iteration.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  1. /*
  2. This file is part of GNUnet.
  3. Copyright (C) 2013, 2018 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 namestore/perf_namestore_api_zone_iteration.c
  18. * @brief testcase for zone iteration functionality: iterate all zones
  19. * @author Christian Grothoff
  20. */
  21. #include "platform.h"
  22. #include "gnunet_namestore_service.h"
  23. #include "gnunet_testing_lib.h"
  24. #include "namestore.h"
  25. #include "gnunet_dnsparser_lib.h"
  26. #define TEST_RECORD_TYPE GNUNET_DNSPARSER_TYPE_TXT
  27. /**
  28. * A #BENCHMARK_SIZE of 1000 takes less than a minute on a reasonably
  29. * modern system, so 30 minutes should be OK even for very, very
  30. * slow systems.
  31. */
  32. #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 30)
  33. /**
  34. * The runtime of the benchmark is expected to be linear
  35. * for the iteration phase with a *good* database. The FLAT
  36. * database uses a quadratic retrieval algorithm,
  37. * hence it should be quadratic in the size.
  38. */
  39. #define BENCHMARK_SIZE 1000
  40. /**
  41. * Maximum record size
  42. */
  43. #define MAX_REC_SIZE 500
  44. /**
  45. * How big are the blocks we fetch? Note that the first block is
  46. * always just 1 record set per current API. Smaller block
  47. * sizes will make quadratic iteration-by-offset penalties
  48. * more pronounced.
  49. */
  50. #define BLOCK_SIZE 100
  51. static struct GNUNET_NAMESTORE_Handle *nsh;
  52. static struct GNUNET_SCHEDULER_Task *timeout_task;
  53. static struct GNUNET_SCHEDULER_Task *t;
  54. static struct GNUNET_IDENTITY_PrivateKey privkey;
  55. static struct GNUNET_NAMESTORE_ZoneIterator *zi;
  56. static struct GNUNET_NAMESTORE_QueueEntry *qe;
  57. static int res;
  58. static unsigned int off;
  59. static unsigned int left_until_next;
  60. static uint8_t seen[1 + BENCHMARK_SIZE / 8];
  61. static struct GNUNET_TIME_Absolute start;
  62. /**
  63. * Terminate everything
  64. *
  65. * @param cls NULL
  66. */
  67. static void
  68. end (void *cls)
  69. {
  70. (void) cls;
  71. if (NULL != qe)
  72. {
  73. GNUNET_NAMESTORE_cancel (qe);
  74. qe = NULL;
  75. }
  76. if (NULL != zi)
  77. {
  78. GNUNET_NAMESTORE_zone_iteration_stop (zi);
  79. zi = NULL;
  80. }
  81. if (NULL != nsh)
  82. {
  83. GNUNET_NAMESTORE_disconnect (nsh);
  84. nsh = NULL;
  85. }
  86. if (NULL != t)
  87. {
  88. GNUNET_SCHEDULER_cancel (t);
  89. t = NULL;
  90. }
  91. if (NULL != timeout_task)
  92. {
  93. GNUNET_SCHEDULER_cancel (timeout_task);
  94. timeout_task = NULL;
  95. }
  96. }
  97. /**
  98. * End with timeout. As this is a benchmark, we do not
  99. * fail hard but return "skipped".
  100. */
  101. static void
  102. timeout (void *cls)
  103. {
  104. (void) cls;
  105. timeout_task = NULL;
  106. GNUNET_SCHEDULER_shutdown ();
  107. res = 77;
  108. }
  109. static struct GNUNET_GNSRECORD_Data *
  110. create_record (unsigned int count)
  111. {
  112. struct GNUNET_GNSRECORD_Data *rd;
  113. rd = GNUNET_malloc (count + sizeof(struct GNUNET_GNSRECORD_Data));
  114. rd->expiration_time = GNUNET_TIME_relative_to_absolute (
  115. GNUNET_TIME_UNIT_HOURS).abs_value_us;
  116. rd->record_type = TEST_RECORD_TYPE;
  117. rd->data_size = count;
  118. rd->data = (void *) &rd[1];
  119. rd->flags = 0;
  120. memset (&rd[1],
  121. 'a',
  122. count);
  123. return rd;
  124. }
  125. static void
  126. zone_end (void *cls)
  127. {
  128. struct GNUNET_TIME_Relative delay;
  129. zi = NULL;
  130. delay = GNUNET_TIME_absolute_get_duration (start);
  131. fprintf (stdout,
  132. "Iterating over %u records took %s\n",
  133. off,
  134. GNUNET_STRINGS_relative_time_to_string (delay,
  135. GNUNET_YES));
  136. if (BENCHMARK_SIZE == off)
  137. {
  138. res = 0;
  139. }
  140. else
  141. {
  142. GNUNET_break (0);
  143. res = 1;
  144. }
  145. GNUNET_SCHEDULER_shutdown ();
  146. }
  147. static void
  148. fail_cb (void *cls)
  149. {
  150. zi = NULL;
  151. res = 2;
  152. GNUNET_break (0);
  153. GNUNET_SCHEDULER_shutdown ();
  154. }
  155. static void
  156. zone_proc (void *cls,
  157. const struct GNUNET_IDENTITY_PrivateKey *zone,
  158. const char *label,
  159. unsigned int rd_count,
  160. const struct GNUNET_GNSRECORD_Data *rd)
  161. {
  162. struct GNUNET_GNSRECORD_Data *wrd;
  163. unsigned int xoff;
  164. GNUNET_assert (NULL != zone);
  165. if (1 != sscanf (label,
  166. "l%u",
  167. &xoff))
  168. {
  169. res = 3;
  170. GNUNET_break (0);
  171. GNUNET_SCHEDULER_shutdown ();
  172. return;
  173. }
  174. if ((xoff > BENCHMARK_SIZE) ||
  175. (0 != (seen[xoff / 8] & (1U << (xoff % 8)))))
  176. {
  177. res = 3;
  178. GNUNET_break (0);
  179. GNUNET_SCHEDULER_shutdown ();
  180. return;
  181. }
  182. seen[xoff / 8] |= (1U << (xoff % 8));
  183. wrd = create_record (xoff % MAX_REC_SIZE);
  184. if ((rd->record_type != wrd->record_type) ||
  185. (rd->data_size != wrd->data_size) ||
  186. (rd->flags != wrd->flags))
  187. {
  188. res = 4;
  189. GNUNET_break (0);
  190. GNUNET_SCHEDULER_shutdown ();
  191. GNUNET_free (wrd);
  192. return;
  193. }
  194. if (0 != memcmp (rd->data,
  195. wrd->data,
  196. wrd->data_size))
  197. {
  198. res = 4;
  199. GNUNET_break (0);
  200. GNUNET_SCHEDULER_shutdown ();
  201. GNUNET_free (wrd);
  202. return;
  203. }
  204. GNUNET_free (wrd);
  205. if (0 != GNUNET_memcmp (zone,
  206. &privkey))
  207. {
  208. res = 5;
  209. GNUNET_break (0);
  210. GNUNET_SCHEDULER_shutdown ();
  211. return;
  212. }
  213. off++;
  214. left_until_next--;
  215. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  216. "Obtained record %u, expecting %u more until asking for more explicitly\n",
  217. off,
  218. left_until_next);
  219. if (0 == left_until_next)
  220. {
  221. left_until_next = BLOCK_SIZE;
  222. GNUNET_NAMESTORE_zone_iterator_next (zi,
  223. left_until_next);
  224. }
  225. }
  226. static void
  227. publish_record (void *cls);
  228. static void
  229. put_cont (void *cls,
  230. int32_t success,
  231. const char *emsg)
  232. {
  233. (void) cls;
  234. qe = NULL;
  235. if (GNUNET_OK != success)
  236. {
  237. GNUNET_break (0);
  238. GNUNET_SCHEDULER_shutdown ();
  239. return;
  240. }
  241. t = GNUNET_SCHEDULER_add_now (&publish_record,
  242. NULL);
  243. }
  244. static void
  245. publish_record (void *cls)
  246. {
  247. struct GNUNET_GNSRECORD_Data *rd;
  248. char *label;
  249. (void) cls;
  250. t = NULL;
  251. if (BENCHMARK_SIZE == off)
  252. {
  253. struct GNUNET_TIME_Relative delay;
  254. delay = GNUNET_TIME_absolute_get_duration (start);
  255. fprintf (stdout,
  256. "Inserting %u records took %s\n",
  257. off,
  258. GNUNET_STRINGS_relative_time_to_string (delay,
  259. GNUNET_YES));
  260. start = GNUNET_TIME_absolute_get ();
  261. off = 0;
  262. left_until_next = 1;
  263. zi = GNUNET_NAMESTORE_zone_iteration_start (nsh,
  264. NULL,
  265. &fail_cb,
  266. NULL,
  267. &zone_proc,
  268. NULL,
  269. &zone_end,
  270. NULL);
  271. GNUNET_assert (NULL != zi);
  272. return;
  273. }
  274. rd = create_record ((++off) % MAX_REC_SIZE);
  275. GNUNET_asprintf (&label,
  276. "l%u",
  277. off);
  278. qe = GNUNET_NAMESTORE_records_store (nsh,
  279. &privkey,
  280. label,
  281. 1, rd,
  282. &put_cont,
  283. NULL);
  284. GNUNET_free (label);
  285. GNUNET_free (rd);
  286. }
  287. static void
  288. run (void *cls,
  289. const struct GNUNET_CONFIGURATION_Handle *cfg,
  290. struct GNUNET_TESTING_Peer *peer)
  291. {
  292. GNUNET_SCHEDULER_add_shutdown (&end,
  293. NULL);
  294. timeout_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT,
  295. &timeout,
  296. NULL);
  297. nsh = GNUNET_NAMESTORE_connect (cfg);
  298. GNUNET_assert (NULL != nsh);
  299. privkey.type = htonl (GNUNET_GNSRECORD_TYPE_PKEY);
  300. GNUNET_CRYPTO_ecdsa_key_create (&privkey.ecdsa_key);
  301. start = GNUNET_TIME_absolute_get ();
  302. t = GNUNET_SCHEDULER_add_now (&publish_record,
  303. NULL);
  304. }
  305. #include "test_common.c"
  306. int
  307. main (int argc,
  308. char *argv[])
  309. {
  310. const char *plugin_name;
  311. char *cfg_name;
  312. SETUP_CFG (plugin_name, cfg_name);
  313. res = 1;
  314. if (0 !=
  315. GNUNET_TESTING_peer_run ("perf-namestore-api-zone-iteration",
  316. cfg_name,
  317. &run,
  318. NULL))
  319. {
  320. res = 1;
  321. }
  322. GNUNET_DISK_purge_cfg_dir (cfg_name,
  323. "GNUNET_TEST_HOME");
  324. GNUNET_free (cfg_name);
  325. return res;
  326. }
  327. /* end of perf_namestore_api_zone_iteration.c */