Main Page   Data Structures   File List   Data Fields   Globals   Related Pages  

deppmark_main.c

00001 /***************************************************************************
00002                                deppmark_main.c
00003                              -------------------
00004     begin                : Tue Jan 15 19:19:41 CET 2002
00005     copyright            : (C) 2002 by Christian Hoenig & Gunter Ohrner
00006     email                : pdepp@customcdrom.de
00007  ***************************************************************************/
00008 
00009 /***************************************************************************
00010  *                                                                         *
00011  *   This program is free software; you can redistribute it and/or modify  *
00012  *   it under the terms of the GNU General Public License as published by  *
00013  *   the Free Software Foundation; either version 2 of the License, or     *
00014  *   (at your option) any later version.                                   *
00015  *                                                                         *
00016  ***************************************************************************/
00017 
00018 
00019 #include "pdeppd/common.h"
00020 
00021 
00022 #ifdef HAVE_GETOPT_H
00023 #  include <getopt.h>
00024 #endif
00025 
00026 
00027 #include <errno.h>
00028 #include <netdb.h>
00029 #include <pthread.h>
00030 #include <stdio.h>
00031 #include <stdlib.h>
00032 #include <string.h>
00033 #include <unistd.h>
00034 #include <sys/types.h>
00035 #include <sys/socket.h>
00036 #include <netinet/in.h>
00037 #include <arpa/inet.h>
00038 
00039 #include "client.h"
00040 #include "pdeppd/misc.h"
00041 #include "pdeppd/xmalloc.h"
00042 #include "pdeppd/bufplist.h"
00043 #include "pdeppd/pthreadlist.h"
00044 #include "deppmark.h"
00045 
00046 
00047 int runLikeHell(char* file, unsigned num_of_users);
00048 
00049 
00053 void printVersionText()
00054 {
00055   printf("%s (%s) Version %s\n", BENCHNAME, PACKAGE, BENCHVERSION);
00056 }
00057 
00058 
00062 void printCopyright()
00063 {
00064   printVersionText();
00065   printf("Copyright (C) 2002 Christian Hoenig & Gunter Ohrner <pdepp@CustomCDROM.de>\n"
00066          "%s comes with ABSOLUTELY NO WARRANTY. This is free software, and\n"
00067          "you are welcome to redistribute it under certain conditions.\n"
00068          "For more details see the GNU General Public License Version 2\n"
00069          "(http://www.gnu.org/copyleft/gpl.html).\n\n",
00070          PACKAGE);
00071 }
00072 
00073 
00080 void printHelpText(int briefHelp, char* argv_0)
00081 {
00082   if (briefHelp)
00083   { //print brief info
00084     fprintf(stderr,
00085 #ifdef HAVE_GETOPT_LONG
00086       "Try %s --help\n", argv_0
00087 #else
00088       "Try %s -h\n", argv_0
00089 #endif        
00090     );
00091   }
00092   else
00093   { //print long helptext
00094     printf(
00095       "Usage: %s [options]\n\n"
00096       "Options:\n"
00097 #ifdef HAVE_GETOPT_LONG
00098 //      "--benchmark    "
00099 #endif
00100 //     "-b        Benchmarking mode: disable all delays and run like hell...\n"
00101 #ifdef HAVE_GETOPT_LONG
00102       "--file=file     "
00103 #endif
00104       "-f file   Tells %s that <file> contains a list of files to be used in that test\n"
00105 #ifdef HAVE_GETOPT_LONG
00106       "--help          "
00107 #endif
00108       "-h        Prints this text\n"
00109 #ifdef HAVE_GETOPT_LONG
00110       "--host=NAME     "
00111 #endif
00112       "-H NAME   Tells %s to use NAME as destination host\n"
00113 #ifdef HAVE_GETOPT_LONG
00114       "--delay=N       "
00115 #endif
00116       "-d N      Virtual user pauses N / 10 sec between querys\n"
00117 #ifdef HAVE_GETOPT_LONG
00118       "--port=N        "
00119 #endif
00120       "-p N      Tells %s to use N as benchmarking destination port\n"
00121 #ifdef HAVE_GETOPT_LONG
00122       "--repetitions=N "
00123 #endif
00124       "-r N      Every VirtualUser repeats N times the whole URI-list\n"
00125 #ifdef HAVE_GETOPT_LONG
00126       "--users=N       "
00127 #endif
00128       "-u N      Sets the count of VirtualUsers to be simulated\n"
00129 #ifdef HAVE_GETOPT_LONG
00130       "--version       "
00131 #endif
00132       "-v        Prints version information and exits\n"
00133       ,
00134       argv_0,
00135       BENCHNAME,
00136       BENCHNAME,
00137       BENCHNAME);
00138   }
00139 }
00140 
00141 
00142 
00143 
00147 int main(int argc, char *argv[])
00148 {
00149   int ret_val = EXIT_SUCCESS;
00150   int c;
00151   int size = 0;
00152   unsigned user = 1;
00153   char* file = NULL;
00154   struct hostent* host;
00155   long int tmp;
00156 
00157   // set some default values
00158   benchmark = false;
00159   delay = 1;
00160   repeat = 1;
00161 
00162   //temporary data for the commandline parameters
00163   
00164 #ifndef NDEBUG
00165   fprintf(stderr, "\n\n!!! DEBUG mode enabled !!!\n\n");
00166 #endif
00167 
00168   printCopyright();
00169 
00170 
00171   while (true)
00172   {
00173 #ifdef HAVE_GETOPT_LONG
00174     int option_index = 0;
00175     static struct option long_options[] =
00176     {
00177       {"benchmark"              , 0, 0, 'b'},
00178       {"file"                   , 1, 0, 'f'},
00179       {"help"                   , 0, 0, 'h'},
00180       {"host"                   , 1, 0, 'H'},
00181       {"delay"                  , 1, 0, 'd'},
00182       {"port"                   , 1, 0, 'p'},
00183       {"repetitions"            , 1, 0, 'r'},
00184       {"users"                  , 1, 0, 'u'},
00185       {"version"                , 0, 0, 'v'},
00186       {0, 0, 0, 0}
00187     };
00188     c = getopt_long(argc, argv, "bf:hH:d:p:r:u:v", long_options, &option_index);
00189 #else
00190     c = getopt(argc, argv,"bf:hH:d:p:r:u:v");
00191 #endif
00192     //if no more parameters found, leave...
00193     if (c == -1)
00194       break;
00195 
00196     //check the commandline parameters:
00197     switch (c)
00198     {
00199     case('b'): //benchmark
00200       benchmark = true;
00201       break;
00202     case('f'): //file
00203       size = strlen(optarg)+1;
00204       file = (char*)pd_malloc(size);
00205       strcpy(file, optarg);
00206       break;
00207     case('h'): //help
00208       printHelpText(false,argv[0]);
00209       return EXIT_SUCCESS;
00210     case('H'): //host
00211       if ((host = gethostbyname(optarg)) == NULL)
00212       {
00213         fprintf(stderr, "Invalid Hostname / IP-Address given: %s\n", optarg);
00214         exit(EXIT_FAILURE);
00215       }
00216 
00217       host_addr.s_addr = *((unsigned long int*) (host->h_addr_list)[0]);
00218 
00219 /*      if (inet_aton(test, &host_addr) == 0)
00220       {
00221         fprintf(stderr, "Invalid Hostname / IP-Address given: %s\n", optarg);
00222         exit(EXIT_FAILURE);
00223       }*/
00224 
00225       break;
00226     case('d'): // delay
00227       tmp = strtol(optarg, NULL, 10);
00228       if (tmp < 0)
00229       {
00230         fprintf(stderr, "Error: invalid delay value given: %u\n", delay);
00231         tmp = 1;
00232       }
00233       delay = (unsigned int) tmp;
00234       break;
00235     case('p'): //port
00236       tmp = strtol(optarg, NULL, 10);
00237       if (tmp < 0 || tmp > 65535)
00238       {
00239         fprintf(stderr, "Error: Invalid port given: %s\n", optarg);
00240         tmp = 1952;
00241       }
00242       port = htons((unsigned short) tmp);
00243       break;
00244     case('r'): // repetition
00245       repeat = strtol(optarg, NULL, 10);
00246       if (repeat <= 0)
00247       {
00248         fprintf(stderr, "Error: Invalid repetition value given: %s\n", optarg);
00249         repeat = 1;
00250       }
00251       break;
00252     case('u'): // user
00253       user = strtol(optarg, NULL, 10);
00254       if (user <= 0)
00255       {
00256         fprintf(stderr, "Error: Invalid number of users given: %s\n", optarg);
00257         user = 1;
00258       }
00259       break;
00260     case('v'): //version
00261 //      printVersionText();
00262       return EXIT_SUCCESS;
00263     default:
00264       printHelpText(true,argv[0]);
00265       return EXIT_FAILURE;
00266     }
00267   }
00268 
00269   //start usersimulation/benchmarking here:
00270   ret_val = runLikeHell(file, user);
00271 
00272 
00273 #ifndef NDEBUG
00274   // this is an unfreed malloc to see if mpatrol really is working *g*
00275   pd_malloc(1952);
00276 #endif
00277 
00278   return ret_val;
00279 }
00280 
00281 
00292 int runLikeHell(char* file, unsigned num_of_users)
00293 {
00294   unsigned cnt;
00295   char* line;
00296   size_t maxtmp;
00297   pthread_t thrd;
00298   PThreadList vclient_list;
00299   time_t start_time, duration;
00300 
00301   FILE* filep = fopen(file, "r");
00302 
00303   if (filep == NULL)
00304   {
00305     fprintf(stderr, "Error: URL file could not be opened: %s\n", strerror(errno));
00306     return -1;
00307   }
00308 
00309   puts("Starting benchmark...");
00310 
00311   file_list = bufpListInit();
00312 
00313   buf_len = 0;
00314   while ((line = getLine(filep)))
00315   {
00316     maxtmp = strlen(line);
00317     bufpListAppend(file_list, bufpInit(line, maxtmp, 0));
00318     buf_len = MAX(buf_len, maxtmp);
00319     if (feof(filep))
00320       break;
00321   }
00322 
00323   buf_len = MAX(4 + buf_len + 9 + 4 + 1, 1024);
00324 
00325   pthread_mutex_init(&malloc_mut, NULL);
00326 
00327   vclient_list = pthrdListInit();
00328 
00329   start_time = time(NULL);
00330 
00331   for(cnt = 1; cnt <= num_of_users; ++cnt)   /* this is pretty vv dirty vv but seems to work... */
00332     if (pthread_create(&thrd, NULL, (void*)virtualClient, (void*)cnt) == 0)
00333     {
00334 #ifndef NDEBUG 
00335       printf("A new virtual client has been spawned successfully...\n");
00336 #endif      
00337       pthrdListAppend(vclient_list, thrd);
00338     }
00339     else
00340       fprintf(stderr, "Error: Thread creation failed! %s\n", strerror(errno));
00341 
00342   pthrdListFirst(vclient_list);
00343   while(!pthrdListIsTail(vclient_list))
00344   {
00345     int* retval;
00347     thrd = pthrdListGetPayload(vclient_list);
00348     pthread_join(thrd, (void**) &retval);
00349     pthrdListDelete(vclient_list);
00350   }
00351 
00352   duration = time(NULL) - start_time;
00353 
00354   printf("The benchmark (%u users, %u 1/10 sec delay, %u repetitions) took\n"
00355          "%lu seconds wall time to complete.\n\n",
00356          num_of_users, delay, repeat, duration);
00357 
00358   pthrdListTerminate(vclient_list);
00359 
00360   return 0;
00361 }

Generated on Fri Jan 25 22:40:31 2002 for PDepp Webserver by doxygen1.2.11.1 written by Dimitri van Heesch, © 1997-2001