Created
February 23, 2014 22:28
-
-
Save rishid/9178261 to your computer and use it in GitHub Desktop.
udp sendto micro benchmark
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <sys/socket.h> | |
#include <netinet/in.h> | |
#include <arpa/inet.h> | |
#include <stdio.h> | |
#include <sys/types.h> | |
#include <sys/socket.h> | |
#include <sys/time.h> | |
#include <unistd.h> | |
#include <stdlib.h> | |
#include <sys/ioctl.h> | |
#include <strings.h> | |
#include <time.h> | |
#include <unistd.h> | |
#include <fcntl.h> | |
struct timespec diff(struct timespec start, struct timespec end) | |
{ | |
struct timespec temp; | |
if ((end.tv_nsec - start.tv_nsec) < 0) | |
{ | |
temp.tv_sec = end.tv_sec - start.tv_sec - 1; | |
temp.tv_nsec = 1000000000 + end.tv_nsec - start.tv_nsec; | |
} | |
else | |
{ | |
temp.tv_sec = end.tv_sec - start.tv_sec; | |
temp.tv_nsec = end.tv_nsec - start.tv_nsec; | |
} | |
return temp; | |
} | |
int main(int argc, char** argv) | |
{ | |
struct sockaddr_in serv_addr; | |
int fd, slen = sizeof(serv_addr); | |
char buf[2048]; | |
//char buf[2048] = { [0 ... 2046] = 'A', [2047] = '\0' }; | |
if (argc != 3) | |
{ | |
fprintf(stderr, "usage: udp_send_bm <ip> <port>\n"); | |
exit(1); | |
} | |
fd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); | |
fcntl(fd, F_SETFL, O_NONBLOCK); | |
bzero(&serv_addr, sizeof(serv_addr)); | |
serv_addr.sin_family = AF_INET; | |
serv_addr.sin_port = htons(atoi(argv[2])); | |
inet_aton(argv[1], &serv_addr.sin_addr); | |
struct timespec ts1, ts2; | |
const int packet_count = 1000000; | |
printf("Loop iteration count: %d\n", packet_count); | |
for (int i = 0; i < 10; i++) | |
{ | |
int packet_size = 100 * (1 + i); | |
clock_gettime(CLOCK_MONOTONIC_RAW, &ts1); | |
for (int j = 0; j < packet_count; j++) | |
sendto(fd, buf, packet_size, 0, (struct sockaddr*)&serv_addr, sizeof(serv_addr)); | |
clock_gettime(CLOCK_MONOTONIC_RAW, &ts2); | |
struct timespec res = diff(ts1, ts2); | |
unsigned long it_time = (res.tv_sec * 1000000000L) + res.tv_nsec; | |
double tt = (double) (ts2.tv_nsec - ts1.tv_nsec) / 1000000000L + (double) (ts2.tv_sec - ts1.tv_sec); | |
printf("Packet Size %d // Time per sendto() %lu nanosec // Total time %f\n", | |
packet_size, it_time / packet_count, tt); | |
} | |
close(fd); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment