statsd-stress.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /* SPDX-License-Identifier: GPL-3.0-or-later */
  2. #include <stdlib.h>
  3. #include <arpa/inet.h>
  4. #include <netinet/in.h>
  5. #include <stdio.h>
  6. #include <sys/types.h>
  7. #include <sys/socket.h>
  8. #include <unistd.h>
  9. #include <string.h>
  10. #include <time.h>
  11. #include <pthread.h>
  12. void diep(char *s)
  13. {
  14. perror(s);
  15. exit(1);
  16. }
  17. size_t run_threads = 1;
  18. size_t metrics = 1024;
  19. #define SERVER_IP "127.0.0.1"
  20. #define PORT 8125
  21. size_t myrand(size_t max) {
  22. size_t loops = max / RAND_MAX;
  23. size_t i;
  24. size_t ret = rand();
  25. for(i = 0; i < loops ;i++)
  26. ret += rand();
  27. return ret % max;
  28. }
  29. struct thread_data {
  30. size_t id;
  31. struct sockaddr_in *si_other;
  32. int slen;
  33. size_t counter;
  34. };
  35. static void *report_thread(void *__data) {
  36. struct thread_data *data = (struct thread_data *)__data;
  37. size_t last = 0;
  38. for (;;) {
  39. size_t i;
  40. size_t total = 0;
  41. for(i = 0; i < run_threads ;i++)
  42. total += data[i].counter;
  43. printf("%zu metrics/s\n", total-last);
  44. last = total;
  45. sleep(1);
  46. printf("\033[F\033[J");
  47. }
  48. return NULL;
  49. }
  50. char *types[] = {"g", "c", "m", "ms", "h", "s", NULL};
  51. // char *types[] = {"g", "c", "C", "h", "ms", NULL}; // brubeck compatible
  52. static void *spam_thread(void *__data) {
  53. struct thread_data *data = (struct thread_data *)__data;
  54. int s;
  55. char packet[1024];
  56. if ((s = socket(AF_INET, SOCK_DGRAM, 0))==-1)
  57. diep("socket");
  58. char **packets = malloc(sizeof(char *) * metrics);
  59. size_t i, *lengths = malloc(sizeof(size_t) * metrics);
  60. size_t t;
  61. for(i = 0, t = 0; i < metrics ;i++, t++) {
  62. if(!types[t]) t = 0;
  63. char *type = types[t];
  64. lengths[i] = sprintf(packet, "stress.%s.t%zu.m%zu:%zu|%s", type, data->id, i, myrand(metrics), type);
  65. packets[i] = strdup(packet);
  66. // printf("packet %zu, of length %zu: '%s'\n", i, lengths[i], packets[i]);
  67. }
  68. //printf("\n");
  69. for (;;) {
  70. for(i = 0; i < metrics ;i++) {
  71. if (sendto(s, packets[i], lengths[i], 0, (void *)data->si_other, data->slen) < 0) {
  72. printf("C ==> DROPPED\n");
  73. return NULL;
  74. }
  75. data->counter++;
  76. }
  77. }
  78. free(packets);
  79. free(lengths);
  80. close(s);
  81. return NULL;
  82. }
  83. int main(int argc, char *argv[])
  84. {
  85. if (argc != 5) {
  86. fprintf(stderr, "Usage: '%s THREADS METRICS IP PORT'\n", argv[0]);
  87. exit(-1);
  88. }
  89. run_threads = atoi(argv[1]);
  90. metrics = atoi(argv[2]);
  91. char *ip = argv[3];
  92. int port = atoi(argv[4]);
  93. struct thread_data data[run_threads];
  94. struct sockaddr_in si_other;
  95. pthread_t threads[run_threads], report;
  96. size_t i;
  97. srand(time(NULL));
  98. memset(&si_other, 0, sizeof(si_other));
  99. si_other.sin_family = AF_INET;
  100. si_other.sin_port = htons(port);
  101. if (inet_aton(ip, &si_other.sin_addr)==0) {
  102. fprintf(stderr, "inet_aton() of ip '%s' failed\n", ip);
  103. exit(1);
  104. }
  105. for (i = 0; i < run_threads; ++i) {
  106. data[i].id = i;
  107. data[i].si_other = &si_other;
  108. data[i].slen = sizeof(si_other);
  109. data[i].counter = 0;
  110. pthread_create(&threads[i], NULL, spam_thread, &data[i]);
  111. }
  112. printf("\n");
  113. printf("THREADS : %zu\n", run_threads);
  114. printf("METRICS : %zu\n", metrics);
  115. printf("DESTINATION : %s:%d\n", ip, port);
  116. printf("\n");
  117. pthread_create(&report, NULL, report_thread, &data);
  118. for (i =0; i < run_threads; ++i)
  119. pthread_join(threads[i], NULL);
  120. return 0;
  121. }