home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: SysTools / SysTools.zip / cdtest10.zip / testspeed.cpp < prev    next >
C/C++ Source or Header  |  1999-01-01  |  6KB  |  205 lines

  1. #define INCL_PM
  2. #define INCL_DOS
  3. #include <os2.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6. #include <stdio.h>
  7. #include <time.h>
  8. #include <conio.h>
  9. #include <float.h>
  10.  
  11. #include "miscsam.h"
  12. #include "readcd.h"
  13.  
  14. void displayHelp(char *program)
  15. {
  16.     printf("CD-ROM Speed Tester 1.0  (C) 1998 Samuel Audet <guardia@cam.org>\n"
  17.            "\n"
  18.            "Syntax:\n"
  19.            "   %s <cd drive letter> [/c <bogus cache size in kB>] [/t <track,track,...>] [/s <fake cd writing speed>]\n"
  20.            "\n"
  21.            "The default bogus cache size is 4096 kB.  "
  22.            "By default, all tracks are tested.\n"
  23.            "The default fake CD writing speed is 4x.\n",program);
  24. }
  25.  
  26. int main(int argc, char *argv[])
  27. {
  28.    CD_drive drive;
  29.    ULONG firstsector, lastsector, sector;
  30.    CDREADLONGDATA data[27];
  31.    double *timeTable;
  32.    double lowestSpeed;
  33.    int sizeTable;
  34.    int farpos, curpos, i, e;
  35.    char *driveLetter = NULL;
  36.    short cacheSize = 4096, cacheStatus;
  37.    double cdSpeed = 4.0;
  38.    double lastTime, curTime, timer;
  39.    char tracksToTest[256];
  40.    int hitBottom; /* how many times did the cache empty */
  41.  
  42.    /* all tracks */
  43.    for(i = 0; i < 256; i++)
  44.       tracksToTest[i] = i+1;
  45.  
  46.    if(argc == 1)
  47.    {
  48.       displayHelp(argv[0]);
  49.       return 0;
  50.    }
  51.  
  52.  
  53.    for(i = 1; i < argc; i++)
  54.    {
  55.       if(argv[i][0] == '/' || argv[i][0] == '-')
  56.       {
  57.          switch(argv[i][1])
  58.          {
  59.             case 'c': case 'C':
  60.                cacheSize = atoi(argv[++i]); break;
  61.             case 't': case 'T':
  62.             {
  63.                char *comma = argv[++i];
  64.                e = 0;
  65.                do 
  66.                {
  67.                   tracksToTest[e++] = atoi(comma);
  68.                } while((comma = strchr(comma,',')+1) != (char*)1);
  69.                tracksToTest[e] = 0;
  70.                break;
  71.             }
  72.             case 's': case 'S':
  73.                cdSpeed = atoi(argv[++i]); break;
  74.             case '?':
  75.             default:
  76.                displayHelp(argv[0]); return 0;
  77.          }
  78.       }
  79.       else
  80.          driveLetter = argv[i];
  81.    }
  82.  
  83.    if(!driveLetter)
  84.    {
  85.       fprintf(stderr,"Error: no drive letter specified.");
  86.       return 1;
  87.    }
  88.  
  89.    printf("Faking a %d kB cache writing at %#.3gx.\n",cacheSize,cdSpeed);
  90.  
  91.    sizeTable = cacheSize*1024/2352/27;
  92.    timeTable = (double*)alloca(sizeTable*sizeof(timeTable[0]));
  93.  
  94.    drive.open(driveLetter);
  95.    drive.readCDInfo();
  96.    drive.fillTrackInfo();
  97.  
  98.    for(i = 0; i < drive.getCount(); i++)
  99.    {
  100.       /* finding if we should test this track */
  101.       bool found = false;
  102.       for(e = 0; e < 256 && tracksToTest[e]; e++)
  103.       {
  104.          if(drive.getTrackInfo(i)->number == tracksToTest[e])
  105.             found = true;
  106.       }
  107.       if(!found)
  108.          continue;
  109.  
  110.       timeTable[0] = (double) clock()/CLOCKS_PER_SEC;
  111.       firstsector = sector = CD_drive::getLBA(drive.getTrackInfo(i)->start);
  112.       lastsector = CD_drive::getLBA(drive.getTrackInfo(i)->end);
  113.       curpos = farpos = 0;
  114.       lowestSpeed = _INF;
  115.       cacheStatus = 0;
  116.       lastTime = (double) clock()/CLOCKS_PER_SEC;
  117.       hitBottom = 0;
  118.  
  119.       /* spin up the drive */
  120.       drive.readSectors(data,1,sector);
  121.       DosSleep(2000);
  122.  
  123.       printf("Testing track %d...\n",drive.getTrackInfo(i)->number);
  124.  
  125.       while(sector < lastsector-1)
  126.       {
  127.          int timeLength; /* length between two times */
  128.          double speed, xspeed;
  129.          int readLength = min(27,lastsector-sector-1); /* amount of sectors to read */
  130.  
  131.          curTime = (double) clock()/CLOCKS_PER_SEC;
  132.          timer += (curTime - lastTime);
  133.  
  134.          if(timer > 0.5 || sector+27 >= lastsector-1)
  135.          {
  136.             if(_kbhit() && _getch() == 27)
  137.             {
  138.                i = drive.getCount();
  139.                break;
  140.             }
  141.  
  142.             fflush(stdin);
  143.  
  144.             printf("Reading %6.1d -> %d ",sector, lastsector-1);
  145.          }
  146.          drive.readSectors(data,readLength,sector);
  147.          sector += readLength;
  148.  
  149.          curpos++;
  150.          if(curpos >= sizeTable)
  151.             curpos = 0;
  152.  
  153.          if(curpos == farpos)
  154.          {
  155.             farpos++;
  156.             if(farpos >= sizeTable)
  157.                farpos = 0;
  158.          }
  159.  
  160.  
  161.          if(curpos > farpos)
  162.             timeLength = (curpos-farpos)*27*2352;
  163.          else
  164.             timeLength = (sizeTable-farpos+curpos)*27*2352;
  165.  
  166.          timeTable[curpos] = (double) clock()/CLOCKS_PER_SEC;
  167.  
  168.          speed = timeLength/(timeTable[curpos]-timeTable[farpos])/1024;
  169.          xspeed = speed /150;
  170.  
  171.          if(timer > 0.5 || sector >= lastsector-1)
  172.             printf(" Avg Speed: %#8.6g kB/s == %#.3gx ", speed, xspeed);
  173.  
  174.  
  175.          cacheStatus += readLength * 2352/1024;
  176.          if(sector-firstsector > sizeTable*27)
  177.             cacheStatus -= (curTime-lastTime) * cdSpeed * 150;
  178.          /* truncate the cache data if it's too big */
  179.          if(cacheStatus > cacheSize)
  180.             cacheStatus = cacheSize;
  181.          else if(cacheStatus <= 0)
  182.          {
  183.             cacheStatus = 0;
  184.             hitBottom++;
  185.          }
  186.          if(timer > 0.5 || sector >= lastsector-1)
  187.             printf(" Cache: %5.1d kB  \r",cacheStatus);
  188.  
  189.  
  190.          if(speed < lowestSpeed && sector-firstsector > sizeTable*27)
  191.             lowestSpeed = speed;
  192.  
  193.          if(timer > 0.5  || sector >= lastsector-1)
  194.             fflush(stdout);
  195.          lastTime = curTime;
  196.          if(timer > 0.5) timer = 0.0;
  197.       }
  198.       printf("\nLowest Speed: %#8.6g kB/s == %#.3gx  Hit Bottom: %d times\n",lowestSpeed,lowestSpeed/150, hitBottom);
  199.    }
  200.  
  201.    drive.close();
  202.  
  203.    return 0;
  204. }
  205.