Skip to content

Instantly share code, notes, and snippets.

@m-mizutani
Last active December 14, 2016 12:50
Show Gist options
  • Save m-mizutani/f7f178e9d8e3eb66b7f6c249d44a993f to your computer and use it in GitHub Desktop.
Save m-mizutani/f7f178e9d8e3eb66b7f6c249d44a993f to your computer and use it in GitHub Desktop.
#include <pcap.h>
#include <stdio.h>
#include <string.h>
int main(int argc, char* argv[]) {
if (argc != 2) {
fprintf(stderr, "usage) pcapperf <dev>\n");
return 1;
}
pcap_t *pd;
char ebuf[PCAP_ERRBUF_SIZE];
pd = pcap_open_live(argv[1], 0xffff, 1, 1, ebuf);
if (pd == NULL) {
fprintf(stderr, "pcap_open_live: %s\n", ebuf);
return 1;
}
int rc;
struct pcap_pkthdr *pkthdr;
const u_char* pkt;
struct timeval prev_tv, tv, dtv;
int pkt_count = 0;
int recv_size = 0;
gettimeofday(&prev_tv, NULL);
for (;;) {
rc = pcap_next_ex(pd, &pkthdr, &pkt);
if (rc != 1) {
continue;
}
pkt_count++;
recv_size += pkthdr->caplen;
gettimeofday(&tv, NULL);
timersub(&tv, &prev_tv, &dtv);
if (tv.tv_sec - prev_tv.tv_sec > 0) {
double c = (double)pkt_count;
double s = (double)recv_size;
double d = (double)dtv.tv_sec + ((double)dtv.tv_usec) / 1000000;
printf("%ld, %10.6lf pps,%10.6lf bps\n", tv.tv_sec, c / d, s / d);
pkt_count = 0;
recv_size = 0;
memcpy(&prev_tv, &tv, sizeof(prev_tv));
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment