Last active
July 20, 2021 01:55
-
-
Save janeczku/dc0e98be4ec33d3537517ffe8b219021 to your computer and use it in GitHub Desktop.
A simple benchmark test for DNS lookups with libc getaddrinfo() function
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/types.h> | |
#include <sys/socket.h> | |
#include <netdb.h> | |
#include <stdio.h> | |
#include <stdlib.h> | |
#include <string.h> | |
#include <time.h> | |
#include <unistd.h> | |
#define NUM_LOOKUPS 1024 | |
#define MAX_RATE 10 | |
#define DOMAIN "godaddy.com" | |
int main() { | |
const char *hostname = DOMAIN; | |
struct addrinfo hints; | |
int err; | |
struct timespec start_time; | |
struct timespec end_time; | |
double latency, total; | |
int i, success; | |
/* | |
memset(&hints, 0, sizeof(hints)); | |
hints.ai_family = AF_UNSPEC; | |
hints.ai_socktype = 0; | |
*/ | |
fprintf(stderr, "Running benchmark\n"); | |
success = 0; | |
for (i = 0; i < NUM_LOOKUPS; i++) { | |
struct addrinfo *res; | |
clock_gettime(CLOCK_REALTIME, &start_time); | |
err = getaddrinfo (hostname, NULL, NULL, &res); | |
if (err) { | |
fprintf(stderr, "getaddrinfo error: %s\n", gai_strerror(err)); | |
} else { | |
clock_gettime(CLOCK_REALTIME, &end_time); | |
latency = (double)end_time.tv_sec + (double)end_time.tv_nsec / 1e9; | |
latency -= (double)start_time.tv_sec + (double)start_time.tv_nsec / 1e9; | |
total += latency; | |
success++; | |
} | |
freeaddrinfo(res); | |
usleep(1e6/MAX_RATE); | |
} | |
double avg = total / (double)success * 1000.0; | |
printf("getaddrinfo: %d/%d successful lookups\n", success, NUM_LOOKUPS); | |
printf("getaddrinfo: %lfms average per lookup\n", avg); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment