(setq hasMilliSecs (/= (getvar "MILLISECS") nil))
(setvar "LOGFILEMODE" 0)

(if (not $BenchTempFolder) (setq $BenchTempFolder ""))

(setq _$localtestfile (strcat $BenchTempFolder "readwrite-char.log"))

(defun t_writechar ( count fptr / time1 time2 file )
  (setq file (open _$localtestfile "w"))
  ;;-----------------------------
  (setq time1 (getvar "MILLISECS"))
  (if (not time1) (setq time1 (getvar "TDUSRTIMER")))
  (if (not time1) (setq time1 0.0))

  (repeat (* count count)
    (write-char 48 file) ; 0
    (write-char 49 file) ; 1
    (write-char 50 file) ; 2
    (write-char 51 file) ; 3
    (write-char 52 file) ; 4
    (write-char 53 file) ; 5
    (write-char 54 file) ; 6
    (write-char 55 file) ; 7
    (write-char 56 file) ; 8
    (write-char 57 file) ; 9
    (write-char 10 file) ; 0x0A
  )
  (write-line "===== EOF =====" file)

  (setq time2 (getvar "MILLISECS"))
  (if (not time2) (setq time2 (getvar "TDUSRTIMER")))
  (if (not time2) (setq time2 0.0))
  ;;-----------------------------
  (close file)

  (setq bench$seconds (if hasMilliSecs (* 0.001 (- time2 time1)) (* 86400.0 (- time2 time1))))
  (princ (strcat "* WRITE-CHAR (File) : " (rtos bench$seconds 2 3) "\n"))
  (if fptr
    (princ (strcat "* WRITE-CHAR (File) : " (rtos bench$seconds 2 3) "\n") fptr)
  )
  bench$seconds
)

(defun t_readchar ( count fptr / time1 time2 file ctr )
  (setq file (open _$localtestfile "r"))
  (if (not file) (exit))
  (setq ctr 0)
  ;;-----------------------------
  (setq time1 (getvar "MILLISECS"))
  (if (not time1) (setq time1 (getvar "TDUSRTIMER")))
  (if (not time1) (setq time1 0.0))

  (while (read-char file)
    (setq ctr (1+ ctr))
  )

  (setq time2 (getvar "MILLISECS"))
  (if (not time2) (setq time2 (getvar "TDUSRTIMER")))
  (if (not time2) (setq time2 0.0))
  ;;-----------------------------
  (close file)

  (setq bench$seconds (if hasMilliSecs (* 0.001 (- time2 time1)) (* 86400.0 (- time2 time1))))
  (princ (strcat "* READ-CHAR (File) : " (rtos bench$seconds 2 3) "\n"))
  (if fptr
    (princ (strcat "* READ-CHAR (File) : " (rtos bench$seconds 2 3) "\n") fptr)
  )
  bench$seconds
)

;; to increase/decrease the number of bench loops : > 1 increase < 1 decrease
(if (not $benchcyclesfactor) (setq $benchcyclesfactor 1))

;; to run this loaded test manually
(defun C:Run ()
  (setvar "cmdecho" 0)
  (print)
  (t_writechar (* 300 $benchcyclesfactor) nil)
  (t_readchar  (* 300 $benchcyclesfactor) nil)
  (setvar "cmdecho" 1)
  (princ)
)
