Socket creation/write/read time

J

Johann Schlamp

Hi folks,
I have to calculate the time needed for specific parts of my program.
Here is some sample code:


#include <stdio.h>
#include <string.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <time.h>

int main(int argc, char *argv[])
{
int timerUsed = CLOCK_THREAD_CPUTIME_ID;
double timeStart, timeEnd;
struct timespec realtime_timeStart, realtime_timeEnd;

clock_gettime (timerUsed, &realtime_timeStart);
// the time consuming part
clock_gettime (timerUsed, &realtime_timeEnd);

timeStart = ((realtime_timeStart.tv_sec) * 1000000000) +
(realtime_timeStart.tv_nsec);
timeEnd = ((realtime_timeEnd.tv_sec) * 1000000000) +
(realtime_timeEnd.tv_nsec);

printf("OVERALL TIME: %g microseconds\n", (timeEnd-timeStart)/1000);
return 0;
}


This code works fine as far as I can tell, but not for the following
"time consuming part":


char* msg = "SSH-2.0-test\r\n";
char* target = "localhost";
int portno = 22;

char buffer[256];
bzero(buffer, sizeof(buffer));

serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htons(portno);
inet_aton(target, &server);
serv_addr.sin_addr = server;

socketfd = socket(AF_INET, SOCK_STREAM, 0);

connect(socketfd, (struct sockaddr*) &serv_addr, sizeof(serv_addr));
write(socketfd, msg, strlen(msg));
read(socketfd,buffer,255);
close(socketfd);


The overall time results for opening a socket, writing, reading and
closing it seem to be random. Sometimes there are valid times in the
range of 300-500 microseconds, but then again I get even times less than
5 nanoseconds which definitely can't be right, can it?

I tried to break it down to as less statements as possible being in the
measured block, and I checked all return values as well as the contents
of the read buffer. I also used tcpdump to verify the correct sending of
packets and checked my sshd-logs for the corresponding connection try
entries. Everything seems to be just right, except for the random timing
results.
Compiling with -O0 or other flags preventing code optimization doesn't
help either.

Maybe someone around here has more experience in C socket programming
than I do, and could give me some advise or point me to the right
direction... Any help would be appreciated!


Best regards,
Johann
 
J

Johann Schlamp

Thank you Anthony. I just tried your code and it seems to work fine,
also for socket creation. :)

I will use this code from now on. But if anyone has a clue why my code
posted earlier doesn't work - I am still interested.


Best regards,
Johann



Anthony said:
Johann said:
Hi folks,
I have to calculate the time needed for specific parts of my program.
Here is some sample code:


#include <stdio.h>
#include <string.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <time.h>

int main(int argc, char *argv[])
{
int timerUsed = CLOCK_THREAD_CPUTIME_ID;
double timeStart, timeEnd;
struct timespec realtime_timeStart, realtime_timeEnd;

clock_gettime (timerUsed, &realtime_timeStart);
// the time consuming part
clock_gettime (timerUsed, &realtime_timeEnd);

timeStart = ((realtime_timeStart.tv_sec) * 1000000000) +
(realtime_timeStart.tv_nsec);
timeEnd = ((realtime_timeEnd.tv_sec) * 1000000000) +
(realtime_timeEnd.tv_nsec);

printf("OVERALL TIME: %g microseconds\n", (timeEnd-timeStart)/1000);
return 0;
}


This code works fine as far as I can tell, but not for the following
"time consuming part":


char* msg = "SSH-2.0-test\r\n";
char* target = "localhost";
int portno = 22;

char buffer[256];
bzero(buffer, sizeof(buffer));

serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htons(portno);
inet_aton(target, &server);
serv_addr.sin_addr = server;

socketfd = socket(AF_INET, SOCK_STREAM, 0);

connect(socketfd, (struct sockaddr*) &serv_addr, sizeof(serv_addr));
write(socketfd, msg, strlen(msg));
read(socketfd,buffer,255);
close(socketfd);


The overall time results for opening a socket, writing, reading and
closing it seem to be random. Sometimes there are valid times in the
range of 300-500 microseconds, but then again I get even times less
than 5 nanoseconds which definitely can't be right, can it?

I tried to break it down to as less statements as possible being in
the measured block, and I checked all return values as well as the
contents of the read buffer. I also used tcpdump to verify the
correct sending of packets and checked my sshd-logs for the
corresponding connection try entries. Everything seems to be just
right, except for the random timing results.
Compiling with -O0 or other flags preventing code optimization doesn't
help either.

Maybe someone around here has more experience in C socket programming
than I do, and could give me some advise or point me to the right
direction... Any help would be appreciated!

This is how I measure time to the uS in Linux. Works real good for me.

struct timeval tv1, tv2, tvdiff;

gettimeofday(&tv1, NULL); /* Get start time */
...... do some stuff that takes time
gettimeofday(&tv2, NULL; /* Get ending time */
/* compute difference */
tvdiff.tv_sec = tv2.tv_sec - tv1.tv_sec;
tvdiff.tv_usec = tv2.tv_usec - tv1.tv_usec;
if(tvdiff.tv_usec < 0){
tvdiff.tv_usec += 1000000;
tvdiff.tv_sec--;
}
 
B

Ben Bacarisse

Johann Schlamp said:
Thank you Anthony. I just tried your code and it seems to work fine,
also for socket creation. :)

Top posting not favoured here.
I will use this code from now on. But if anyone has a clue why my code
posted earlier doesn't work - I am still interested.

This is a problem (and it is a plain C problem). tv_sec is probably
an integer type (long int on my system) so the multiplication is done
as an integer multiply. Depending on system details, you will get
junk results after only 2 and bit seconds. Try 1e9 instead.
 
K

Keith Thompson

Johann Schlamp said:
I have to calculate the time needed for specific parts of my program.
Here is some sample code:


#include <stdio.h>
#include <string.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <time.h> [snip]
Maybe someone around here has more experience in C socket programming
than I do, and could give me some advise or point me to the right
direction... Any help would be appreciated!

Sockets are not part of the C language; they're an extension provided
by some systems. Try asking in comp.unix.programmer.
 
J

Johann Schlamp

Ben said:
Top posting not favoured here.
Ok.


This is a problem (and it is a plain C problem). tv_sec is probably
an integer type (long int on my system) so the multiplication is done
as an integer multiply. Depending on system details, you will get
junk results after only 2 and bit seconds. Try 1e9 instead.

Thanks for your answer, I think I understand the problem now. :)


Best regards,
Johann
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top