2 * Compute baseline average and standard deviancy ping latency
6 * gcc -o pingtest -O2 -std=c99 pingtest.c
12 * And finally start the client
14 * ./pingtest client <number of pings; for instance, 10000>
16 * The server will terminate when the client does.
18 * Edsko de Vries <edsko@well-typed.com>
24#include <sys/socket.h>
34// Note: this is not consistent across CPUs (and hence across threads on multicore machines)
37 gettimeofday(&tp, NULL);
38 return ((double) tp.tv_sec) * 1e6 + (double) tp.tv_usec;
42 printf("starting server\n");
44 struct addrinfo hints, *res;
45 int error, server_socket;
47 memset(&hints, 0, sizeof(hints));
48 hints.ai_family = PF_INET;
49 hints.ai_socktype = SOCK_STREAM;
50 hints.ai_flags = AI_PASSIVE;
52 error = getaddrinfo(NULL, "8080", &hints, &res);
54 printf("server error: %s\n", gai_strerror(error));
58 server_socket = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
59 if(server_socket < 0) {
60 printf("server error: could not create socket\n");
65 if(setsockopt(server_socket, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int)) < 0) {
66 printf("server error: could not set socket options\n");
70 if(bind(server_socket, res->ai_addr, res->ai_addrlen) < 0) {
71 printf("server error: could not bind to socket\n");
75 listen(server_socket, 5);
78 struct sockaddr_storage client_addr;
80 client_socket = accept(server_socket, (struct sockaddr *)&client_addr, &addr_size);
84 ssize_t read = recv(client_socket, &buf, 8, 0);
85 // printf("server received '%s'\n", buf);
86 send(client_socket, buf, 8, 0);
93int client(int pings) {
94 printf("starting client\n");
96 struct addrinfo hints, *res;
97 int error, client_socket;
99 memset(&hints, 0, sizeof(hints));
100 hints.ai_family = PF_INET;
101 hints.ai_socktype = SOCK_STREAM;
103 error = getaddrinfo("127.0.0.1", "8080", &hints, &res);
105 printf("client error: %s\n", gai_strerror(error));
109 client_socket = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
110 if(client_socket < 0) {
111 printf("client error: could not create socket\n");
115 if(connect(client_socket, res->ai_addr, res->ai_addrlen) < 0) {
116 printf("client error: could not connect: %s\n", strerror(errno));
120 double* round_trip_latency = (double*) calloc(pings, sizeof(double));
122 for(int i = 0; i < pings; i++) {
123 double timestamp_before = timestamp();
125 send(client_socket, "ping123", 8, 0);
128 ssize_t read = recv(client_socket, &buf, 8, 0);
129 // printf("client received '%s'\n", buf);
131 double timestamp_after = timestamp();
132 round_trip_latency[i] = timestamp_after - timestamp_before;
135 printf("client did %d pings\n", pings);
137 // Write data to a file for plotting
138 FILE* data = fopen("round-trip-latency-c.data", "wt");
139 for(int i = 0; i < pings; i++) {
140 fprintf(data, "%i %lf\n", i, round_trip_latency[i]);
145 double round_trip_latency_average = 0;
146 for(int i = 0; i < pings; i++) {
147 round_trip_latency_average += round_trip_latency[i];
149 round_trip_latency_average /= (double) pings;
152 // compute standard deviation
153 double round_trip_latency_stddev = 0;
154 for(int i = 0; i < pings; i++) {
155 round_trip_latency_stddev += pow(round_trip_latency[i] - round_trip_latency_average, 2);
157 round_trip_latency_stddev = sqrt(round_trip_latency_stddev / (double) (pings - 1));
159 printf("average round-trip latency: %lf us (standard devitation %lf us)\n", round_trip_latency_average, round_trip_latency_stddev);
165int usage(int argc, char** argv) {
166 printf("usage: %s <number of pings>\n", argv[0]);
170int main(int argc, char** argv) {
172 return usage(argc, argv);
176 // TODO: we should wait until we know the server is ready
178 sscanf(argv[1], "%d", &pings);
179 return client(pings);
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list