forked from doublecmd/doublecmd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fbenchmark.pas
192 lines (155 loc) · 4.69 KB
/
fbenchmark.pas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
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, DCOSUtils, 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;
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 Pred(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:= GetTickCountEx;
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:= GetTickCountEx - 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.