0

I have a packet sniffer (see below). To measure bandwidth I think I need to start a timer at the beginning of receving data. Record the number of bytes that has been transmitted and then calculate the average bandwidth. To measure time time to receive/send data I did:

int main() {
    //usual packet sniffer staff up untill while loop
    struct pcap_pkthdr header;
    const unsigned char *packet;
    char errbuf[PCAP_ERRBUF_SIZE];

    char *device;
    pcap_t *pcap_handle;

    int i;
    device = pcap_lookupdev(errbuf);
    if (device == NULL) perror("pcap_lookupdev failed");

    printf("Sniffing on device %s\n", device);

    pcap_handle = pcap_open_live(device, 4096, 1, 0, errbuf);
    if (pcap_handle == NULL) perror("pcap_open_live failed");   

    while (1) {
            //starting the timer
        double diff = 0.0;
        time_t start;
        time_t stop;
        char buff[128];
        time(&start);

            //receiving packet
        packet = pcap_next(pcap_handle, &header);

            //stopping the timer
        time(&stop);

             //measuring time of receiving data
        diff = difftime(stop, start);

        process_packet(packet, header.len, diff);
    }

}

diff turns out to always be 0.0000, which is probably wrong. Is my understanding correct, if yes, Is there any problems with code?

I also tries using milliseconds:

float diff;
clock_t start;
clock_t stop;
char buff[128];
start = clock();   

packet = pcap_next(pcap_handle, &header);//just creates a pointer in no time

stop = clock();
diff = (((float)stop - (float)start) / 1000000.0F ) * 1000;  

The same output...

maximilliano
  • 163
  • 1
  • 2
  • 16

1 Answers1

0

Insufficient number of samples or too coarse a clock.

The number of packets between the start and stop are likely far too small. time_t typically only has a resolution of 1 second. clock_t has an implementation defined number of ticks per seconds of CLOCKS_PER_SEC. I've seen values of 18.2 or 100 or 1000 and others. It too, may be insufficient for 1 data packet.

Recommend increase the time of bytes transferred to be at least 10x the clock period. So if you are using time_t and running at 19,200 baud, then transfer 192,000 bytes.

For consistency, synchronizing the start time helps. Sample below works for clock_t, just scale accordingly.

// sync
time_t was;
time(&was);
time_t now;
do {
  time(&now);
} while (now == was);

// do test
do_test();  // about 10 seconds;

// results
time_t later;
time(&later);
time_t delta = late - now;
BitsPerDataByte = 1+8+1;
double TestedBaud = 1.0*DataBytesSent*BitsPerDataByte/delta;
chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256
  • I do not have control over number of bytes transferred... I have to calculate bandwidth of each packet received and calculate average bandwidth... – maximilliano Nov 29 '13 at 22:23
  • I used the following piece of code which works: http://stackoverflow.com/questions/8558625/how-to-get-the-current-time-in-milliseconds-in-c-programming .But the way I try do measure it is right you think? – maximilliano Nov 29 '13 at 22:34
  • @maximilliano Your post implies using `clock_t` fails, yet your comment implies it works. I do not think your approach is correct. You need to measure when a packet begins to arrive and when done. Not how long it took to call a routine which may already have the received packet waiting. Maybe a new paradigm: Note the time (clock_t) each time a packet is received and report the difference between two packets. BTW: use CLOCKS_PER_SEC, not 1000000.0F. – chux - Reinstate Monica Nov 30 '13 at 01:07
  • @maximilliano By average, do you mean a running average of the last N packets, the running average of the recent results or report the average once? – chux - Reinstate Monica Nov 30 '13 at 01:10
  • average over all results dynamically. So for the first packet it is just the bandwidth of the packet, for the second packet it is the average of first and second, etc. – maximilliano Dec 02 '13 at 19:50
  • @maximilliano Note the time before the very _first_ packet `t0`, then the time after each received packet `t`. `diff = (float) SumOfAllPackets/(t - t0)`. Watch for divide by 0. – chux - Reinstate Monica Dec 02 '13 at 20:22