perf_peerinfo_api.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. /*
  2. This file is part of GNUnet.
  3. Copyright (C) 2004, 2009, 2010, 2017 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 peerinfo/perf_peerinfo_api.c
  18. * @brief testcase for peerinfo_api.c, hopefully hammer the peerinfo service,
  19. * this performance test adds up to 5000 peers with one address each and checks
  20. * over how many peers it can iterate before receiving a timeout after 5 seconds
  21. * @author Nathan Evans
  22. */
  23. #include "platform.h"
  24. #include "gnunet_hello_lib.h"
  25. #include "gnunet_util_lib.h"
  26. #include "gnunet_testing_lib.h"
  27. #include "gnunet_peerinfo_service.h"
  28. #include "peerinfo.h"
  29. #include <gauger.h>
  30. #define NUM_REQUESTS 5000
  31. static struct GNUNET_PEERINFO_IteratorContext *ic[NUM_REQUESTS];
  32. static struct GNUNET_PEERINFO_Handle *h;
  33. static unsigned int numpeers;
  34. static struct GNUNET_PeerIdentity pid;
  35. static struct GNUNET_SCHEDULER_Task *tt;
  36. static void
  37. do_shutdown (void *cls)
  38. {
  39. if (NULL != tt)
  40. {
  41. GNUNET_SCHEDULER_cancel (tt);
  42. tt = NULL;
  43. }
  44. for (unsigned int i = 0; i < NUM_REQUESTS; i++)
  45. if (NULL != ic[i])
  46. GNUNET_PEERINFO_iterate_cancel (ic[i]);
  47. GNUNET_PEERINFO_disconnect (h);
  48. h = NULL;
  49. }
  50. static void
  51. do_timeout (void *cls)
  52. {
  53. tt = NULL;
  54. GNUNET_SCHEDULER_shutdown ();
  55. }
  56. static int
  57. check_it (void *cls,
  58. const struct GNUNET_HELLO_Address *address,
  59. struct GNUNET_TIME_Absolute expiration)
  60. {
  61. return GNUNET_OK;
  62. }
  63. static ssize_t
  64. address_generator (void *cls, size_t max, void *buf)
  65. {
  66. size_t *agc = cls;
  67. ssize_t ret;
  68. char *caddress;
  69. struct GNUNET_HELLO_Address address;
  70. if (*agc == 0)
  71. return GNUNET_SYSERR; /* Done */
  72. GNUNET_asprintf (&caddress, "Address%d", *agc);
  73. address.peer = pid;
  74. address.address_length = strlen (caddress) + 1;
  75. address.address = caddress;
  76. address.transport_name = "peerinfotest";
  77. ret =
  78. GNUNET_HELLO_add_address (&address,
  79. GNUNET_TIME_relative_to_absolute
  80. (GNUNET_TIME_UNIT_HOURS), buf, max);
  81. GNUNET_free (caddress);
  82. *agc = 0;
  83. return ret;
  84. }
  85. static void
  86. add_peer (size_t i)
  87. {
  88. struct GNUNET_HELLO_Message *h2;
  89. memset (&pid, i, sizeof (pid));
  90. h2 = GNUNET_HELLO_create (&pid.public_key,
  91. &address_generator,
  92. &i,
  93. GNUNET_NO);
  94. GNUNET_PEERINFO_add_peer (h, h2, NULL, NULL);
  95. GNUNET_free (h2);
  96. }
  97. static void
  98. process (void *cls,
  99. const struct GNUNET_PeerIdentity *peer,
  100. const struct GNUNET_HELLO_Message *hello,
  101. const char *err_msg)
  102. {
  103. struct GNUNET_PEERINFO_IteratorContext **icp = cls;
  104. if (NULL == peer)
  105. {
  106. *icp = NULL;
  107. return;
  108. }
  109. numpeers++;
  110. if (0 && (NULL != hello) )
  111. GNUNET_HELLO_iterate_addresses (hello,
  112. GNUNET_NO,
  113. &check_it,
  114. NULL);
  115. }
  116. static void
  117. run (void *cls,
  118. const struct GNUNET_CONFIGURATION_Handle *cfg,
  119. struct GNUNET_TESTING_Peer *peer)
  120. {
  121. h = GNUNET_PEERINFO_connect (cfg);
  122. GNUNET_assert (h != NULL);
  123. for (unsigned int i = 0; i < NUM_REQUESTS; i++)
  124. {
  125. add_peer (i);
  126. ic[i] = GNUNET_PEERINFO_iterate (h,
  127. GNUNET_YES,
  128. NULL,
  129. &process,
  130. &ic[i]);
  131. }
  132. tt = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS,
  133. 5),
  134. &do_timeout,
  135. NULL);
  136. GNUNET_SCHEDULER_add_shutdown (&do_shutdown,
  137. NULL);
  138. }
  139. int
  140. main (int argc,
  141. char *argv[])
  142. {
  143. if (0 != GNUNET_TESTING_service_run ("perf-gnunet-peerinfo",
  144. "peerinfo",
  145. "test_peerinfo_api_data.conf",
  146. &run, NULL))
  147. return 1;
  148. FPRINTF (stderr,
  149. "Received %u/%u calls before timeout\n",
  150. numpeers,
  151. NUM_REQUESTS * NUM_REQUESTS / 2);
  152. GAUGER ("PEERINFO",
  153. "Peerinfo lookups",
  154. numpeers / 5,
  155. "peers/s");
  156. return 0;
  157. }
  158. /* end of perf_peerinfo_api.c */