@cryptotaxi247 / netdata-1 / commits / 3fb5cc583

added statsd-stress.c

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