/* ************************************************************************* */
/* This is TEST script                                                       */
/*                                                                           */
/*                                                                           */
/*                                                                           */
/*                                                                           */
/*                                                                           */
/*                                                                           */
/* ************************************************************************* */

call RxFuncAdd 'SysSleep', 'RexxUtil', 'SysSleep'

/* Setup counter, used for displaying current status */
iteration = 0

/*-----------------------------------------------------------------------------

   Sleep is used for performance purposes:
   Infinite calculations is causing OS/2 to become very unresponsive to user
   actions when calling this script therefore 1 second rest time is added.

------------------------------------------------------------------------------*/

say "*** Battery Drain utility (CPU use Only) ***"

SleepTime = 1                        /* Default Sleep time is 1 second.       */
WorkTime  = 2                        /* Default work time is 1 second.        */
MaxDelta  = 0.1           
LastStableTime = 0
LastTime = 0
TotalTime = 0
IterationsPerPacket = 50000          /* This value is calculated in 
                                        procedure SpeedDetection()            */
                                   

Call SpeedDetection
IterationsPerPacket = EICount*WorkTime

Say "   * Packet SleepTime      : "||SleepTime
Say "   * Packet WorkTime       : "||WorkTime
Say "   * Iterations per packet : "||IterationsPerPacket
call Charout ,D2C(13)||'Iteration: 0'

/* Infinite loop */
do forever 

  iteration = iteration + 1

  /* call Charout ,D2C(13)||'Iteration: '||iteration*/
  

  StartTime=time('E')
  do counter=1 to IterationsPerPacket

    /* Do some calculations */
    call OneIteration
  end
  EndTime=time('E')
  PacketTime=EndTime-StartTime
  
  /* erase output line and display iteration info*/
  call Charout ,D2C(13)||'                                                     '
  call Charout ,D2C(13)||'Iteration: '||iteration||' Time='||PacketTime
  call SysSleep 1

end
return


/*-------------------------------------------------------------------------
   SUBROUTINES:
-------------------------------------------------------------------------*/

/*-------------------------------------------------------------------------
   This procedure are intended to detect how many iterations can 
   current machine perform in 1 second. Approximately!
-------------------------------------------------------------------------*/
SpeedDetection:
  StartTime=time('E')
  do counter=1 to 10000

    /* Do some calculations */
    call OneIteration
  end  
  EndTime = time('E')
  DetectTime = EndTime-StartTime
  say "Speed Detection Done"
  say "   * Total Time per 10 000 iterations  :"||DetectTime
  EICount = trunc(10000/DetectTime)
  say "   * Estimated iterations per 1 second :"||EICount
return

/*-------------------------------------------------------------------------
   One Iteration, this can be any CPU consumpting operation.
   At this moment: "Very scientific" calculations. 
-------------------------------------------------------------------------*/
OneIteration:
    a=12345.67890
    b=98765.43210
    c=a/b+(a*b)-a
    c=a/b/b/b/b/b/b
return
