| 1 |
program TestMTP1; |
| 2 |
|
| 3 |
{$mode objfpc}{$H+} |
| 4 |
|
| 5 |
uses |
| 6 |
{$IFDEF UNIX} |
| 7 |
cthreads, cmem, |
| 8 |
{$ENDIF} |
| 9 |
Math, SysUtils, Classes, MTProcs, MTPUtils, MultiThreadProcsLaz; |
| 10 |
|
| 11 |
type |
| 12 |
|
| 13 |
{ TTestItem } |
| 14 |
|
| 15 |
TTestItem = class |
| 16 |
private |
| 17 |
FIndex: int64; |
| 18 |
public |
| 19 |
property Index: int64 read FIndex; |
| 20 |
constructor Create(NewIndex: int64); |
| 21 |
end; |
| 22 |
|
| 23 |
{ TTests } |
| 24 |
|
| 25 |
TTests = class |
| 26 |
public |
| 27 |
procedure Work(Seconds: integer); |
| 28 |
|
| 29 |
// RTLeventSetEvent, RTLeventWaitFor |
| 30 |
procedure TestRTLevent_Set_WaitFor; |
| 31 |
|
| 32 |
// single thread test |
| 33 |
procedure TestSingleThread; |
| 34 |
procedure MTPLoop_TestSingleThread(Index: PtrInt; Data: Pointer; |
| 35 |
Item: TMultiThreadProcItem); |
| 36 |
|
| 37 |
// two threads test: run once |
| 38 |
procedure TestTwoThreads1; |
| 39 |
procedure MTPLoop_TestTwoThreads1(Index: PtrInt; Data: Pointer; |
| 40 |
Item: TMultiThreadProcItem); |
| 41 |
|
| 42 |
// 0 runs two seconds, |
| 43 |
// 1 runs a second then waits for 0 then runs a second |
| 44 |
// 2 runs a second then waits for 1 |
| 45 |
// 3 waits for 0 |
| 46 |
// 4 waits for 1 |
| 47 |
// 5 waits for 2 |
| 48 |
procedure TestMTPWaitForIndex; |
| 49 |
procedure MTPLoop_TestMTPWaitForIndex(Index: PtrInt; Data: Pointer; |
| 50 |
Item: TMultiThreadProcItem); |
| 51 |
|
| 52 |
// two threads test: various run times |
| 53 |
procedure TestMTPTwoThreads2; |
| 54 |
procedure MTPLoop_TestTwoThreads2(Index: PtrInt; Data: Pointer; |
| 55 |
Item: TMultiThreadProcItem); |
| 56 |
|
| 57 |
// test exception in starter thread |
| 58 |
procedure TestMTPExceptionInStarterThread; |
| 59 |
procedure MTPLoop_TestExceptionInStarterThread(Index: PtrInt; Data: Pointer; |
| 60 |
Item: TMultiThreadProcItem); |
| 61 |
|
| 62 |
// test exception in helper thread |
| 63 |
procedure TestMTPExceptionInHelperThread; |
| 64 |
procedure MTPLoop_TestExceptionInHelperThread(Index: PtrInt; Data: Pointer; |
| 65 |
Item: TMultiThreadProcItem); |
| 66 |
|
| 67 |
// test parallel sort |
| 68 |
procedure TestMTPSort; |
| 69 |
procedure MTPLoop_TestDoubleMTPSort(Index: PtrInt; Data: Pointer; |
| 70 |
Item: TMultiThreadProcItem); |
| 71 |
end; |
| 72 |
|
| 73 |
{ TTestItem } |
| 74 |
|
| 75 |
constructor TTestItem.Create(NewIndex: int64); |
| 76 |
begin |
| 77 |
FIndex:=NewIndex; |
| 78 |
end; |
| 79 |
|
| 80 |
{ TTests } |
| 81 |
|
| 82 |
procedure TTests.Work(Seconds: integer); |
| 83 |
var |
| 84 |
Start: TDateTime; |
| 85 |
begin |
| 86 |
Start:=Now; |
| 87 |
while (Now-Start)*86400<Seconds do if GetCurrentDir='' then ; |
| 88 |
end; |
| 89 |
|
| 90 |
procedure TTests.TestRTLevent_Set_WaitFor; |
| 91 |
var |
| 92 |
e: PRTLEvent; |
| 93 |
begin |
| 94 |
e:=RTLEventCreate; |
| 95 |
RTLeventSetEvent(e); |
| 96 |
RTLeventWaitFor(e); |
| 97 |
RTLeventdestroy(e); |
| 98 |
end; |
| 99 |
|
| 100 |
procedure TTests.TestSingleThread; |
| 101 |
begin |
| 102 |
ProcThreadPool.DoParallel(@MTPLoop_TestSingleThread,1,3,nil,1); |
| 103 |
end; |
| 104 |
|
| 105 |
procedure TTests.MTPLoop_TestSingleThread(Index: PtrInt; Data: Pointer; |
| 106 |
Item: TMultiThreadProcItem); |
| 107 |
begin |
| 108 |
writeln('TTests.MTPLoop_TestSingleThread Index=',Index); |
| 109 |
end; |
| 110 |
|
| 111 |
procedure TTests.TestTwoThreads1; |
| 112 |
begin |
| 113 |
WriteLn('TTests.TestTwoThreads1 START'); |
| 114 |
ProcThreadPool.DoParallel(@MTPLoop_TestTwoThreads1,1,2,nil,2); |
| 115 |
WriteLn('TTests.TestTwoThreads1 END'); |
| 116 |
end; |
| 117 |
|
| 118 |
procedure TTests.MTPLoop_TestTwoThreads1(Index: PtrInt; Data: Pointer; |
| 119 |
Item: TMultiThreadProcItem); |
| 120 |
var |
| 121 |
i: Integer; |
| 122 |
begin |
| 123 |
for i:=1 to 3 do begin |
| 124 |
WriteLn('TTests.MTPLoop_TestTwoThreads1 Index=',Index,' ',i); |
| 125 |
Work(1); |
| 126 |
end; |
| 127 |
end; |
| 128 |
|
| 129 |
procedure TTests.TestMTPWaitForIndex; |
| 130 |
var |
| 131 |
IndexStates: PInteger; |
| 132 |
begin |
| 133 |
ProcThreadPool.MaxThreadCount:=8; |
| 134 |
IndexStates:=nil; |
| 135 |
GetMem(IndexStates,SizeOf(Integer)*10); |
| 136 |
FillByte(IndexStates^,SizeOf(Integer)*10,0); |
| 137 |
WriteLn('TTests.TestMTPWaitForIndex START'); |
| 138 |
ProcThreadPool.DoParallel(@MTPLoop_TestMTPWaitForIndex,0,5,IndexStates); |
| 139 |
FreeMem(IndexStates); |
| 140 |
WriteLn('TTests.TestMTPWaitForIndex END'); |
| 141 |
end; |
| 142 |
|
| 143 |
procedure TTests.MTPLoop_TestMTPWaitForIndex(Index: PtrInt; Data: Pointer; |
| 144 |
Item: TMultiThreadProcItem); |
| 145 |
// 0 runs two seconds, |
| 146 |
// 1 runs a second then waits for 0 then runs a second |
| 147 |
// 2 runs a second then waits for 1 |
| 148 |
// 3 waits for 0 |
| 149 |
// 4 waits for 1 |
| 150 |
// 5 waits for 2 |
| 151 |
|
| 152 |
procedure WaitFor(OtherIndex: PtrInt); |
| 153 |
begin |
| 154 |
WriteLn('TTests.MTPLoop_TestMTPWaitForIndex Index='+IntToStr(Index)+' waiting for '+IntToStr(OtherIndex)+' ...'); |
| 155 |
Item.WaitForIndex(OtherIndex); |
| 156 |
WriteLn('TTests.MTPLoop_TestMTPWaitForIndex Index='+IntToStr(Index)+' waited for '+IntToStr(OtherIndex)+'. working ...'); |
| 157 |
if PInteger(Data)[OtherIndex]<>2 then begin |
| 158 |
WriteLn('TTests.MTPLoop_TestMTPWaitForIndex Index='+IntToStr(Index)+' ERROR: waited for '+IntToStr(OtherIndex)+' failed: OtherState='+IntToStr(PInteger(Data)[OtherIndex])); |
| 159 |
end; |
| 160 |
end; |
| 161 |
|
| 162 |
begin |
| 163 |
WriteLn('TTests.MTPLoop_TestMTPWaitForIndex Index='+IntToStr(Index)+' START'); |
| 164 |
if PInteger(Data)[Index]<>0 then begin |
| 165 |
WriteLn('TTests.MTPLoop_TestMTPWaitForIndex Index='+IntToStr(Index)+' ERROR: IndexState='+IntToStr(PInteger(Data)[Index])); |
| 166 |
end; |
| 167 |
PInteger(Data)[Index]:=1; |
| 168 |
case Index of |
| 169 |
0: Work(2); |
| 170 |
1:begin |
| 171 |
Work(1); |
| 172 |
WaitFor(0); |
| 173 |
Work(1); |
| 174 |
end; |
| 175 |
2:begin |
| 176 |
Work(1); |
| 177 |
WaitFor(1); |
| 178 |
end; |
| 179 |
3:begin |
| 180 |
WaitFor(0); |
| 181 |
end; |
| 182 |
4:begin |
| 183 |
WaitFor(1); |
| 184 |
end; |
| 185 |
5:begin |
| 186 |
WaitFor(2); |
| 187 |
end; |
| 188 |
end; |
| 189 |
WriteLn('TTests.MTPLoop_TestMTPWaitForIndex Index='+IntToStr(Index)+' END'); |
| 190 |
PInteger(Data)[Index]:=2; |
| 191 |
end; |
| 192 |
|
| 193 |
procedure TTests.TestMTPTwoThreads2; |
| 194 |
begin |
| 195 |
WriteLn('TTests.TestMTPTwoThreads1 START'); |
| 196 |
ProcThreadPool.DoParallel(@MTPLoop_TestTwoThreads2,1,6,nil,2); |
| 197 |
WriteLn('TTests.TestMTPTwoThreads1 END'); |
| 198 |
end; |
| 199 |
|
| 200 |
procedure TTests.MTPLoop_TestTwoThreads2(Index: PtrInt; Data: Pointer; |
| 201 |
Item: TMultiThreadProcItem); |
| 202 |
var |
| 203 |
i: Integer; |
| 204 |
begin |
| 205 |
for i:=1 to (Index mod 3)+1 do begin |
| 206 |
WriteLn('TTests.MTPLoop_TestTwoThreads1 Index=',Index,' i=',i,' ID=',PtrUint(GetThreadID)); |
| 207 |
Work(1); |
| 208 |
end; |
| 209 |
end; |
| 210 |
|
| 211 |
type |
| 212 |
TMyException = class(Exception); |
| 213 |
|
| 214 |
procedure TTests.TestMTPExceptionInStarterThread; |
| 215 |
var |
| 216 |
IndexStates: PInteger; |
| 217 |
begin |
| 218 |
WriteLn('TTests.TestMTPExceptionInStarterThread START'); |
| 219 |
ProcThreadPool.MaxThreadCount:=8; |
| 220 |
IndexStates:=nil; |
| 221 |
GetMem(IndexStates,SizeOf(Integer)*10); |
| 222 |
FillByte(IndexStates^,SizeOf(Integer)*10,0); |
| 223 |
try |
| 224 |
ProcThreadPool.DoParallel(@MTPLoop_TestExceptionInStarterThread,1,3,IndexStates,2); |
| 225 |
except |
| 226 |
on E: Exception do begin |
| 227 |
WriteLn('TTests.TestMTPExceptionInHelperThread E.ClassName=',E.ClassName,' E.Message=',E.Message); |
| 228 |
end; |
| 229 |
end; |
| 230 |
FreeMem(IndexStates); |
| 231 |
WriteLn('TTests.TestMTPExceptionInStarterThread END'); |
| 232 |
end; |
| 233 |
|
| 234 |
procedure TTests.MTPLoop_TestExceptionInStarterThread(Index: PtrInt; |
| 235 |
Data: Pointer; Item: TMultiThreadProcItem); |
| 236 |
begin |
| 237 |
WriteLn('TTests.MTPLoop_TestExceptionInStarterThread START Index='+IntToStr(Index)); |
| 238 |
if PInteger(Data)[Index]<>0 then |
| 239 |
WriteLn('TTests.MTPLoop_TestExceptionInStarterThread Index='+IntToStr(Index)+' ERROR: IndexState='+IntToStr(PInteger(Data)[Index])); |
| 240 |
PInteger(Data)[Index]:=1; |
| 241 |
case Index of |
| 242 |
1: |
| 243 |
begin |
| 244 |
// Main Thread |
| 245 |
Work(1); |
| 246 |
WriteLn('TTests.MTPLoop_TestExceptionInStarterThread raising exception in Index='+IntToStr(Index)+' ...'); |
| 247 |
raise Exception.Create('Exception in starter thread'); |
| 248 |
end; |
| 249 |
else |
| 250 |
Work(Index); |
| 251 |
end; |
| 252 |
PInteger(Data)[Index]:=2; |
| 253 |
WriteLn('TTests.MTPLoop_TestExceptionInStarterThread END Index='+IntToStr(Index)); |
| 254 |
end; |
| 255 |
|
| 256 |
procedure TTests.TestMTPExceptionInHelperThread; |
| 257 |
var |
| 258 |
IndexStates: PInteger; |
| 259 |
begin |
| 260 |
WriteLn('TTests.TestMTPExceptionInHelperThread START'); |
| 261 |
ProcThreadPool.MaxThreadCount:=8; |
| 262 |
IndexStates:=nil; |
| 263 |
GetMem(IndexStates,SizeOf(Integer)*10); |
| 264 |
FillByte(IndexStates^,SizeOf(Integer)*10,0); |
| 265 |
try |
| 266 |
ProcThreadPool.DoParallel(@MTPLoop_TestExceptionInHelperThread,1,3,IndexStates,2); |
| 267 |
except |
| 268 |
on E: Exception do begin |
| 269 |
WriteLn('TTests.TestMTPExceptionInHelperThread E.ClassName=',E.ClassName,' E.Message=',E.Message); |
| 270 |
end; |
| 271 |
end; |
| 272 |
FreeMem(IndexStates); |
| 273 |
WriteLn('TTests.TestMTPExceptionInHelperThread END'); |
| 274 |
end; |
| 275 |
|
| 276 |
procedure TTests.MTPLoop_TestExceptionInHelperThread(Index: PtrInt; |
| 277 |
Data: Pointer; Item: TMultiThreadProcItem); |
| 278 |
begin |
| 279 |
WriteLn('TTests.MTPLoop_TestExceptionInHelperThread START Index='+IntToStr(Index)); |
| 280 |
if PInteger(Data)[Index]<>0 then |
| 281 |
WriteLn('TTests.MTPLoop_TestExceptionInHelperThread Index='+IntToStr(Index)+' ERROR: IndexState='+IntToStr(PInteger(Data)[Index])); |
| 282 |
PInteger(Data)[Index]:=1; |
| 283 |
case Index of |
| 284 |
2: |
| 285 |
begin |
| 286 |
// Helper Thread 2 |
| 287 |
Work(1); |
| 288 |
WriteLn('TTests.MTPLoop_TestExceptionInHelperThread raising exception in Index='+IntToStr(Index)+' ...'); |
| 289 |
raise TMyException.Create('Exception in helper thread'); |
| 290 |
end; |
| 291 |
else |
| 292 |
Work(Index+1); |
| 293 |
end; |
| 294 |
PInteger(Data)[Index]:=2; |
| 295 |
WriteLn('TTests.MTPLoop_TestExceptionInHelperThread END Index='+IntToStr(Index)); |
| 296 |
end; |
| 297 |
|
| 298 |
function CompareTestItems(Data1, Data2: Pointer): integer; |
| 299 |
begin |
| 300 |
if TTestItem(Data1).Index>TTestItem(Data2).Index then |
| 301 |
Result:=1 |
| 302 |
else if TTestItem(Data1).Index<TTestItem(Data2).Index then |
| 303 |
Result:=-1 |
| 304 |
else |
| 305 |
Result:=0; |
| 306 |
end; |
| 307 |
|
| 308 |
procedure TTests.TestMTPSort; |
| 309 |
var |
| 310 |
OuterLoop: Integer; |
| 311 |
InnerLoop: Integer; |
| 312 |
begin |
| 313 |
OuterLoop:=1; |
| 314 |
InnerLoop:=0; |
| 315 |
if Paramcount=1 then begin |
| 316 |
InnerLoop:=StrToInt(ParamStr(1)); |
| 317 |
end else if Paramcount=2 then begin |
| 318 |
OuterLoop:=StrToInt(ParamStr(1)); |
| 319 |
InnerLoop:=StrToInt(ParamStr(2)); |
| 320 |
end; |
| 321 |
writeln('TTests.TestMTPSort Running ',OuterLoop,'x',InnerLoop); |
| 322 |
ProcThreadPool.DoParallel(@MTPLoop_TestDoubleMTPSort,1,OuterLoop,@InnerLoop); |
| 323 |
end; |
| 324 |
|
| 325 |
procedure TTests.MTPLoop_TestDoubleMTPSort(Index: PtrInt; Data: Pointer; |
| 326 |
Item: TMultiThreadProcItem); |
| 327 |
var |
| 328 |
i: Integer; |
| 329 |
List: TFPList; |
| 330 |
t: double; |
| 331 |
begin |
| 332 |
// create an unsorted list of values |
| 333 |
List:=TFPList.Create; |
| 334 |
for i:=1 to 10000000 do List.Add(TTestItem.Create(Random(99999999999))); |
| 335 |
//QuickSort(List,0,List.Count-1,@AnsiCompareText); |
| 336 |
|
| 337 |
t:=Now; |
| 338 |
ParallelSortFPList(List,@CompareTestItems,PInteger(Data)^); |
| 339 |
t:=Now-t; |
| 340 |
writeln('TTests.TestMTPSort ',t*86400); |
| 341 |
// check |
| 342 |
sleep(1); |
| 343 |
for i:=0 to List.Count-2 do |
| 344 |
if CompareTestItems(List[i],List[i+1])>0 then raise Exception.Create('not sorted'); |
| 345 |
|
| 346 |
for i:=0 to List.Count-1 do |
| 347 |
TObject(List[i]).Free; |
| 348 |
List.Free; |
| 349 |
end; |
| 350 |
|
| 351 |
var |
| 352 |
Tests: TTests; |
| 353 |
begin |
| 354 |
writeln('threads=',ProcThreadPool.MaxThreadCount); |
| 355 |
ProcThreadPool.MaxThreadCount:=8; |
| 356 |
Tests:=TTests.Create; |
| 357 |
//Tests.Test1; |
| 358 |
//Tests.Test2; |
| 359 |
//Tests.TestTwoThreads2; |
| 360 |
//Tests.TestRTLevent_Set_WaitFor; |
| 361 |
//Tests.TestMTPWaitForIndex; |
| 362 |
//Tests.TestMTPExceptionInStarterThread; |
| 363 |
Tests.TestMTPExceptionInHelperThread; |
| 364 |
//Tests.TestMTPSort; |
| 365 |
Tests.Free; |
| 366 |
end. |
| 367 |
|