program FPCBenchmark;
{$ifdef fpc}{$mode delphi}{$endif}
{$r-}
uses
SysUtils,
FastBase64,
mormot.core.buffers;
{$define mormotFirst} //-- by disable or enable swap test order
{$define measure_2nd_Run} //-- for fast option measure 1st or 2nd run
{$define fastFastBase64} //-- make fast FastBase64 1st run 4.5, 2nd run 5.5 GB/s (disabled 2.7 GB/s)
{$define fastMormot} //-- make fast Mormot 1st run 4.4, 2nd run 5.5 GB/s (disabled 2.7 GB/s)
function toSeconds (d : double):double;
var a, b : double;
hh,mm,ss,ms : word;
begin
DeCodeTime (d,Hh,MM,SS,MS);
if (hh=0) and (mm=0) and (ss=0) and (ms=0) then hh:=$ffff; //-- avoid division by zero
toSeconds:=hh*60*60+mm*60+ss+ms/1000;
end;
procedure activateBuffer(destbuf, buf:pansichar; len : qword);
var k : qword;
begin
if len>0 then
for k:=0 to len-1 do
begin
//-- do whatever (but do something)
destbuf[k]:=char(byte(buf[k])+byte(k));
end;
end;
procedure LaregeBench;
var buf : PByte;
destBuf : PByte;
i: qword;
j: qword;
base64Buf : RawByteString;
sTim, eTim : double;
const cLargeBuf = 512*1024*1024;
cNumRounds = 1; ///---- should be "one"
cLargeBuf2 = ((cLargeBuf + 2) div 3) * 4;
begin
//RandSeed := 79929; // just a seed to get the same results for debugging,...
Randomize;
getmem(buf,cLargeBuf);
getmem(destbuf,cLargeBuf2);
writeln('Generating data..');
for j := 0 to cLargeBuf - 1 do
buf[j] := Random(255);
SetLength(base64Buf, ((cLargeBuf + 2) div 3) * 4);
writeln('Testing..');
//-- test boosters (or not)
{$ifdef fastMormot}
activateBuffer( PAnsiChar(destbuf), @buf[0] ,cLargeBuf);
{$endif}
{$ifdef fastFastBase64}
activateBuffer( PAnsiChar(base64Buf), @buf[0], cLargeBuf);
{$endif}
//-- booster for 2nd run
{$ifdef measure_2nd_Run}
{$ifdef fastMormot}
mormot.core.buffers.Base64Encode( PAnsiChar(destbuf), @buf[0] ,cLargeBuf);
{$endif}
{$ifdef fastFastBase64}
FastBase64.Base64Encode(PAnsiChar(base64Buf), @buf[0], cLargeBuf);
{$endif}
{$endif}
//-- test one
stim:=now();
for i := 1 to cNumRounds do
begin
{$ifdef mormotFirst}
mormot.core.buffers.Base64Encode( PAnsiChar(destbuf), @buf[0] ,cLargeBuf);
{$else}
FastBase64.Base64Encode(PAnsiChar(base64Buf), @buf[0], cLargeBuf );
{$endif}
end;
etim:=now();
{$ifdef mormotFirst}
Writeln('mormot encoding took: ',toSeconds(etim-stim):12:3,' sec');
{$ifdef fastMormot}write('Fast ');{$ifdef measure_2nd_Run}write('2nd run ');{$else}write('1st run ');{$endif}{$endif}
Writeln(Format('mormot GB/s: %.3f', [ (cLargeBuf*cNumRounds/1024/1024/1024)/toSeconds(etim-stim) ]));
{$else}
Writeln('FastBase64 encoding took: ',toSeconds(etim-stim):12:3,' sec');
{$ifdef fastFastBase64}write('Fast ');{$ifdef measure_2nd_Run}write('2nd run ');{$else}write('1st run ');{$endif}{$endif}
Writeln(Format('FastBase64 GB/s: %.3f', [ (cLargeBuf*cNumRounds/1024/1024/1024)/ toSeconds(etim-stim) ]));
{$endif}
//-- test two
stim:=now();
for i := 1 to cNumRounds do
begin
{$ifndef mormotFirst}
mormot.core.buffers.Base64Encode( PAnsiChar(destbuf), @buf[0] ,cLargeBuf);
{$else}
FastBase64.Base64Encode(PAnsiChar(base64Buf), @buf[0], cLargeBuf );
{$endif}
end;
etim:=now();
{$ifndef mormotFirst}
Writeln('mormot encoding took: ',toSeconds(etim-stim):12:3,' sec');
{$ifdef fastMormot}write('Fast ');{$ifdef measure_2nd_Run}write('2nd run ');{$else}write('1st run ');{$endif}{$endif}
Writeln(Format('mormot GB/s: %.3f', [ (cLargeBuf*cNumRounds/1024/1024/1024)/toSeconds(etim-stim) ]));
{$else}
Writeln('FastBase64 encoding took: ',toSeconds(etim-stim):12:3,' sec');
{$ifdef fastFastBase64}write('Fast ');{$ifdef measure_2nd_Run}write('2nd run ');{$else}write('1st run ');{$endif}{$endif}
Writeln(Format('FastBase64 GB/s: %.3f', [ (cLargeBuf*cNumRounds/1024/1024/1024)/ toSeconds(etim-stim) ]));
{$endif}
freemem(buf,cLargeBuf);
freemem(destbuf,cLargeBuf2);
end;
begin
LaregeBench;
readln;
end.