perf_namestore_api_zone_iteration.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  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_CRYPTO_EcdsaPrivateKey *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. if (NULL != privkey)
  97. {
  98. GNUNET_free (privkey);
  99. privkey = NULL;
  100. }
  101. }
  102. /**
  103. * End with timeout. As this is a benchmark, we do not
  104. * fail hard but return "skipped".
  105. */
  106. static void
  107. timeout (void *cls)
  108. {
  109. (void) cls;
  110. timeout_task = NULL;
  111. GNUNET_SCHEDULER_shutdown ();
  112. res = 77;
  113. }
  114. static struct GNUNET_GNSRECORD_Data *
  115. create_record (unsigned int count)
  116. {
  117. struct GNUNET_GNSRECORD_Data *rd;
  118. rd = GNUNET_malloc (count + sizeof (struct GNUNET_GNSRECORD_Data));
  119. rd->expiration_time = GNUNET_TIME_relative_to_absolute (GNUNET_TIME_UNIT_HOURS).abs_value_us;
  120. rd->record_type = TEST_RECORD_TYPE;
  121. rd->data_size = count;
  122. rd->data = (void *) &rd[1];
  123. rd->flags = 0;
  124. memset (&rd[1],
  125. 'a',
  126. count);
  127. return rd;
  128. }
  129. static void
  130. zone_end (void *cls)
  131. {
  132. struct GNUNET_TIME_Relative delay;
  133. zi = NULL;
  134. delay = GNUNET_TIME_absolute_get_duration (start);
  135. fprintf (stdout,
  136. "Iterating over %u records took %s\n",
  137. off,
  138. GNUNET_STRINGS_relative_time_to_string (delay,
  139. GNUNET_YES));
  140. if (BENCHMARK_SIZE == off)
  141. {
  142. res = 0;
  143. }
  144. else
  145. {
  146. GNUNET_break (0);
  147. res = 1;
  148. }
  149. GNUNET_SCHEDULER_shutdown ();
  150. }
  151. static void
  152. fail_cb (void *cls)
  153. {
  154. zi = NULL;
  155. res = 2;
  156. GNUNET_break (0);
  157. GNUNET_SCHEDULER_shutdown ();
  158. }
  159. static void
  160. zone_proc (void *cls,
  161. const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone,
  162. const char *label,
  163. unsigned int rd_count,
  164. const struct GNUNET_GNSRECORD_Data *rd)
  165. {
  166. struct GNUNET_GNSRECORD_Data *wrd;
  167. unsigned int xoff;
  168. GNUNET_assert (NULL != zone);
  169. if (1 != sscanf (label,
  170. "l%u",
  171. &xoff))
  172. {
  173. res = 3;
  174. GNUNET_break (0);
  175. GNUNET_SCHEDULER_shutdown ();
  176. return;
  177. }
  178. if ( (xoff > BENCHMARK_SIZE) ||
  179. (0 != (seen[xoff / 8] & (1U << (xoff % 8)))) )
  180. {
  181. res = 3;
  182. GNUNET_break (0);
  183. GNUNET_SCHEDULER_shutdown ();
  184. return;
  185. }
  186. seen[xoff / 8] |= (1U << (xoff % 8));
  187. wrd = create_record (xoff % MAX_REC_SIZE);
  188. if ( (rd->record_type != wrd->record_type) ||
  189. (rd->data_size != wrd->data_size) ||
  190. (rd->flags != wrd->flags) )
  191. {
  192. res = 4;
  193. GNUNET_break (0);
  194. GNUNET_SCHEDULER_shutdown ();
  195. GNUNET_free (wrd);
  196. return;
  197. }
  198. if (0 != memcmp (rd->data,
  199. wrd->data,
  200. wrd->data_size))
  201. {
  202. res = 4;
  203. GNUNET_break (0);
  204. GNUNET_SCHEDULER_shutdown ();
  205. GNUNET_free (wrd);
  206. return;
  207. }
  208. GNUNET_free (wrd);
  209. if (0 != memcmp (zone,
  210. privkey,
  211. sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey)))
  212. {
  213. res = 5;
  214. GNUNET_break (0);
  215. GNUNET_SCHEDULER_shutdown ();
  216. return;
  217. }
  218. off++;
  219. left_until_next--;
  220. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  221. "Obtained record %u, expecting %u more until asking for mor explicitly\n",
  222. off,
  223. left_until_next);
  224. if (0 == left_until_next)
  225. {
  226. left_until_next = BLOCK_SIZE;
  227. GNUNET_NAMESTORE_zone_iterator_next (zi,
  228. left_until_next);
  229. }
  230. }
  231. static void
  232. publish_record (void *cls);
  233. static void
  234. put_cont (void *cls,
  235. int32_t success,
  236. const char *emsg)
  237. {
  238. (void) cls;
  239. qe = NULL;
  240. if (GNUNET_OK != success)
  241. {
  242. GNUNET_break (0);
  243. GNUNET_SCHEDULER_shutdown ();
  244. return;
  245. }
  246. t = GNUNET_SCHEDULER_add_now (&publish_record,
  247. NULL);
  248. }
  249. static void
  250. publish_record (void *cls)
  251. {
  252. struct GNUNET_GNSRECORD_Data *rd;
  253. char *label;
  254. (void) cls;
  255. t = NULL;
  256. if (BENCHMARK_SIZE == off)
  257. {
  258. struct GNUNET_TIME_Relative delay;
  259. delay = GNUNET_TIME_absolute_get_duration (start);
  260. fprintf (stdout,
  261. "Inserting %u records took %s\n",
  262. off,
  263. GNUNET_STRINGS_relative_time_to_string (delay,
  264. GNUNET_YES));
  265. start = GNUNET_TIME_absolute_get ();
  266. off = 0;
  267. left_until_next = 1;
  268. zi = GNUNET_NAMESTORE_zone_iteration_start (nsh,
  269. NULL,
  270. &fail_cb,
  271. NULL,
  272. &zone_proc,
  273. NULL,
  274. &zone_end,
  275. NULL);
  276. GNUNET_assert (NULL != zi);
  277. return;
  278. }
  279. rd = create_record ((++off) % MAX_REC_SIZE);
  280. GNUNET_asprintf (&label,
  281. "l%u",
  282. off);
  283. qe = GNUNET_NAMESTORE_records_store (nsh,
  284. privkey,
  285. label,
  286. 1, rd,
  287. &put_cont,
  288. NULL);
  289. GNUNET_free (label);
  290. GNUNET_free (rd);
  291. }
  292. static void
  293. run (void *cls,
  294. const struct GNUNET_CONFIGURATION_Handle *cfg,
  295. struct GNUNET_TESTING_Peer *peer)
  296. {
  297. GNUNET_SCHEDULER_add_shutdown (&end,
  298. NULL);
  299. timeout_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT,
  300. &timeout,
  301. NULL);
  302. nsh = GNUNET_NAMESTORE_connect (cfg);
  303. GNUNET_assert (NULL != nsh);
  304. privkey = GNUNET_CRYPTO_ecdsa_key_create ();
  305. GNUNET_assert (NULL != privkey);
  306. start = GNUNET_TIME_absolute_get ();
  307. t = GNUNET_SCHEDULER_add_now (&publish_record,
  308. NULL);
  309. }
  310. #include "test_common.c"
  311. int
  312. main (int argc,
  313. char *argv[])
  314. {
  315. const char *plugin_name;
  316. char *cfg_name;
  317. SETUP_CFG (plugin_name, cfg_name);
  318. res = 1;
  319. if (0 !=
  320. GNUNET_TESTING_peer_run ("perf-namestore-api-zone-iteration",
  321. cfg_name,
  322. &run,
  323. NULL))
  324. {
  325. res = 1;
  326. }
  327. GNUNET_DISK_purge_cfg_dir (cfg_name,
  328. "GNUNET_TEST_HOME");
  329. GNUNET_free (cfg_name);
  330. return res;
  331. }
  332. /* end of perf_namestore_api_zone_iteration.c */