diff options
Diffstat (limited to 'tests')
-rw-r--r-- | tests/filewriter.c | 158 | ||||
-rw-r--r-- | tests/tcpflood.c | 271 |
2 files changed, 399 insertions, 30 deletions
diff --git a/tests/filewriter.c b/tests/filewriter.c new file mode 100644 index 00000000..07991b1d --- /dev/null +++ b/tests/filewriter.c @@ -0,0 +1,158 @@ +/* This program expands the input file several times. This + * is done in order to obtain large (and maybe huge) files for + * testing. Note that the input file is stored in memory. It's + * last line must properly be terminated. + * Max input line size is 10K. + * + * command line options: + * -i file to be used for input (else stdin) + * -o file to be used for output (else stdout) + * -c number of times the file is to be copied + * -n add line numbers (default: off) + * -w wait nbr of microsecs between batches + * -W number of file lines to generate in a batch + * This is useful only if -w is specified as well, + * default is 1000. + * + * Copyright 2010 Rainer Gerhards and Adiscon GmbH. + * + * This file is part of rsyslog. + * + * Rsyslog is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Rsyslog is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Rsyslog. If not, see <http://www.gnu.org/licenses/>. + * + * A copy of the GPL can be found in the file "COPYING" in this distribution. + */ +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <getopt.h> + +/* input file is stored in a single-linked list */ +struct line { + struct line *next; + char *ln; +} *root, *tail; + +static FILE *fpIn; +static FILE *fpOut; +static long long nCopies = 1; +static int linenbrs = 0; +static int waitusecs = 0; +static int batchsize = 1000; + + +/* read the input file and create in-memory representation + */ +static inline void +readFile() +{ + char *r; + char lnBuf[10240]; + struct line *node; + + root = tail = NULL; + r = fgets(lnBuf, sizeof(lnBuf), fpIn); + while(r != NULL) { + node = malloc(sizeof(struct line)); + if(node == NULL) { + perror("malloc node"); + exit(1); + } + node->next = NULL; + node->ln = strdup(lnBuf); + if(node->ln == NULL) { + perror("malloc node"); + exit(1); + } + if(tail == NULL) { + tail = root = node; + } else { + tail->next = node; + tail = node; + } + r = fgets(lnBuf, sizeof(lnBuf), fpIn); + } + if(!feof(fpIn)) { + perror("fgets"); + fprintf(stderr, "end of read loop, but not end of file!"); + exit(1); + } +} + + +static void +genCopies() +{ + long long i; + long long unsigned lnnbr; + struct line *node; + + lnnbr = 1; + for(i = 0 ; i < nCopies ; ++i) { + if(i % 10000 == 0) + fprintf(stderr, "copyrun %d\n", i); + if(waitusecs && (i % batchsize == 0)) { + usleep(waitusecs); + } + for(node = root ; node != NULL ; node = node->next) { + if(linenbrs) + fprintf(fpOut, "%12.12llu:%s", lnnbr, node->ln); + else + fprintf(fpOut, "%s", node->ln); + ++lnnbr; + } + } +} + +void main(int argc, char *argv[]) +{ + int opt; + fpIn = stdin; + fpOut = stdout; + + while((opt = getopt(argc, argv, "i:o:c:nw:W:")) != -1) { + switch (opt) { + case 'i': /* input file */ + if((fpIn = fopen(optarg, "r")) == NULL) { + perror(optarg); + exit(1); + } + break; + case 'o': /* output file */ + if((fpOut = fopen(optarg, "w")) == NULL) { + perror(optarg); + exit(1); + } + break; + case 'c': + nCopies = atoll(optarg); + break; + case 'n': + linenbrs = 1; + break; + case 'w': + waitusecs = atoi(optarg); + break; + case 'W': + batchsize = atoi(optarg); + break; + default: printf("invalid option '%c' or value missing - terminating...\n", opt); + exit (1); + break; + } + } + + readFile(); + genCopies(); +} diff --git a/tests/tcpflood.c b/tests/tcpflood.c index a37845a3..acca4452 100644 --- a/tests/tcpflood.c +++ b/tests/tcpflood.c @@ -16,6 +16,7 @@ * bytes as forth. Add -r to randomize the amount of extra * data included in the range 1..(value of -d). * -r randomize amount of extra data added (-d must be > 0) + * -s (silent) do not show progress indicator (never done on non-tty) * -f support for testing dynafiles. If given, include a dynafile ID * in the range 0..(f-1) as the SECOND field, shifting all field values * one field to the right. Zero (default) disables this functionality. @@ -32,6 +33,19 @@ * -D randomly drop and re-establish connections. Useful for stress-testing * the TCP receiver. * -F USASCII value for frame delimiter (in octet-stuffing mode), default LF + * -R number of times the test shall be run (very useful for gathering performance + * data and other repetitive things). Default: 1 + * -S number of seconds to sleep between different runs (-R) Default: 30 + * -X generate sTats data records. Default: off + * -e encode output in CSV (not yet everywhere supported) + * for performance data: + * each inidividual line has the runtime of one test + * the last line has 0 in field 1, followed by numberRuns,TotalRuntime, + * Average,min,max + * -T transport to use. Currently supported: "udp", "tcp" (default) + * Note: UDP supports a single target port, only + * -W wait time between sending batches of messages, in microseconds (Default: 0) + * -b number of messages within a batch (default: 100,000,000 millions) * * Part of the testbench for rsyslog. * @@ -67,6 +81,7 @@ #include <string.h> #include <netinet/in.h> #include <sys/resource.h> +#include <sys/time.h> #define EXIT_FAILURE 1 #define INVALID_SOCKET -1 @@ -87,6 +102,7 @@ static int numConnections = 1; /* number of connections to create */ static int *sockArray; /* array of sockets to use */ static int msgNum = 0; /* initial message number to start with */ static int bShowProgress = 1; /* show progress messages */ +static int bSilent = 0; /* completely silent operation */ static int bRandConnDrop = 0; /* randomly drop connections? */ static char *MsgToSend = NULL; /* if non-null, this is the actual message to send */ static int bBinaryFile = 0; /* is -I file binary */ @@ -95,6 +111,44 @@ static int numFileIterations = 1;/* how often is file data to be sent? */ static char frameDelim = '\n'; /* default frame delimiter */ FILE *dataFP = NULL; /* file pointer for data file, if used */ static long nConnDrops = 0; /* counter: number of time connection was dropped (-D option) */ +static int numRuns = 1; /* number of times the test shall be run */ +static int sleepBetweenRuns = 30; /* number of seconds to sleep between runs */ +static int bStatsRecords = 0; /* generate stats records */ +static int bCSVoutput = 0; /* generate output in CSV (where applicable) */ +static long long batchsize = 100000000ll; +static int waittime = 0; + + +/* the following structure is used to gather performance data */ +struct runstats { + unsigned long long totalRuntime; + unsigned long minRuntime; + unsigned long maxRuntime; + int numRuns; +}; + +static int udpsock; /* socket for sending in UDP mode */ +static struct sockaddr_in udpRcvr; /* remote receiver in UDP mode */ + +static enum { TP_UDP, TP_TCP } transport = TP_TCP; + +/* prepare send subsystem for UDP send */ +static inline int +setupUDP(void) +{ + if((udpsock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) == -1) + return 1; + + memset((char *) &udpRcvr, 0, sizeof(udpRcvr)); + udpRcvr.sin_family = AF_INET; + udpRcvr.sin_port = htons(targetPort); + if(inet_aton(targetIP, &udpRcvr.sin_addr)==0) { + fprintf(stderr, "inet_aton() failed\n"); + return(1); + } + + return 0; +} /* open a single tcp connection @@ -154,6 +208,9 @@ int openConnections(void) char msgBuf[128]; size_t lenMsg; + if(transport == TP_UDP) + return setupUDP(); + if(bShowProgress) write(1, " open connections", sizeof(" open connections")-1); sockArray = calloc(numConnections, sizeof(int)); @@ -167,8 +224,10 @@ int openConnections(void) return 1; } } - lenMsg = sprintf(msgBuf, "\r%5.5d open connections\n", i); - write(1, msgBuf, lenMsg); + if(bShowProgress) { + lenMsg = sprintf(msgBuf, "\r%5.5d open connections\n", i); + write(1, msgBuf, lenMsg); + } return 0; } @@ -188,6 +247,9 @@ void closeConnections(void) struct linger ling; char msgBuf[128]; + if(transport != TP_TCP) + return; + if(bShowProgress) write(1, " close connections", sizeof(" close connections")-1); for(i = 0 ; i < numConnections ; ++i) { @@ -207,8 +269,10 @@ void closeConnections(void) close(sockArray[i]); } } - lenMsg = sprintf(msgBuf, "\r%5.5d close connections\n", i); - write(1, msgBuf, lenMsg); + if(bShowProgress) { + lenMsg = sprintf(msgBuf, "\r%5.5d close connections\n", i); + write(1, msgBuf, lenMsg); + } } @@ -218,12 +282,11 @@ void closeConnections(void) * of constructing test messages. -- rgerhards, 2010-03-31 */ static inline void -genMsg(char *buf, size_t maxBuf, int *pLenBuf) +genMsg(char *buf, size_t maxBuf, int *pLenBuf, long long *numMsgsGen) { int edLen; /* actual extra data length to use */ char extraData[MAX_EXTRADATA_LEN + 1]; char dynFileIDBuf[128] = ""; - static int numMsgsGen = 0; int done; if(dataFP != NULL) { @@ -263,7 +326,7 @@ genMsg(char *buf, size_t maxBuf, int *pLenBuf) *pLenBuf = snprintf(buf, maxBuf, "%s\n", MsgToSend); } - if(numMsgsGen++ >= numMsgsToSend) + if((*numMsgsGen)++ >= (unsigned) numMsgsToSend) *pLenBuf = 0; /* indicate end of run */ finalize_it: ; @@ -284,14 +347,18 @@ int sendMessages(void) int lenBuf; int lenSend; char *statusText; + long long numSent = 0; /* number of messages sent in this test */ char buf[MAX_EXTRADATA_LEN + 1024]; - if(dataFile == NULL) { - printf("Sending %d messages.\n", numMsgsToSend); - statusText = "messages"; - } else { - printf("Sending file '%s' %d times.\n", dataFile, numFileIterations); - statusText = "kb"; + if(!bSilent) { + if(dataFile == NULL) { + printf("Sending %d messages.\n", numMsgsToSend); + statusText = "messages"; + } else { + printf("Sending file '%s' %d times.\n", dataFile, + numFileIterations); + statusText = "kb"; + } } if(bShowProgress) printf("\r%8.8d %s sent", 0, statusText); @@ -304,23 +371,27 @@ int sendMessages(void) int rnd = rand(); socknum = rnd % numConnections; } - genMsg(buf, sizeof(buf), &lenBuf); /* generate the message to send according to params */ + genMsg(buf, sizeof(buf), &lenBuf, &numSent); /* generate the message to send according to params */ if(lenBuf == 0) break; /* end of processing! */ - if(sockArray[socknum] == -1) { - /* connection was dropped, need to re-establish */ - if(openConn(&(sockArray[socknum])) != 0) { - printf("error in trying to re-open connection %d\n", socknum); - exit(1); + if(transport == TP_TCP) { + if(sockArray[socknum] == -1) { + /* connection was dropped, need to re-establish */ + if(openConn(&(sockArray[socknum])) != 0) { + printf("error in trying to re-open connection %d\n", socknum); + exit(1); + } } + lenSend = send(sockArray[socknum], buf, lenBuf, 0); + } else if(transport == TP_UDP) { + lenSend = sendto(udpsock, buf, lenBuf, 0, &udpRcvr, sizeof(udpRcvr)); } - lenSend = send(sockArray[socknum], buf, lenBuf, 0); if(lenSend != lenBuf) { printf("\r%5.5d\n", i); fflush(stdout); perror("send test data"); - printf("send() failed at socket %d, index %d, msgNum %d\n", - sockArray[socknum], i, msgNum); + printf("send() failed at socket %d, index %d, msgNum %lld\n", + sockArray[socknum], i, numSent); fflush(stderr); return(1); } @@ -338,10 +409,121 @@ int sendMessages(void) sockArray[socknum] = -1; } } + if(numSent % batchsize == 0) { + usleep(waittime); + } ++msgNum; ++i; } - printf("\r%8.8d %s sent\n", i, statusText); + if(!bSilent) + printf("\r%8.8d %s sent\n", i, statusText); + + return 0; +} + + +/* functions related to computing statistics on the runtime of a test. This is + * a separate function primarily not to mess up the test driver. + * rgerhards, 2010-12-08 + */ +static inline void +endTiming(struct timeval *tvStart, struct runstats *stats) +{ + long sec, usec; + unsigned long runtime; + struct timeval tvEnd; + + gettimeofday(&tvEnd, NULL); + if(tvStart->tv_usec > tvEnd.tv_usec) { + tvEnd.tv_sec--; + tvEnd.tv_usec += 1000000; + } + + sec = tvEnd.tv_sec - tvStart->tv_sec; + usec = tvEnd.tv_usec - tvStart->tv_usec; + + runtime = sec * 1000 + (usec / 1000); + stats->totalRuntime += runtime; + if(runtime < stats->minRuntime) + stats->minRuntime = runtime; + if(runtime > stats->maxRuntime) + stats->maxRuntime = runtime; + + if(!bSilent || bStatsRecords) { + if(bCSVoutput) { + printf("%ld.%4.4ld\n", runtime / 1000, runtime % 1000); + } else { + printf("runtime: %ld.%4.4ld\n", runtime / 1000, runtime % 1000); + } + } +} + + +/* generate stats summary record at end of run + */ +static inline void +genStats(struct runstats *stats) +{ + long unsigned avg; + avg = stats->totalRuntime / stats->numRuns; + + if(bCSVoutput) { + printf("#numRuns,TotalRuntime,AvgRuntime,MinRuntime,MaxRuntime\n"); + printf("%d,%llu.%4.4d,%lu.%4.4lu,%lu.%4.4lu,%lu.%4.4lu\n", + stats->numRuns, + stats->totalRuntime / 1000, (int) stats->totalRuntime % 1000, + avg / 1000, avg % 1000, + stats->minRuntime / 1000, stats->minRuntime % 1000, + stats->maxRuntime / 1000, stats->maxRuntime % 1000); + } else { + printf("Runs: %d\n", stats->numRuns); + printf("Runtime:\n"); + printf(" total: %llu.%4.4d\n", stats->totalRuntime / 1000, + (int) stats->totalRuntime % 1000); + printf(" avg: %lu.%4.4lu\n", avg / 1000, avg % 1000); + printf(" min: %lu.%4.4lu\n", stats->minRuntime / 1000, stats->minRuntime % 1000); + printf(" max: %lu.%4.4lu\n", stats->maxRuntime / 1000, stats->maxRuntime % 1000); + printf("All times are wallclock time.\n"); + } +} + + +/* Run the actual test. This function handles various meta-parameters, like + * a specified number of iterations, performance measurement and so on... + * rgerhards, 2010-12-08 + */ +static int +runTests(void) +{ + struct timeval tvStart; + struct runstats stats; + int run; + + stats.totalRuntime = 0; + stats.minRuntime = (unsigned long) 0xffffffffffffffff; + stats.maxRuntime = 0; + stats.numRuns = numRuns; + run = 1; + while(1) { /* loop broken inside */ + if(!bSilent) + printf("starting run %d\n", run); + gettimeofday(&tvStart, NULL); + if(sendMessages() != 0) { + printf("error sending messages (run %d)\n", run); + return 1; + } + endTiming(&tvStart, &stats); + if(run == numRuns) + break; + if(!bSilent) + printf("sleeping %d seconds before next run\n", sleepBetweenRuns); + sleep(sleepBetweenRuns); + ++run; + } + + if(bStatsRecords) { + genStats(&stats); + } return 0; } @@ -369,11 +551,10 @@ int main(int argc, char *argv[]) setvbuf(stdout, buf, _IONBF, 48); - if(!isatty(1)) - bShowProgress = 0; - - while((opt = getopt(argc, argv, "f:F:t:p:c:C:m:i:I:P:d:Dn:M:rB")) != -1) { + while((opt = getopt(argc, argv, "b:ef:F:t:p:c:C:m:i:I:P:d:Dn:M:rsBR:S:T:XW:")) != -1) { switch (opt) { + case 'b': batchsize = atoll(optarg); + break; case 't': targetIP = optarg; break; case 'p': targetPort = atoi(optarg); @@ -413,14 +594,43 @@ int main(int argc, char *argv[]) */ numMsgsToSend = 1000000; break; + case 's': bSilent = 1; + break; case 'B': bBinaryFile = 1; break; + case 'R': numRuns = atoi(optarg); + break; + case 'S': sleepBetweenRuns = atoi(optarg); + break; + case 'X': bStatsRecords = 1; + break; + case 'e': bCSVoutput = 1; + break; + case 'T': if(!strcmp(optarg, "udp")) { + transport = TP_UDP; + } else if(!strcmp(optarg, "tcp")) { + transport = TP_TCP; + } else { + fprintf(stderr, "unkonwn transport '%s'\n", optarg); + exit(1); + } + break; + case 'W': waittime = atoi(optarg); + break; default: printf("invalid option '%c' or value missing - terminating...\n", opt); exit (1); break; } } + if(bStatsRecords && waittime) { + fprintf(stderr, "warning: generating performance stats and useing a waittime " + "is somewhat contradictory!\n"); + } + + if(!isatty(1) || bSilent) + bShowProgress = 0; + if(numConnections > 20) { /* if we use many (whatever this means, 20 is randomly picked) * connections, we need to make sure we have a high enough @@ -447,8 +657,8 @@ int main(int argc, char *argv[]) exit(1); } - if(sendMessages() != 0) { - printf("error sending messages\n"); + if(runTests() != 0) { + printf("error running tests\n"); exit(1); } @@ -457,7 +667,8 @@ int main(int argc, char *argv[]) if(nConnDrops > 0) printf("-D option initiated %ld connection closures\n", nConnDrops); - printf("End of tcpflood Run\n"); + if(!bSilent) + printf("End of tcpflood Run\n"); exit(ret); } |