Skip to content

Commit 648e739

Browse files
committed
Add Kdf benchmarks
1 parent 68cf0d9 commit 648e739

7 files changed

Lines changed: 851 additions & 282 deletions

HashLib.Benchmark/Delphi/PerformanceBenchmarkConsole.dpr

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ program PerformanceBenchmarkConsole;
55
uses
66
Classes,
77
SysUtils,
8+
uBenchmarkCommon in '..\src\Core\uBenchmarkCommon.pas',
9+
uHashPerformanceBenchmark in '..\src\Core\uHashPerformanceBenchmark.pas',
10+
uKdfPerformanceBenchmark in '..\src\Core\uKdfPerformanceBenchmark.pas',
811
uPerformanceBenchmark in '..\src\Core\uPerformanceBenchmark.pas',
912
HlpHash in '..\..\HashLib\src\Base\HlpHash.pas',
1013
HlpKDF in '..\..\HashLib\src\Base\HlpKDF.pas',

HashLib.Benchmark/Delphi/PerformanceBenchmarkFMX.dpr

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ uses
44
System.StartUpCopy,
55
FMX.Forms,
66
fmxMainForm in '..\src\Forms\FMX\fmxMainForm.pas' {MainForm},
7+
uBenchmarkCommon in '..\src\Core\uBenchmarkCommon.pas',
8+
uHashPerformanceBenchmark in '..\src\Core\uHashPerformanceBenchmark.pas',
9+
uKdfPerformanceBenchmark in '..\src\Core\uKdfPerformanceBenchmark.pas',
710
uPerformanceBenchmark in '..\src\Core\uPerformanceBenchmark.pas',
811
HlpHash in '..\..\HashLib\src\Base\HlpHash.pas',
912
HlpKDF in '..\..\HashLib\src\Base\HlpKDF.pas',

HashLib.Benchmark/Lazarus/PerformanceBenchmarkConsole.lpi

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<CONFIG>
33
<ProjectOptions>
4-
<Version Value="10"/>
4+
<Version Value="12"/>
55
<PathDelim Value="\"/>
66
<General>
77
<Flags>
88
<MainUnitHasCreateFormStatements Value="False"/>
99
<MainUnitHasTitleStatement Value="False"/>
1010
<MainUnitHasScaledStatement Value="False"/>
11+
<CompatibilityMode Value="True"/>
1112
</Flags>
1213
<SessionStorage Value="InProjectDir"/>
13-
<MainUnit Value="0"/>
1414
<Title Value="PerformanceBenchmarkConsole"/>
1515
<UseAppBundle Value="False"/>
1616
<ResourceType Value="res"/>
@@ -58,16 +58,17 @@
5858
<Version Value="2"/>
5959
</PublishOptions>
6060
<RunParams>
61-
<local>
62-
<FormatVersion Value="1"/>
63-
</local>
61+
<FormatVersion Value="2"/>
62+
<Modes Count="1">
63+
<Mode0 Name="default"/>
64+
</Modes>
6465
</RunParams>
6566
<RequiredPackages Count="1">
6667
<Item1>
6768
<PackageName Value="HashLib4PascalPackage"/>
6869
</Item1>
6970
</RequiredPackages>
70-
<Units Count="2">
71+
<Units Count="5">
7172
<Unit0>
7273
<Filename Value="PerformanceBenchmarkConsole.lpr"/>
7374
<IsPartOfProject Value="True"/>
@@ -76,6 +77,18 @@
7677
<Filename Value="..\src\Core\uPerformanceBenchmark.pas"/>
7778
<IsPartOfProject Value="True"/>
7879
</Unit1>
80+
<Unit2>
81+
<Filename Value="..\src\Core\uBenchmarkCommon.pas"/>
82+
<IsPartOfProject Value="True"/>
83+
</Unit2>
84+
<Unit3>
85+
<Filename Value="..\src\Core\uHashPerformanceBenchmark.pas"/>
86+
<IsPartOfProject Value="True"/>
87+
</Unit3>
88+
<Unit4>
89+
<Filename Value="..\src\Core\uKdfPerformanceBenchmark.pas"/>
90+
<IsPartOfProject Value="True"/>
91+
</Unit4>
7992
</Units>
8093
</ProjectOptions>
8194
<CompilerOptions>
Lines changed: 191 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,191 @@
1+
unit uBenchmarkCommon;
2+
3+
{$IFDEF FPC}
4+
{$MODE DELPHI}
5+
{$WARNINGS OFF}
6+
{$ENDIF FPC}
7+
8+
interface
9+
10+
uses
11+
SysUtils;
12+
13+
type
14+
TBenchmarkLogProc = procedure(const AMessage: String);
15+
16+
const
17+
BENCH_DURATION_MS = UInt32(3000);
18+
BENCH_ROUNDS = 3;
19+
BENCH_LABEL_COL_WIDTH = 32;
20+
BENCH_VALUE_COL_WIDTH = 16;
21+
BENCH_KDF_VALUE_COL_WIDTH = 22;
22+
23+
type
24+
TBenchmarkReport = class sealed(TObject)
25+
public
26+
class var
27+
FloatFormat: TFormatSettings;
28+
strict private
29+
class constructor Create;
30+
public
31+
class function GetPlatformInfo: String;
32+
class function BuildSeparator(AWidth: Int32): String;
33+
class function BuildHeaderRow(const AFirstColTitle: String;
34+
const AColumnLabels: array of String): String; overload;
35+
class function BuildHeaderRow(const AFirstColTitle: String;
36+
const AColumnLabels: array of String;
37+
AValueColumnWidth: Int32): String; overload;
38+
class function BuildDataRow(const ARowLabel: String;
39+
const ACells: array of String): String; overload;
40+
class function BuildDataRow(const ARowLabel: String;
41+
const ACells: array of String;
42+
AValueColumnWidth: Int32): String; overload;
43+
class procedure WriteStandardFooter(ALogProc: TBenchmarkLogProc;
44+
ATableWidth: Int32);
45+
end;
46+
47+
implementation
48+
49+
{ TBenchmarkReport }
50+
51+
class constructor TBenchmarkReport.Create;
52+
begin
53+
{$IFDEF FPC}
54+
FloatFormat := DefaultFormatSettings;
55+
{$ELSE}
56+
FloatFormat := FormatSettings;
57+
{$ENDIF}
58+
FloatFormat.ThousandSeparator := ',';
59+
FloatFormat.DecimalSeparator := '.';
60+
end;
61+
62+
class function TBenchmarkReport.BuildSeparator(AWidth: Int32): String;
63+
begin
64+
Result := StringOfChar('-', AWidth);
65+
end;
66+
67+
class function TBenchmarkReport.BuildHeaderRow(const AFirstColTitle: String;
68+
const AColumnLabels: array of String): String;
69+
begin
70+
Result := BuildHeaderRow(AFirstColTitle, AColumnLabels, BENCH_VALUE_COL_WIDTH);
71+
end;
72+
73+
class function TBenchmarkReport.BuildHeaderRow(const AFirstColTitle: String;
74+
const AColumnLabels: array of String; AValueColumnWidth: Int32): String;
75+
var
76+
LIdx: Int32;
77+
LCell: String;
78+
begin
79+
Result := AFirstColTitle;
80+
while System.Length(Result) < BENCH_LABEL_COL_WIDTH do
81+
Result := Result + ' ';
82+
83+
for LIdx := System.Low(AColumnLabels) to System.High(AColumnLabels) do
84+
begin
85+
LCell := AColumnLabels[LIdx];
86+
while System.Length(LCell) < AValueColumnWidth do
87+
LCell := ' ' + LCell;
88+
Result := Result + LCell;
89+
end;
90+
end;
91+
92+
class function TBenchmarkReport.BuildDataRow(const ARowLabel: String;
93+
const ACells: array of String): String;
94+
begin
95+
Result := BuildDataRow(ARowLabel, ACells, BENCH_VALUE_COL_WIDTH);
96+
end;
97+
98+
class function TBenchmarkReport.BuildDataRow(const ARowLabel: String;
99+
const ACells: array of String; AValueColumnWidth: Int32): String;
100+
var
101+
LIdx: Int32;
102+
LCell: String;
103+
begin
104+
Result := ARowLabel;
105+
while System.Length(Result) < BENCH_LABEL_COL_WIDTH do
106+
Result := Result + ' ';
107+
108+
for LIdx := System.Low(ACells) to System.High(ACells) do
109+
begin
110+
LCell := ACells[LIdx];
111+
while System.Length(LCell) < AValueColumnWidth do
112+
LCell := ' ' + LCell;
113+
Result := Result + LCell;
114+
end;
115+
end;
116+
117+
class function TBenchmarkReport.GetPlatformInfo: String;
118+
var
119+
LOS, LCompiler, LCPU: String;
120+
begin
121+
{$IFDEF FPC}
122+
{$IF DEFINED(MSWINDOWS)}
123+
LOS := 'Windows';
124+
{$ELSEIF DEFINED(LINUX)}
125+
LOS := 'Linux';
126+
{$ELSEIF DEFINED(DARWIN)}
127+
LOS := 'macOS';
128+
{$ELSE}
129+
LOS := 'Unknown OS';
130+
{$ENDIF}
131+
132+
{$IF DEFINED(CPUX86_64)}
133+
LCPU := 'x86_64';
134+
{$ELSEIF DEFINED(CPUI386)}
135+
LCPU := 'i386';
136+
{$ELSEIF DEFINED(CPUAARCH64)}
137+
LCPU := 'AArch64';
138+
{$ELSEIF DEFINED(CPUARM)}
139+
LCPU := 'ARM';
140+
{$ELSE}
141+
LCPU := 'Unknown CPU';
142+
{$ENDIF}
143+
{$ELSE}
144+
{$IF DEFINED(MSWINDOWS)}
145+
LOS := 'Windows';
146+
{$ELSEIF DEFINED(ANDROID)}
147+
LOS := 'Android';
148+
{$ELSEIF DEFINED(IOS)}
149+
LOS := 'iOS';
150+
{$ELSEIF DEFINED(MACOS)}
151+
LOS := 'macOS';
152+
{$ELSEIF DEFINED(LINUX)}
153+
LOS := 'Linux';
154+
{$ELSE}
155+
LOS := 'Unknown OS';
156+
{$ENDIF}
157+
158+
{$IF DEFINED(CPUX64)}
159+
LCPU := 'x86_64';
160+
{$ELSEIF DEFINED(CPUX86)}
161+
LCPU := 'i386';
162+
{$ELSEIF DEFINED(CPUARM64)}
163+
LCPU := 'AArch64';
164+
{$ELSEIF DEFINED(CPUARM)}
165+
LCPU := 'ARM';
166+
{$ELSE}
167+
LCPU := 'Unknown CPU';
168+
{$ENDIF}
169+
{$ENDIF}
170+
171+
{$IFDEF FPC}
172+
LCompiler := 'FPC ' + {$I %FPCVERSION%};
173+
{$ELSE}
174+
LCompiler := Format('Delphi (CompilerVersion %.1f)', [CompilerVersion]);
175+
{$ENDIF}
176+
177+
Result := Format('Platform: %s %s, %s', [LOS, LCPU, LCompiler]);
178+
end;
179+
180+
class procedure TBenchmarkReport.WriteStandardFooter(ALogProc: TBenchmarkLogProc;
181+
ATableWidth: Int32);
182+
begin
183+
if ATableWidth < 40 then
184+
ATableWidth := 40;
185+
ALogProc(BuildSeparator(ATableWidth));
186+
ALogProc('Benchmark complete.');
187+
ALogProc(GetPlatformInfo);
188+
ALogProc('Date: ' + FormatDateTime('yyyy-mm-dd', Now));
189+
end;
190+
191+
end.

0 commit comments

Comments
 (0)