📄 bench.c
字号:
/* bench.c,v 1.3 2000/09/22 19:30:37 brunsch Exp */
/**************************************************************************
* *
* Copyright (C) 1995 Silicon Graphics, Inc. *
* *
* These coded instructions, statements, and computer programs were *
* developed by SGI for public use. If any changes are made to this code*
* please try to get the changes back to the author. Feel free to make *
* modifications and changes to the code and release it. *
* *
**************************************************************************/
/* FUZZ: disable check_for_math_include */
#include <stdio.h>
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <math.h>
#include <limits.h>
#include <float.h>
#ifndef WIN32
#include <unistd.h>
#include <sys/time.h>
#include <sys/uio.h>
#include <sys/param.h>
#include <netdb.h>
#else
#include <windows.h>
#include <winsock.h>
#endif /* WIN32 */
#include "sysdep.h"
#include "bench.h"
/* allocate memory and exit if out of memory */
void *mymalloc(size_t size) {
void *ptr;
ptr = malloc(size);
if (ptr == NULL)
errexit("Call to malloc() failed\n");
return ptr;
}
/*
* Receive n bytes from a socket
*/
int
recvdata(SOCKET sock, char *ptr, int nbytes) {
int nleft, nread;
nleft = nbytes;
while (nleft > 0)
{
D_PRINTF( "In recvdata(%d, %d)\n", sock, nleft );
nread = NETREAD(sock, ptr, nleft);
D_PRINTF( "NETREAD() returned %d\n", nread );
if (BADSOCKET(nread) || nread == 0)
{
/* return error value NETWRITE */
D_PRINTF( "Error in recvdata(): %s\n",neterrstr() );
return(nread);
}
D_PRINTF( "NETREAD() data: \"%.*s\"\n", nread, ptr);
nleft -= nread;
ptr += nread;
} /* end while */
/* return >= 0 */
return(nbytes - nleft);
} /* end recvdata */
/*
* Send n bytes to a socket
*/
int
senddata(SOCKET sock, char *ptr, int nbytes) {
int nleft, nwritten;
D_PRINTF( "In senddata(%d, \"%.*s\", %d)\n", sock, nbytes, ptr, nbytes );
nleft = nbytes;
while (nleft > 0)
{
nwritten = NETWRITE(sock, ptr, nleft);
D_PRINTF( "senddata() returned %d\n", nwritten );
if (BADSOCKET(nwritten))
{
/* return error value from NETWRITE */
D_PRINTF( "Error in senddata(): %s\n", neterrstr() );
return(nwritten);
}
nleft -= nwritten;
ptr += nwritten;
}
return(nbytes - nleft);
} /* end senddata */
/* GENERAL NOTE: the conversion routines that follow pass their results
* back in a static arrays. A second call to the same routine overwrites
* the previous buffer value for that routine. If you want to save the
* value in the buffer copy it to another variable.
*/
char *
timeval_to_text(const struct timeval *the_timeval) {
/*
* given a timeval (seconds and microseconds), put the text
* "seconds.microseconds" into timeval_as_text
*/
THREAD static char timeval_as_text[SIZEOF_TIMEVALTEXT+1];
int seconds, microseconds;
int returnval = 0;
seconds = the_timeval->tv_sec;
microseconds = the_timeval->tv_usec;
returnval = sprintf(timeval_as_text,
"%10d.%6.6d\t", seconds, microseconds);
return timeval_as_text;
}
char *
double_to_text(const double the_double) {
/*
* given a double, return text
*/
THREAD static char double_as_text[SIZEOF_DOUBLETEXT + 1];
int returnval = 0;
returnval = sprintf(double_as_text, "%17.01f\t", the_double);
return(double_as_text);
}
struct timeval
text_to_timeval(char *timeval_as_text) {
int returnval = 0;
long int seconds, microseconds;
struct timeval the_timeval;
D_PRINTF("T/%d %s\n", (int)timeval_as_text, timeval_as_text);
returnval = sscanf(timeval_as_text, "%ld.%ld",
&seconds, µseconds);
the_timeval.tv_sec = seconds;
the_timeval.tv_usec = microseconds;
return the_timeval;
}
double
text_to_double(char *double_as_text) {
double the_double = 0;
int returnval = 0;
D_PRINTF("D/%d %s\n", (int)double_as_text, double_as_text);
returnval = sscanf(double_as_text, "%lf", &the_double);
return(the_double);
}
rqst_stats_t *
text_to_rqst_stats(char *rqst_stats_as_text) {
THREAD static rqst_stats_t rqst_stats;
rqst_stats_t *the_rqst_stats = &rqst_stats;
the_rqst_stats->totalresponsetime =
text_to_timeval(strtok(rqst_stats_as_text, "\t"));
the_rqst_stats->totalresponsetimesq =
text_to_double(strtok((char *)NULL, "\t"));
the_rqst_stats->minresponsetime =
text_to_timeval(strtok((char *)NULL, "\t"));
the_rqst_stats->maxresponsetime =
text_to_timeval(strtok((char *)NULL, "\t"));
the_rqst_stats->totalconnecttime =
text_to_timeval(strtok((char *)NULL, "\t"));
the_rqst_stats->totalconnecttimesq =
text_to_double(strtok((char *)NULL, "\t"));
the_rqst_stats->minconnecttime =
text_to_timeval(strtok((char *)NULL, "\t"));
the_rqst_stats->maxconnecttime =
text_to_timeval(strtok((char *)NULL, "\t"));
the_rqst_stats->totalconnects = (unsigned long)
text_to_double(strtok((char *)NULL, "\t"));
the_rqst_stats->totalerrs = (unsigned long)
text_to_double(strtok((char *)NULL, "\t"));
the_rqst_stats->totalerrortime =
text_to_timeval(strtok((char *)NULL, "\t"));
the_rqst_stats->totalbytes =
text_to_double(strtok((char *)NULL, "\t"));
the_rqst_stats->totalbytessq =
text_to_double(strtok((char *)NULL, "\t"));
the_rqst_stats->minbytes =
text_to_double(strtok((char *)NULL, "\t"));
the_rqst_stats->maxbytes =
text_to_double(strtok((char *)NULL, "\t"));
the_rqst_stats->totalbody =
text_to_double(strtok((char *)NULL, "\t"));
the_rqst_stats->totalbodysq =
text_to_double(strtok((char *)NULL, "\t"));
the_rqst_stats->minbody =
text_to_double(strtok((char *)NULL, "\t"));
the_rqst_stats->maxbody =
text_to_double(strtok((char *)NULL, "\t"));
return(the_rqst_stats);
} /* end text_to_rqst_stats */
char *
rqst_stats_to_text(rqst_stats_t *the_rqst_stats) {
THREAD static char rqst_stats_as_text[SIZEOF_RQSTSTATSTEXT];
char *tmpbuf;
*rqst_stats_as_text = 0;
tmpbuf = timeval_to_text(&(the_rqst_stats->totalresponsetime));
strcat(rqst_stats_as_text, tmpbuf);
tmpbuf = double_to_text((the_rqst_stats->totalresponsetimesq));
strcat(rqst_stats_as_text, tmpbuf);
tmpbuf = timeval_to_text(&(the_rqst_stats->minresponsetime));
strcat(rqst_stats_as_text, tmpbuf);
tmpbuf = timeval_to_text(&(the_rqst_stats->maxresponsetime));
strcat(rqst_stats_as_text, tmpbuf);
tmpbuf = timeval_to_text(&(the_rqst_stats->totalconnecttime));
strcat(rqst_stats_as_text, tmpbuf);
tmpbuf = double_to_text((the_rqst_stats->totalconnecttimesq));
strcat(rqst_stats_as_text, tmpbuf);
tmpbuf = timeval_to_text(&(the_rqst_stats->minconnecttime));
strcat(rqst_stats_as_text, tmpbuf);
tmpbuf = timeval_to_text(&(the_rqst_stats->maxconnecttime));
strcat(rqst_stats_as_text, tmpbuf);
tmpbuf = double_to_text((the_rqst_stats->totalconnects));
strcat(rqst_stats_as_text, tmpbuf);
tmpbuf = double_to_text((the_rqst_stats->totalerrs));
strcat(rqst_stats_as_text, tmpbuf);
tmpbuf = timeval_to_text(&(the_rqst_stats->totalerrortime));
strcat(rqst_stats_as_text, tmpbuf);
tmpbuf = double_to_text((the_rqst_stats->totalbytes));
strcat(rqst_stats_as_text, tmpbuf);
tmpbuf = double_to_text((the_rqst_stats->totalbytessq));
strcat(rqst_stats_as_text, tmpbuf);
tmpbuf = double_to_text((the_rqst_stats->minbytes));
strcat(rqst_stats_as_text, tmpbuf);
tmpbuf = double_to_text((the_rqst_stats->maxbytes));
strcat(rqst_stats_as_text, tmpbuf);
tmpbuf = double_to_text((the_rqst_stats->totalbody));
strcat(rqst_stats_as_text, tmpbuf);
tmpbuf = double_to_text((the_rqst_stats->totalbodysq));
strcat(rqst_stats_as_text, tmpbuf);
tmpbuf = double_to_text((the_rqst_stats->minbody));
strcat(rqst_stats_as_text, tmpbuf);
tmpbuf = double_to_text((the_rqst_stats->maxbody));
strcat(rqst_stats_as_text, tmpbuf);
D_PRINTF( "rqst_stats_to_text returning %d: %s\n",
strlen(rqst_stats_as_text),
rqst_stats_as_text );
return(rqst_stats_as_text);
}
stats_t *
text_to_stats(char *stats_as_text) {
int i;
rqst_stats_t *the_rqst_stats;
THREAD static stats_t stats;
stats_t *the_stats = &stats;
D_PRINTF( "Parsing stats: %s\n", stats_as_text );
/* grab stats.rs */
the_rqst_stats = text_to_rqst_stats(stats_as_text);
the_stats->rs = *the_rqst_stats;
/* grab main structure */
the_stats->starttime = text_to_timeval(strtok((char *)NULL, "\t"));
the_stats->endtime = text_to_timeval(strtok((char *)NULL, "\t"));
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -