home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / C / Snippets / FileWrite / FileWriteTest.c
Encoding:
C/C++ Source or Header  |  1994-05-06  |  2.6 KB  |  107 lines  |  [TEXT/R*ch]

  1. // File Write Test, Mac Version
  2. //  requires 1.1 MB RAM
  3. //  does not make use of Asynch Write w/ completion
  4. //  06may94  bhamlin@netcom.com (Brian Hamlin)
  5.  
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include <time.h>
  9.  
  10. #include <Files.h>
  11.  
  12. #define DEFAULT_DIR 0
  13.  
  14. #define MB         (1)
  15. #define TEST_BLK     (0x100000)
  16. #define MIN_WRITE_SIZE  (0x100)
  17.  
  18. //char memory[1024*1024];
  19.  
  20. main()
  21. {
  22.   register char i;
  23.   register long j,n;
  24.   //FILE *handle;
  25.   double mytime, transfer;
  26.   clock_t start, end;
  27.   
  28.   HParamBlockRec  PBHRec;
  29.   OSErr      err;
  30.   short      refNum;
  31.   Ptr        mBuff;
  32.   
  33.   printf("Disk Test, Mac Version\n");  fflush( stdout);
  34.  
  35.   mBuff = NewPtr( TEST_BLK);
  36.   if ( mBuff == 0 || MemError() )
  37.   {
  38.     printf("can't allocate buffer\n");
  39.     exit(-1);
  40.   }
  41.   
  42.   n = MIN_WRITE_SIZE;
  43.   for(i=0; i<21-8; i++)
  44.   {
  45.     PBHRec.fileParam.ioCompletion = 0;
  46.     PBHRec.fileParam.ioNamePtr = "\ptest.tmp";
  47.     PBHRec.fileParam.ioVRefNum = DEFAULT_DIR;
  48.     PBHRec.fileParam.ioDirID = DEFAULT_DIR;
  49.     err = PBHCreate (&PBHRec, false);
  50.     if ( err != 0  && err != dupFNErr)
  51.     {  // ok if tmp file already exists
  52.       printf("can't create file\n");
  53.       exit(-1);
  54.     }
  55.  
  56.         // if((handle = fopen("test.tmp","wb")) == NULL)
  57.  
  58.     // open file
  59.     PBHRec.ioParam.ioCompletion = 0;
  60.     PBHRec.ioParam.ioNamePtr = "\ptest.tmp";
  61.     PBHRec.ioParam.ioVRefNum = DEFAULT_DIR;
  62.     PBHRec.ioParam.ioPermssn = fsWrPerm;
  63.     PBHRec.ioParam.ioMisc = 0;
  64.     PBHRec.fileParam.ioDirID = DEFAULT_DIR;    // <- note union use
  65.     err = PBHOpen( &PBHRec, false);
  66.     if ( err != 0)
  67.     {
  68.       printf("can't open file\n");
  69.       exit(-1);
  70.     }
  71.     refNum = PBHRec.ioParam.ioRefNum;
  72.         
  73.         // set-up for Write
  74.     PBHRec.ioParam.ioCompletion = 0;
  75.     PBHRec.ioParam.ioRefNum = refNum;
  76.     PBHRec.ioParam.ioBuffer = mBuff;
  77.     PBHRec.ioParam.ioReqCount = n;
  78.     PBHRec.ioParam.ioPosMode = fsFromStart;
  79.     PBHRec.ioParam.ioPosOffset = 0;
  80.  
  81.         //start = clock();
  82.         start = TickCount();
  83.     for ( j=0; j < (MB*TEST_BLK)/n; j++)
  84.       err = PBWriteSync( (ParmBlkPtr)&PBHRec);
  85.         
  86.         //for(j=0; j < ((MB*1024*1024)/n); j++)
  87.         //{
  88.         //  fwrite(memory,sizeof(char),n,handle);
  89.         //}
  90.  
  91.         end = TickCount();
  92.         //end = clock();
  93.         mytime = ((double)(end - start)) / ((double) 60);
  94.         //mytime = ((double)(end - start)) / ((double) CLOCKS_PER_SEC);
  95.         transfer = (double)(MB*1024)/mytime;
  96.         printf("transferrate to harddisk = %f KByte/sec with blocksize %ld 
  97. Bytes\n",transfer,n);
  98.  
  99.         //fclose(handle);
  100.     PBHRec.ioParam.ioCompletion = 0;
  101.     PBHRec.ioParam.ioRefNum = refNum;
  102.     err = PBCloseSync( (ParmBlkPtr)&PBHRec);
  103.  
  104.         n = n * 2;
  105.   }
  106. }
  107.