#include #include #include #include #include #include #include #include #include #include #define LOCALSOCK #ifdef LOCALSOCK #include #else #include #include #include #include #endif /*#define NOBLOCK*/ #ifdef NOBLOCK #include #endif #define SCHEDRT #ifdef SCHEDRT #include #endif # define timersub(a, b, result) \ do { \ (result)->tv_sec = (a)->tv_sec - (b)->tv_sec; \ (result)->tv_usec = (a)->tv_usec - (b)->tv_usec; \ if ((result)->tv_usec < 0) { \ --(result)->tv_sec; \ (result)->tv_usec += 1000000; \ } \ } while (0) #define MAX(a,b) \ ({ typeof (a) _a = (a); \ typeof (b) _b = (b); \ _a > _b ? _a : _b; }) #define MIN(a,b) \ ({ typeof (a) _a = (a); \ typeof (b) _b = (b); \ _a > _b ? _b : _a; }) int sock_init () { #ifdef LOCALSOCK #define UNIX_PATH_MAX 108 #define SOCKPTH "/tmp/alt-dbus" /* connection oriented local socket that preserve * order and packet boundaries */ int s; struct sockaddr_un sockstr; sockstr.sun_family = AF_UNIX; memcpy (sockstr.sun_path, SOCKPTH, UNIX_PATH_MAX); if (-1 == (s = socket (PF_UNIX, SOCK_SEQPACKET, 0))) { perror ("socket"); return 0; } #else /* tcp sockets */ #define HOST "127.0.0.1" #define PORT 50101 int s; struct hostent *he; if ((he = gethostbyname (HOST)) == NULL) { herror ("gethostbyname"); } struct sockaddr_in sockstr; sockstr.sin_family = AF_INET; sockstr.sin_port = htons (PORT); sockstr.sin_addr = *((struct in_addr *) he->h_addr); memset (sockstr.sin_zero, '\0', sizeof sockstr.sin_zero); if(-1 == (s = socket(PF_INET, SOCK_STREAM,0))) { perror("socket"); return 0; } #endif if (-1 == connect (s, (struct sockaddr *) &sockstr, sizeof (sockstr))) { perror ("connect"); return 0; } return s; } void test_int (int scket,uint32_t i) { static char recvbuff[1024]; char buffer[1 + sizeof (uint32_t)]; int ret; buffer[0] = 'i'; memcpy (buffer + 1, (uint32_t[]){htonl (i)}, sizeof (uint32_t)); #ifndef LOCALSOCK setsockopt (scket, IPPROTO_TCP, TCP_NODELAY, (int[]){1}, sizeof (int)); setsockopt (scket, IPPROTO_TCP, TCP_CORK, (int[]){1}, sizeof (int)); #endif ret = send (scket, &buffer, sizeof (buffer), #ifndef NOBLOCK 0 #else MSG_DONTWAIT #endif ); if(ret == -1 && (errno == EWOULDBLOCK || errno == EAGAIN)) printf("send would block!\n"); #ifndef LOCALSOCK setsockopt (scket, IPPROTO_TCP, TCP_NODELAY, (int[]){0}, sizeof (int)); setsockopt (scket, IPPROTO_TCP, TCP_CORK, (int[]){0}, sizeof (int)); #endif size_t n = recv (scket, recvbuff, sizeof recvbuff - 1, 0); recvbuff[n] = '\0'; } int main () { #ifdef SCHEDRT struct sched_param parms; parms.sched_priority = 5; sched_setscheduler(0,SCHED_FIFO,&parms); #endif int i; int scket = sock_init (); int j = 100000; uint64_t total = 0; uint64_t t = 0; uint64_t max = 0; uint64_t min = (uint64_t) -1; uint64_t timestamp; struct timeval starttime; gettimeofday(&starttime, NULL); FILE *mf = fopen("altdatafile.txt","w"); if(mf == NULL) return 0; for (i = 0; i < j; i++) { struct timeval tv1, tv2, diff,diff2; gettimeofday(&tv1, NULL); test_int(scket,i); gettimeofday(&tv2, NULL); timersub(&tv2, &tv1, &diff); t = diff.tv_usec + diff.tv_sec * 1000000; timersub(&tv2,&starttime,&diff2); timestamp = diff2.tv_usec + diff2.tv_sec * 1000000; fprintf(mf,"%llu.%06llu %llu.%06llu\n",timestamp/1000000,timestamp%1000000,t/1000000,t%1000000); max = MAX(max,t); min = MIN(min,t); total += t; } printf("j=%d,min: %llu.%06llu, max: %llu.%06llu, mean: %llu.%06llu (total: %llu)\n", j,min/1000000,min%1000000,max/1000000,max%1000000, (total /j ) / 1000000, (total / j) % 1000000, total); close (scket); return EXIT_SUCCESS; }