Skip to content

Instantly share code, notes, and snippets.

@whs
Created September 5, 2017 01:53
Show Gist options
  • Select an option

  • Save whs/4c71e2f9e24897be8d9a3af21d203c30 to your computer and use it in GitHub Desktop.

Select an option

Save whs/4c71e2f9e24897be8d9a3af21d203c30 to your computer and use it in GitHub Desktop.
#define _GNU_SOURCE
#define _DARWIN_C_SOURCE
#include <stdio.h>
#include <stdint.h>
#include <limits.h>
#include <time.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
struct timespec time_start;
uint64_t times = 0;
uint64_t errors = 0;
uint64_t min_time = INT_MAX;
uint64_t max_time = 0;
uint64_t diff_time(struct timespec *start, struct timespec *end){
uint64_t start_ts = (start->tv_sec * 1000000) + (start->tv_nsec / 1000);
uint64_t end_ts = (end->tv_sec * 1000000) + (end->tv_nsec / 1000);
return end_ts - start_ts;
}
int main(){
struct addrinfo *res;
struct timespec current_time;
struct timespec round_start;
struct timespec round_end;
time_t last_printed_time = 0;
clock_gettime(CLOCK_MONOTONIC_RAW, &time_start);
while(1){
clock_gettime(CLOCK_MONOTONIC_RAW, &round_start);
int result = getaddrinfo("www.wongnai.com", NULL, NULL, &res);
clock_gettime(CLOCK_MONOTONIC_RAW, &round_end);
times++;
if(result != 0){
errors++;
}
uint64_t time_diff = diff_time(&round_start, &round_end);
if(time_diff < min_time){
min_time = time_diff;
}
if(time_diff > max_time){
max_time = time_diff;
}
clock_gettime(CLOCK_MONOTONIC_RAW, &current_time);
time_t running_time = current_time.tv_sec - time_start.tv_sec;
if(running_time != last_printed_time && running_time % 5 == 0){
last_printed_time = running_time;
printf("Statistics:\n");
printf("Running time: %ld\n", running_time);
printf("Qps: %f\n", (float) times / (float) running_time);
printf("Done: %"PRIu64", Success: %"PRIu64", Failed: %"PRIu64"\n", times, times-errors, errors);
printf("Max query time: %"PRIu64"μs, min %"PRIu64"μs\n\n", max_time, min_time);
fflush(stdout);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment