home *** CD-ROM | disk | FTP | other *** search
/ The Developer Connection…ice Driver Kit for OS/2 3 / DEV3-D1.ISO / source / util2src / tlbuf.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-09-30  |  9.2 KB  |  251 lines

  1. /*============================================================================*
  2.  * main() module: tlbuf.c - Test lbuf.c functions.
  3.  *
  4.  * (C)Copyright IBM Corporation, 1991.                           Brian E. Yoder
  5.  *
  6.  * This module tests (and times) the lgets() function in the lbuf.c module.
  7.  *
  8.  * It also times the fgets() function, using setvbuf() to use the same
  9.  * buffer size as used by lgets(), to compare the execution times of these
  10.  * two functions.
  11.  *
  12.  * Note: The environment variable FGETS must be defined (to any value)
  13.  * in order to also run fgets()!
  14.  *
  15.  * Some test results, based on timings to the nearest second:
  16.  *
  17.  * 1. With printf() redirected to NUL, lgets performed just a tiny bit
  18.  *    better than fgets().  A buffer size of 16384 (even multiple of 512)
  19.  *    did the best.  A 122K file was read in 9 secs by lgets and 10 by fgets.
  20.  *
  21.  * 2. With the calls to printf() commented out (when looping to get lines:
  22.  *    see lines marked with %out%), the times got better.  A lot of time
  23.  *    was spent in the printf().  Buffer sizes of 4096, 8192, and 16384 all
  24.  *    resulted in consistent read times of 4 seconds for both lgets and fgets.
  25.  *
  26.  * 10/19/91 - Created.
  27.  * 10/26/91 - Initial version.
  28.  *============================================================================*/
  29.  
  30. #include <stdio.h>
  31. #include <stdlib.h>
  32. #include <fcntl.h>
  33. #include <time.h>
  34. #include <stdlib.h>
  35. #include <string.h>
  36. #include <sys/types.h>
  37. #include <sys/stat.h>
  38.  
  39. #include "util.h"
  40.  
  41. #define MAX_LINE 1024               /* Maximum line length supported */
  42.  
  43. /*----------------------------------------------------------------------------*
  44.  * Static data
  45.  *----------------------------------------------------------------------------*/
  46.  
  47. static  char  buff[MAX_LINE];
  48.  
  49. /*============================================================================*
  50.  * Define internal functions to allow forward access
  51.  *============================================================================*/
  52.  
  53. static void       syntax();
  54.  
  55. /*============================================================================*
  56.  * Main Program Entry Point
  57.  *============================================================================*/
  58.  
  59. main(
  60.  
  61. int argc      ,                     /* arg count */
  62. char **argv   )                     /* arg pointers */
  63.  
  64. {
  65.   int     rc;                       /* Return code store area */
  66.   char   *str;                      /* Pointer to string */
  67.   time_t  tstart, tend;             /* Used to determine elapsed time */
  68.  
  69.   uint    buffsize;                 /* Size of I/O buffer */
  70.   uint    linesize;                 /* Max. line size */
  71.   char   *fname;                    /* Pointer to file name */
  72.  
  73.   int     fd;                       /* File descriptor */
  74.   LBUF   *istream;                  /* Pointer to buffered input stream */
  75.  
  76.   FILE   *infile;                   /* Pointer to open input file */
  77.   char   *iobuff;                   /* Pointer to I/O buffer for fgets() */
  78.  
  79.  /*----------------------------------------------------------------------------*
  80.   * Check arguments
  81.   *----------------------------------------------------------------------------*/
  82.  
  83.   argc--;                           /* Ignore 1st argument (program name) */
  84.   argv++;
  85.  
  86.   if (argc < 2)                     /* Be sure there are at least two arguments */
  87.      syntax();
  88.  
  89.   buffsize = atoi(argv[0]);         /* Convert/store size of I/O buffer */
  90.  
  91.   linesize = atoi(argv[1]);         /* Convert/store maximum line length */
  92.   if (linesize > MAX_LINE)          /* Be sure it isn't longer than our buff[] */
  93.      linesize = MAX_LINE;
  94.  
  95.   if (argc >= 3)                    /* If a filename was specified: */
  96.      fname = argv[2];                    /* Store pointer to it */
  97.   else                              /* Else no filename was specified: */
  98.      fname = NULL;                       /* Store a NULL pointer */
  99.  
  100.  /*----------------------------------------------------------------------------*
  101.   * Open the file (or use stdin, if fname is NULL), and bind the file
  102.   * descriptor to an LBUF buffered stream
  103.   *----------------------------------------------------------------------------*/
  104.  
  105.   istream = newlbuf(buffsize);
  106.   if (istream == NULL)
  107.   {
  108.      fprintf(stderr, "Cannot allocate memory for istream.\n");
  109.      exit(2);
  110.   }
  111.  
  112.   if (fname == NULL)
  113.      fd = 0;
  114.   else
  115.   {
  116.      fd = open(fname, O_RDONLY | O_BINARY);
  117.      if (fd == -1)
  118.      {
  119.         fprintf(stderr, "Cannot open file '%s'.\n", fname);
  120.         exit(2);
  121.      }
  122.   }
  123.  
  124.   setlbuf(istream, fd);
  125.  
  126.  /*----------------------------------------------------------------------------*
  127.   * Display what we've got so far
  128.   *----------------------------------------------------------------------------*/
  129.  
  130.   fprintf(stderr, "Input file           : %s\n", fname != NULL ? fname : "(stdin)" );
  131.   fprintf(stderr, "Buffer size (spec'd) : %u\n", buffsize);
  132.   fprintf(stderr, "Buffer size (actual) : %u\n", istream->bufflen);
  133.   fprintf(stderr, "Max line length      : %u\n", linesize);
  134.   fprintf(stderr, "-------------------------------------------------\n");
  135.   fflush(stderr);
  136.  
  137.  /*----------------------------------------------------------------------------*
  138.   * Read from the stream using lgets()
  139.   *----------------------------------------------------------------------------*/
  140.  
  141.   tstart = time(NULL);
  142.  
  143.   for (;;)                          /* Loop: */
  144.   {
  145.      str = lgets(buff, linesize,         /* Get next line from stream */
  146.                  istream);
  147.  
  148.      if (str == NULL)                    /* If NULL returned: */
  149.         break;                                /* End-of-stream, or error: quit */
  150.  
  151.      printf("%s\n", buff);               /* Show the line %out% */
  152.   }
  153.  
  154.   tend = time(NULL);
  155.  
  156.   if (fd != 0) close(fd);           /* Close the file, if not stdin */
  157.  
  158.   fprintf(stderr, "Elapsed time using lgets(): %ld seconds.\n",
  159.      tend - tstart);
  160.  
  161.  /*============================================================================*
  162.   * Use setvbuf() and fgets(), to compare time needed to process the file
  163.   *============================================================================*/
  164.  
  165.   str = getenv("FGETS");            /* First see of FGETS is in environment */
  166.   if (str == NULL)
  167.      return(0);
  168.  
  169.   fprintf(stderr, "Reading using fgets()...\n" );
  170.  
  171.   if (fname == NULL)                /* If no filename: use stdin */
  172.      infile = stdin;
  173.   else                              /* Else: Open the file */
  174.   {
  175.      infile = fopen(fname, "r");
  176.      if (infile == NULL)
  177.      {
  178.         fprintf(stderr, "Cannot fopen file '%s'.\n", fname);
  179.         exit(2);
  180.      }
  181.   }
  182.  
  183.   iobuff = malloc(buffsize);        /* Allocate I/O buffer */
  184.   if (iobuff == NULL)
  185.   {
  186.      fprintf(stderr, "Cannot allocate %u bytes for I/O buffer.\n", buffsize);
  187.      return(2);
  188.   }
  189.  
  190.   rc = setvbuf(infile, iobuff, _IOFBF, buffsize);
  191.   if (rc != 0)
  192.   {
  193.      fprintf(stderr, "Call to setvbuf() failed.\n");
  194.      return(2);
  195.   }
  196.  
  197.  /*----------------------------------------------------------------------------*
  198.   * Read from the stream using fgets()
  199.   *----------------------------------------------------------------------------*/
  200.  
  201.   tstart = time(NULL);
  202.  
  203.   for (;;)                          /* Loop: */
  204.   {
  205.      str = fgets(buff, linesize,         /* Get next line from stream */
  206.                  infile);
  207.  
  208.      if (str == NULL)                    /* If NULL returned: */
  209.         break;                                /* End-of-stream, or error: quit */
  210.  
  211.      printf("%s\n", buff);               /* Show the line %out% */
  212.   }
  213.  
  214.   tend = time(NULL);
  215.  
  216.   if (fd != 0) close(fd);           /* Close the file, if not stdin */
  217.  
  218.   fprintf(stderr, "Elapsed time using fgets(): %ld seconds.\n",
  219.      tend - tstart);
  220.  
  221.  /*----------------------------------------------------------------------------*
  222.   * Done
  223.   *----------------------------------------------------------------------------*/
  224.  
  225.   return(0);                        /* Done with main(): Return */
  226.  
  227. } /* end of main() */
  228.  
  229. /*============================================================================*
  230.  * syntax() - Display command syntax and exit to operating system!
  231.  *============================================================================*/
  232. static void syntax()
  233. {
  234.   fprintf(stderr, "Usage:  tlbuf buffsize linesize [fname]\n");
  235.   fprintf(stderr, "\n");
  236.   fprintf(stderr, "   This program uses lgets() to read from the specified file.\n");
  237.   fprintf(stderr, "   If no filename is given, then lgets reads from stdin.\n");
  238.   fprintf(stderr, "   The elasped read/write time, in seconds, is shown.  Direct\n");
  239.   fprintf(stderr, "   standard output to NUL to just time reading.\n");
  240.   fprintf(stderr, "\n");
  241.   fprintf(stderr, "   The buffsize argument specifies the size of the I/O buffer.\n");
  242.   fprintf(stderr, "\n");
  243.   fprintf(stderr, "   The linesize argument specifies the maximum length of lines\n");
  244.   fprintf(stderr, "   that can be read at one time.  A maximum of %d is supported.\n", MAX_LINE);
  245.   fprintf(stderr, "\n");
  246.   fprintf(stderr, "   If the FGETS environment variable is defined, then the process\n");
  247.   fprintf(stderr, "   is repeated using fgets(), to compare performance.\n");
  248.   fprintf(stderr, "\n");
  249.   exit(1);
  250. }
  251.