home *** CD-ROM | disk | FTP | other *** search
/ Tools / WinSN5.0Ver.iso / NETSCAP.50 / WIN1998.ZIP / ns / nsprpub / pr / tests / fsync.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-04-08  |  4.3 KB  |  137 lines

  1. /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
  2. /*
  3.  * The contents of this file are subject to the Netscape Public License
  4.  * Version 1.0 (the "NPL"); you may not use this file except in
  5.  * compliance with the NPL.  You may obtain a copy of the NPL at
  6.  * http://www.mozilla.org/NPL/
  7.  * 
  8.  * Software distributed under the NPL is distributed on an "AS IS" basis,
  9.  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL
  10.  * for the specific language governing rights and limitations under the
  11.  * NPL.
  12.  * 
  13.  * The Initial Developer of this code under the NPL is Netscape
  14.  * Communications Corporation.  Portions created by Netscape are
  15.  * Copyright (C) 1998 Netscape Communications Corporation.  All Rights
  16.  * Reserved.
  17.  */
  18.  
  19. #include "prio.h"
  20. #include "prmem.h"
  21. #include "prprf.h"
  22. #include "prinrval.h"
  23.  
  24. #include "plerror.h"
  25. #include "plgetopt.h"
  26.  
  27. static PRFileDesc *err = NULL;
  28.  
  29. static void Help(void)
  30. {
  31.     PR_fprintf(err, "Usage: [-S] [-K <n>] [-h] <filename>\n");
  32.     PR_fprintf(err, "\t-c   Nuber of iterations     (default: 10)\n");
  33.     PR_fprintf(err, "\t-S   Sync the file           (default: FALSE)\n");
  34.     PR_fprintf(err, "\t-K   Size of file (K bytes)  (default: 10)\n");
  35.     PR_fprintf(err, "\t     Name of file to write   (default: /usr/tmp/sync.dat)\n");
  36.     PR_fprintf(err, "\t-h   This message and nothing else\n");
  37. }  /* Help */
  38.  
  39. PRIntn main(PRIntn argc, char **argv)
  40. {
  41.     PRStatus rv;
  42.     PLOptStatus os;
  43.     PRUint8 *buffer;
  44.     PRFileDesc *file = NULL;
  45.     const char *filename = "/usr/tmp/sync.dat";
  46.     PRUint32 index, loops, iterations = 10, filesize = 10;
  47.     PRIntn flags = PR_WRONLY | PR_CREATE_FILE | PR_TRUNCATE;
  48.     PLOptState *opt = PL_CreateOptState(argc, argv, "hSK:c:");
  49.     PRIntervalTime time, total = 0, shortest = 0x7fffffff, longest = 0;
  50.  
  51.     err = PR_GetSpecialFD(PR_StandardError);
  52.  
  53.     while (PL_OPT_EOL != (os = PL_GetNextOpt(opt)))
  54.     {
  55.         if (PL_OPT_BAD == os) continue;
  56.         switch (opt->option)
  57.         {
  58.         case 0:       /* Name of file to create */
  59.             filename = opt->value;
  60.             break;
  61.         case 'S':       /* Use sych option on file */
  62.             flags |= PR_SYNC;
  63.             break;
  64.         case 'K':       /* Size of file to write */
  65.             filesize = atoi(opt->value);
  66.             break;
  67.         case 'c':       /* Number of iterations */
  68.             iterations = atoi(opt->value);
  69.             break;
  70.         case 'h':       /* user wants some guidance */
  71.         default:        /* user needs some guidance */
  72.             Help();     /* so give him an earful */
  73.             return 2;   /* but not a lot else */
  74.         }
  75.     }
  76.     PL_DestroyOptState(opt);
  77.  
  78.     file = PR_Open(filename, flags, 0666);
  79.     if (NULL == file)
  80.     {
  81.         PL_FPrintError(err, "Failed to open file");
  82.         return 1;
  83.     }
  84.  
  85.     buffer = (PRUint8*)PR_CALLOC(1024);
  86.     if (NULL == buffer)
  87.     {
  88.         PL_FPrintError(err, "Cannot allocate buffer");
  89.         return 1;
  90.     }
  91.  
  92.     for (index = 0; index < sizeof(buffer); ++index)
  93.         buffer[index] = (PRUint8)index;
  94.  
  95.     for (loops = 0; loops < iterations; ++loops)
  96.     {
  97.         time = PR_IntervalNow();
  98.         for (index = 0; index < filesize; ++index)
  99.         {
  100.             PR_Write(file, buffer, 1024);
  101.         }
  102.         time = (PR_IntervalNow() - time);
  103.  
  104.         total += time;
  105.         if (time < shortest) shortest = time;
  106.         else if (time > longest) longest = time;
  107.         if (0 != PR_Seek(file, 0, PR_SEEK_SET))
  108.         {
  109.            PL_FPrintError(err, "Rewinding file");
  110.            return 1;
  111.         }
  112.     }
  113.  
  114.     total = total / iterations;
  115.     PR_fprintf(
  116.         err, "%u iterations over a %u kbyte %sfile: %u [%u] %u\n",
  117.         iterations, filesize, ((flags & PR_SYNC) ? "SYNCH'd " : ""),
  118.         PR_IntervalToMicroseconds(shortest),
  119.         PR_IntervalToMicroseconds(total),
  120.         PR_IntervalToMicroseconds(longest));
  121.  
  122.     PR_DELETE(buffer);
  123.     rv = PR_Close(file);
  124.     if (PR_SUCCESS != rv)
  125.     {
  126.         PL_FPrintError(err, "Closing file failed");
  127.         return 1;
  128.     }
  129.     rv = PR_Delete(filename);
  130.     if (PR_SUCCESS != rv)
  131.     {
  132.         PL_FPrintError(err, "Deleting file failed");
  133.         return 1;
  134.     }
  135.     return 0;
  136. }
  137.