-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathHowTo.HttpClient.dpr
110 lines (102 loc) · 2.41 KB
/
HowTo.HttpClient.dpr
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
program HowTo.HttpClient;
{$APPTYPE CONSOLE}
{$R *.res}
uses
{$I ../Impl.inc}
{$I ../Includes.inc}
System.SysUtils,
Classes,
IdIOHandlerStack,
Generics.Collections,
IdHTTP;
const
PARALLEL_NUM = 50;
STRESS_FACTOR = 100;
var
Stamp: TDateTime;
Ms, Sec, Min, Hrs: Word;
Threads: TList<TThread>;
Greenlets: TGreenGroup<Integer>;
G: TSymmetric;
Th: TThread;
I: Integer;
procedure PrintTimeout(Start, Stop: TDateTime);
begin
DecodeTime(Stop-Start, Hrs, Min, Sec, Ms);
Writeln(Format('Sec: %d, MSec: %d', [Sec, Ms]));
end;
begin
Writeln('Workers Count = ' + IntToStr(PARALLEL_NUM));
Writeln('Using blocking Indy sockets');
Stamp := Now;
// Threads with blocking sockets
Threads := TList<TThread>.Create;
try
for I := 0 to PARALLEL_NUM-1 do begin
Th := TThread.CreateAnonymousThread(procedure
var
Client: TIdHTTP;
Response: string;
I: Integer;
begin
Client := TIdHTTP.Create(nil);
try
Client.IOHandler := TIdIOHandlerStack.Create(Client);
Client.HandleRedirects := True;
for I := 1 to STRESS_FACTOR do begin
Response := Client.Get('http://uit.fun/aio');
Assert(Response <> '');
end;
finally
Client.Free;
end;
end
);
Threads.Add(Th);
Th.FreeOnTerminate := False;
Th.Start;
end;
// Join
for I := 0 to PARALLEL_NUM-1 do begin
Threads[I].WaitFor;
Threads[I].Free;
end;
PrintTimeout(Stamp, Now);
finally
Threads.Free
end;
Writeln('Using non-blocking Aio IO handler');
Stamp := Now;
for I := 0 to PARALLEL_NUM-1 do begin
G := TSymmetric.Create(procedure
var
Client: TIdHTTP;
Response: string;
I: Integer;
begin
Client := TIdHTTP.Create(nil);
try
//Write('*');
Client.IOHandler := TAioIdIOHandlerSocket.Create(Client);
Client.HandleRedirects := True;
for I := 1 to STRESS_FACTOR do begin
Response := Client.Get('http://uit.fun/aio');
Assert(Response <> '');
end;
finally
Client.Free;
end;
end
);
Greenlets[I] := G;
end;
try
Greenlets.Join(INFINITE, True);
except
on E: Exception do begin
Writeln('Error: ' + E.Message);
end
end;
PrintTimeout(Stamp, Now);
Readln;
end.