Skip to content

Commit

Permalink
ADD: Simple benchmark test
Browse files Browse the repository at this point in the history
  • Loading branch information
alexx2000 committed Dec 16, 2017
1 parent 0e1078a commit 27937c2
Show file tree
Hide file tree
Showing 4 changed files with 281 additions and 1 deletion.
74 changes: 74 additions & 0 deletions src/fbenchmark.lfm
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
object frmBenchmark: TfrmBenchmark
Left = 705
Height = 560
Top = 188
Width = 480
Caption = 'Benchmark'
ClientHeight = 560
ClientWidth = 480
OnClose = FormClose
Position = poOwnerFormCenter
ShowInTaskBar = stAlways
LCLVersion = '1.8.1.0'
object stgResult: TStringGrid
Left = 0
Height = 479
Top = 35
Width = 480
Align = alClient
AutoEdit = False
AutoFillColumns = True
ColCount = 3
Columns = <
item
Title.Caption = 'Hash'
Width = 159
end
item
Title.Caption = 'Time (ms)'
Width = 159
end
item
Title.Caption = 'Speed (MB/s)'
Width = 161
end>
FixedCols = 0
Flat = True
TabOrder = 0
ColWidths = (
159
159
161
)
end
object ButtonPanel: TButtonPanel
Left = 6
Height = 34
Top = 520
Width = 468
OKButton.Name = 'OKButton'
OKButton.DefaultCaption = True
HelpButton.Name = 'HelpButton'
HelpButton.DefaultCaption = True
CloseButton.Name = 'CloseButton'
CloseButton.DefaultCaption = True
CancelButton.Name = 'CancelButton'
CancelButton.DefaultCaption = True
TabOrder = 1
ShowButtons = [pbClose]
end
object lblBenchmarkSize: TLabel
Left = 10
Height = 15
Top = 10
Width = 460
Align = alTop
Alignment = taCenter
BorderSpacing.Left = 10
BorderSpacing.Top = 10
BorderSpacing.Right = 10
BorderSpacing.Bottom = 10
Caption = 'Benchmark data size: %d MB'
ParentColor = False
end
end
7 changes: 7 additions & 0 deletions src/fbenchmark.lrj
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{"version":1,"strings":[
{"hash":77557835,"name":"tfrmbenchmark.caption","sourcebytes":[66,101,110,99,104,109,97,114,107],"value":"Benchmark"},
{"hash":321688,"name":"tfrmbenchmark.stgresult.columns[0].title.caption","sourcebytes":[72,97,115,104],"value":"Hash"},
{"hash":57855833,"name":"tfrmbenchmark.stgresult.columns[1].title.caption","sourcebytes":[84,105,109,101,32,40,109,115,41],"value":"Time (ms)"},
{"hash":125308217,"name":"tfrmbenchmark.stgresult.columns[2].title.caption","sourcebytes":[83,112,101,101,100,32,40,77,66,47,115,41],"value":"Speed (MB/s)"},
{"hash":197921842,"name":"tfrmbenchmark.lblbenchmarksize.caption","sourcebytes":[66,101,110,99,104,109,97,114,107,32,100,97,116,97,32,115,105,122,101,58,32,37,100,32,77,66],"value":"Benchmark data size: %d MB"}
]}
193 changes: 193 additions & 0 deletions src/fbenchmark.pas
Original file line number Diff line number Diff line change
@@ -0,0 +1,193 @@
unit fBenchmark;

{$mode objfpc}{$H+}

interface

uses
Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs,
Grids, Contnrs, ButtonPanel, StdCtrls, uFile, uFileSourceOperation, uOSForms,
uFileSourceCalcChecksumOperation;

type

{ TfrmBenchmark }

TfrmBenchmark = class(TAloneForm)
ButtonPanel: TButtonPanel;
lblBenchmarkSize: TLabel;
stgResult: TStringGrid;
procedure FormClose(Sender: TObject; var CloseAction: TCloseAction);
end;

{ TBenchmarkResult }

TBenchmarkResult = class
Hash: String;
Time: QWord;
Speed: Double;
end;

{ TBenchmarkOperation }

TBenchmarkOperation = class(TFileSourceCalcChecksumOperation)
private
FFiles: TFiles;
FBuffer: TBytes;
FOwner: TCustomForm;
FSpeedResult: TObjectList;
FStatistics: TFileSourceCalcChecksumOperationStatistics;
protected
procedure MainExecute; override;
procedure OnBenchmarkStateChanged(Operation: TFileSourceOperation;
AState: TFileSourceOperationState);
public
constructor Create(TheOwner: TCustomForm); reintroduce;
destructor Destroy; override;
end;

implementation

uses
ISAAC, uFileSystemFileSource, uHash, uGlobs, uDCUtils;

const
cSize = 1024 * 1024 * 256;

function CompareFunc(Item1, Item2: Pointer): Integer;
begin
if TBenchmarkResult(Item1).Time = TBenchmarkResult(Item2).Time then
Result:= 0
else if TBenchmarkResult(Item1).Time < TBenchmarkResult(Item2).Time then
Result:= -1
else begin
Result:= +1;
end;
end;

{ TfrmBenchmark }

procedure TfrmBenchmark.FormClose(Sender: TObject; var CloseAction: TCloseAction);
begin
CloseAction:= caFree;
end;

{ TBenchmarkOperation }

procedure TBenchmarkOperation.MainExecute;
var
ASize: Int64;
AHash: String;
ACount: Integer;
ARandom: isaac_ctx;
ABufferSize: Integer;
Context: THashContext;
Index: THashAlgorithm;
AStart, AFinish: QWord;
AResult: TBenchmarkResult;
begin
ABufferSize := gHashBlockSize;
SetLength(FBuffer, ABufferSize);
isaac_init(ARandom, Int32(GetTickCount64));
isaac_read(ARandom, @FBuffer[0], ABufferSize);
ASize:= (cSize div ABufferSize) * ABufferSize;
FStatistics.TotalFiles := (Length(HashName) - 1);
FStatistics.TotalBytes:= ASize * FStatistics.TotalFiles;

for Index := Low(THashAlgorithm) to High(THashAlgorithm) do
begin
if Index = HASH_SFV then Continue;

with FStatistics do
begin
CurrentFile := HashName[Index];
CurrentFileTotalBytes := ASize;
CurrentFileDoneBytes := 0;
end;

UpdateStatistics(FStatistics);

AStart:= GetTickCount64;
HashInit(Context, Index);

while FStatistics.CurrentFileDoneBytes < ASize do
begin
HashUpdate(Context, FBuffer[0], ABufferSize);

with FStatistics do
begin
CurrentFileDoneBytes := CurrentFileDoneBytes + ABufferSize;
DoneBytes := DoneBytes + ABufferSize;

UpdateStatistics(FStatistics);
end;

CheckOperationState; // check pause and stop
end;

HashFinal(Context, AHash);
AFinish:= GetTickCount64 - AStart;

Inc(FStatistics.DoneFiles);
UpdateStatistics(FStatistics);

AResult:= TBenchmarkResult.Create;

AResult.Hash:= HashName[Index];
AResult.Time:= AFinish;
AResult.Speed:= (cSize / (1024 * 1024)) / (AFinish / 1000);

FSpeedResult.Add(AResult);
end;
FSpeedResult.Sort(@CompareFunc);
end;

procedure TBenchmarkOperation.OnBenchmarkStateChanged(
Operation: TFileSourceOperation; AState: TFileSourceOperationState);
var
Index: Integer;
AValue: TBenchmarkResult;
begin
if (AState = fsosStopped) and (Operation.Result = fsorFinished) then
begin
with TfrmBenchmark.Create(FOwner) do
begin
stgResult.BeginUpdate;
stgResult.RowCount:= FSpeedResult.Count + 1;
try
for Index:= 0 to FSpeedResult.Count - 1 do
begin
AValue:= TBenchmarkResult(FSpeedResult[Index]);
stgResult.Cells[0, Index + 1]:= AValue.Hash;
stgResult.Cells[1, Index + 1]:= IntToStr(AValue.Time);
stgResult.Cells[2, Index + 1]:= FloatToStrF(AValue.Speed, ffFixed, 15, 3);
end;
FreeAndNil(FSpeedResult);
lblBenchmarkSize.Caption:= Format(lblBenchmarkSize.Caption, [cSize div (1024 * 1024)]);
finally
stgResult.EndUpdate();
end;
Show;
end;
end;
end;

constructor TBenchmarkOperation.Create(TheOwner: TCustomForm);
begin
FOwner:= TheOwner;
inherited Create(TFileSystemFileSource.GetFileSource, FFiles, EmptyStr, EmptyStr);
AddStateChangedListener([fsosStopped], @OnBenchmarkStateChanged);
FSpeedResult:= TObjectList.Create;
Mode:= checksum_calc;
end;

destructor TBenchmarkOperation.Destroy;
begin
FSpeedResult.Free;
inherited Destroy;
end;

{$R *.lfm}

end.

8 changes: 7 additions & 1 deletion src/umaincommands.pas
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,7 @@ TMainCommands = class(TComponent{$IF FPC_FULLVERSION >= 020501}, IFormCommands
procedure cm_ConfigHotKeys(const {%H-}Params: array of string);
procedure cm_ExecuteScript(const {%H-}Params: array of string);
procedure cm_FocusSwap(const {%H-}Params: array of string);
procedure cm_Benchmark(const {%H-}Params: array of string);

// Internal commands
procedure cm_ExecuteToolbarItem(const Params: array of string);
Expand All @@ -379,7 +380,7 @@ implementation
uHotDir, DCXmlConfig, dmCommonData, fOptionsFrame, foptionsDirectoryHotlist,
fOptionsToolbar, fMainCommandsDlg, uConnectionManager, fOptionsTabs, fOptionsFavoriteTabs,
fTreeViewMenu, fOptionsTreeViewMenu, fOptionsTreeViewMenuColor, uArchiveFileSource,
fOptionsFileSearch, fOptionsHotKeys
fOptionsFileSearch, fOptionsHotKeys, fBenchmark
{$IFDEF COLUMNSFILEVIEW_VTV}
, uColumnsFileViewVtv
{$ELSE}
Expand Down Expand Up @@ -4911,6 +4912,11 @@ procedure TMainCommands.cm_FocusSwap(const Params: array of string);
end;
end;

procedure TMainCommands.cm_Benchmark(const Params: array of string);
begin
OperationsManager.AddOperation(TBenchmarkOperation.Create(frmMain));
end;

{ TMainCommands.cm_AddNewSearch }
procedure TMainCommands.cm_AddNewSearch(const Params: array of string);
var
Expand Down

0 comments on commit 27937c2

Please sign in to comment.