! Crude Filestore benchmark test

%include "inc:util.imp"
%include "inc:fs.imp"
%begin
%integer base,cpu0,cpu1,cpu2,len,base2,cpu3,blks,i
%string(255) param
%integerarray cpu1a,cpu2a,cpu3a(1:50)

%routine b1(%string(255)f, %integer burst, %integername l)
!Haul in a big file as efficiently as possible. Routine derived from CONNECT
!but ditches received data
@16_35C4 %short  uno
%integer xno=0,fsize=0,fpos=0
%integer i,j,len,blocks
%bytearray data(0:599)
%string(255)fscomm
  
  %routine closefile
    fscomm = "K".tostring(xno+'0').tostring(nl)
cpu3=cputime
    etherwrite(fsport,charno(fscomm,1),length(fscomm))
    len = etherread(fsport,charno(fscomm,1),255)
cpu3=cputime-cpu3
  %end

  %integerfn hdhex(%integername p)
  %integer k,v=0
    %cycle
      p = p+1;  k = byteinteger(p-1)-'0'
      %result = v %if k < 0
      v = v<<4+k
    %repeat
  %end

  l = 0
  fscomm = "S".tostring(uno+'0').f.tostring(nl)
cpu1=cputime
  etherwrite(fsport,charno(fscomm,1),length(fscomm))
  len = etherread(fsport,charno(fscomm,1),255)
cpu1=cputime-cpu1
  %if charno(fscomm,1) = '-' %start; !filestore error message
     cpu1=-1; cpu2=-1; cpu3=-1
     %return
  %finish
  i = addr(fscomm)+1
  xno = hdhex(i); blocks = hdhex(i); fsize = hdhex(i)
  fsize = blocks<<9-fsize
  l = fsize
  %returnif xno=0
  fscomm = "X".tostring(xno+'0'). %c
  tostring(burst>>4+'0').tostring(burst&15+'0').tostring(nl)
  !<burst> blocks at a time
blks=0
cpu2=cputime
  %cycle
    etherwrite(fsport,charno(fscomm,1),length(fscomm))
    %for j = 1,1,burst %cycle
      len = etherread(fsport,data(0),515)
      blks=blks+1
      i = addr(data(0))
      len = hdhex(i)
      blocks = 0 %andexitunless len=512
    %repeat
  %repeatuntil blocks=0
  closefile
cpu2=cputime-cpu2
%end

%routine do b1(%string (255) filename, %integer burst, maxtimes)
   %integer benchno,i

   %for benchno=1,1,maxtimes %cycle
      %stop %if testsymbol=' '
      b1(filename, burst, len)
      %stop %if cpu1<0
      printstring(time." B1: Readsq "); write(blks, 6); printstring(" blks: ")
      cpu1a(benchno)=cpu1; cpu2a(benchno)=cpu2; cpu3a(benchno)=cpu3
      print(cpu1/1000, 3,3); printsymbol('/'); print(cpu2/1000, 3,3)
      printsymbol('/'); print(cpu3/1000, 3,3)
      printstring(" Secs."); newline
   %repeat

   newline
   printstring("Averages: ")
   cpu1=0; cpu2=0; cpu3=0
   %for i=1,1,maxtimes %cycle
      cpu1=cpu1+cpu1a(i); cpu2=cpu2+cpu2a(i); cpu3=cpu3+cpu3a(i)
   %repeat
   print(cpu1/(maxtimes*1000), 3,3); printsymbol('/')
   print(cpu2/(maxtimes*1000), 3,3); printsymbol('/')
   print(cpu3/(maxtimes*1000), 3,3); newline

%end


%routine b2
   %string (255) fscomm,res
   fscomm = "G00".tostring(nl)
cpu1=cputime
   etherwrite(fsport,charno(fscomm,1),length(fscomm))
   length(res)=etherread(fsport,charno(res,1),128)
cpu1=cputime-cpu1
%end

%routine do b2(%integer maxtimes)
   %integer i,n
   %integerarray cpu4a(-1:101)

   printstring(time." B2: Time Rq"); write(maxtimes, 3); printstring(" requests")
   newline
   %for i=-1,1,101 %cycle; cpu4a(i)=0; %repeat
   %for i=1,1,maxtimes %cycle
      %stop %if testsymbol = ' '
      b2
      cpu1=-1 %if cpu1<0
      cpu1=101 %if cpu1>100
      cpu4a(cpu1)=cpu4a(cpu1)+1
   %repeat

   n=0
   printstring("Histogram of response times in milliseconds"); newlines(2)
   
   %if cpu4a(-1)>0 %then write(cpu4a(-1), -1) %and printstring(" <0. ")
   %if cpu4a(101)>0 %then write(cpu4a(101), -1) %and printstring(" >100.")
   %for i=0,1,99 %cycle
      %if n=0 %start
         newline; write(i, 3); printstring(":")
      %finishelse space
      write(cpu4a(i), 3)
      n=n+1; n=0 %if n=10
   %repeat
   newline

%end


set terminal mode(8)
param = cli param
%if param # "" %then openoutput(1, param) %and selectoutput(1)

printstring(date."  "); phex2(ldte); printstring("->")
phex2(rdte); newlines(2)
cpu0=cputime

do b2(100)
do b1("bench:minifile.dat", 1, 20)
do b1("bench:bigfile.dat", 257, 5)
do b1("bench:bigfile.dat", 1, 5)

cpu0=cputime-cpu0
newlines(2)
printstring(date."  End of tests - total time "); print(cpu0, 4,2)
printstring(" Secs."); newline

close output %if param # ""
%endofprogram
