!******************************************************************************!
! benchmark_truncate: Compares the benchmark waveform-based file I/O performance
! with a TruncateBLUE environment option set to OFF, versus the same file I/O
! performance with TruncateBLUE environment option set to ON.
! This script has four "modes", represented by the S:MODE["ALL"] argument, aimed
! at testing file I/O performance under different circumstances.
! The "SEQUENTIAL" mode starts with the largest specified file size / number of
! wave elements (MAXELEM)_ and progressively creates smaller and smaller shapes 
! until it reaches the MINELEM value.
! The "REVERSE" mode starts with MINELEM and progressively creates larger shapes
! until it reaches MAXELEM.
! The "RANDOM" mode uses a random value multiplied by the MAXELEM in order to
! test random file sizes that do not follow any particular pattern.
! The "ALL" mode will run all tests in order (SEQUENTIAL->REVERSE->RANDOM) and
! then repeat for the given number of tests (LOOPCOUNT).
! Arguments:
! Z:TRUNC [TRUE]   - whether to perform the benchmark with TruncateBLUE ON or oFF
! S:MODE ["ALL"]   - a String argument for selecting the appropriate benchmark to run
!                  - options are "ALL", "SEQUENTIAL", "REVERSE", and "RANDOM
! S:SHAPE ["SIN"]  - the wave-based shape to use
! L:LOOPCOUNT [10] - the number of benchmark iterations to perform
! D:MAXELEM[100M]  - the largest size waveform shape to create
! D:MINELEM[100M]  - the smallest size waveform shape to create
!
!
! @bug      2175  DATAFILE: Should use TRUNCATE by default
! @author:  Chris Simons
! @version: $Id: benchmark_truncate.mm,v 1.2 2011/02/11 16:44:49 crs Exp $
!******************************************************************************!
startmacro Z:TRUNC[TRUE] S:MODE["ALL"] S:SHAPE["SIN"] L:LOOPCOUNT[10] D:MAXELEM[100M] D:MINELEM[1M]

  ! constants
  set l:divisor 10
  set l:multiplier 10
  set l:randomizer 100
  set l:SBSWITCHSIZE 500M
  
  ENV GET IOOPTIONS origIOOptions
	if TRUNC istrue then
	  ENV SET IOOPTIONS +TruncateBLUE
  else
	  ENV SET IOOPTIONS -TruncateBLUE
	endif

  ! start the timer
  timer set 

  do count 1 LOOPCOUNT
    if MODE eqs "ALL" then
      call all
    elseif MODE eqs "SEQUENTIAL" then
      call sequential
    elseif MODE eqs "REVERSE" then
      call reverse
    elseif MODE eqs "RANDOM" then
      call random
    else
      call all
    endif
  enddo
	
  ! store the elapsed time
  timer elapse wallTime

  ! print the elapsed time in a friendly manner
  say "Elapsed Time (Wall Clock) after ^count Loops: ^wallTime seconds"

	! Restore original IOOPTIONS
	ENV SET IOOPTIONS origIOOptions


endmacro

!****************************************************************************!
! run the sequential test case
!****************************************************************************!
procedure sequential
  set fname_test "junksequential.tmp"
  set CURRENTSIZE MAXELEM
  ! decrement the waveform by divisor
  while CURRENTSIZE gt MINELEM
    ! added to use FORM=SB instead of default FORM=SF when file size is gt 100M
    if CURRENTSIZE gt SBSWITCHSIZE then
      wave fname_test form=SB elem=currentsize shape=SHAPE
    else
      wave fname_test form=SF elem=currentsize shape=SHAPE
    endif
    calc CURRENTSIZE currentsize divisor div
  endwhile

  ! cleanup
  erase/warn=no fname_test
return

!****************************************************************************!
! run the sequential test case
!****************************************************************************!
procedure reverse
  set fname_test "junkreverse.tmp"

  set CURRENTSIZE MINELEM
  
  ! decrement the waveform by divisor
  while CURRENTSIZE lt MAXELEM
    ! added to use FORM=SB instead of default FORM=SF when file size is gt 100M
    if CURRENTSIZE gt SBSWITCHSIZE then
      wave fname_test form=SB elem=currentsize shape=SHAPE
    else
      wave fname_test form=SF elem=currentsize shape=SHAPE
    endif
    calc CURRENTSIZE currentsize multiplier mul
  endwhile

  ! cleanup
  erase/warn=no fname_test
return

!****************************************************************************!
! run the random test case
!****************************************************************************!
procedure random

  set fname_test "junkrandom.tmp"

  set l:counter 0
  set CURRENTSIZE minelem
  set LASTSIZE currentsize    ! to add to the new CURRENTSIZE after each run
  set MAXRANDOM maxelem
  set MINRANDOM minelem

  ! randomly create waveforms given the latest random generated value
  ! multiplied by the last file size, divided by the divisor
  !while CURRENTSIZE le MAXRANDOM and CURRENTSIZE ge MINRANDOM
  while counter le 10
    calc CURRENTSIZE random
    calc CURRENTSIZE currentsize maxrandom mul
    ! added to use FORM=SB instead of default FORM=SF when file size is gt 100M
    if CURRENTSIZE gt SBSWITCHSIZE then
      wave fname_test form=SB elem=currentsize shape=SHAPE
    else
      wave fname_test form=SF elem=currentsize shape=SHAPE
    endif
  
    ! decrease the size of MAXRANDOM for the next iteration
    calc MAXRANDOM maxrandom 2 div

    calc counter counter 1 add
    
  endwhile

  ! cleanup
  erase/warn=no fname_test

return

!****************************************************************************!
! run all test cases in order
!****************************************************************************!
procedure all
  call sequential
  call reverse
  call random
return 
