|
| 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