aws_21.0.0_57fddf8f/regtests/0060_hload_sec/s_hload_pack.adb

  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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
------------------------------------------------------------------------------
--                              Ada Web Server                              --
--                                                                          --
--                     Copyright (C) 2004-2014, AdaCore                     --
--                                                                          --
--  This is free software;  you can redistribute it  and/or modify it       --
--  under terms of the  GNU General Public License as published  by the     --
--  Free Software  Foundation;  either version 3,  or (at your option) any  --
--  later version.  This software is distributed in the hope  that it will  --
--  be useful, but WITHOUT ANY WARRANTY;  without even the implied warranty --
--  of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU     --
--  General Public License for  more details.                               --
--                                                                          --
--  You should have  received  a copy of the GNU General  Public  License   --
--  distributed  with  this  software;   see  file COPYING3.  If not, go    --
--  to http://www.gnu.org/licenses for a complete copy of the license.      --
------------------------------------------------------------------------------

--  Test for heavy loading

with Ada.Calendar;
with Ada.Containers.Indefinite_Doubly_Linked_Lists;
with Ada.Exceptions;
with Ada.Strings.Unbounded;
with Ada.Strings.Hash;
with Ada.Task_Attributes;
with Ada.Task_Identification;
with Ada.Text_IO;

with AWS.Config.Set;
with AWS.Client;
with AWS.MIME;
with AWS.Net.Log;
with AWS.Net.SSL;
with AWS.Parameters;
with AWS.Response;
with AWS.Server.Status;
with AWS.Status;
with AWS.Utils;

package body S_HLoad_Pack is

   use Ada;
   use Ada.Exceptions;
   use Ada.Strings.Unbounded;

   use AWS;

   Max_Client   : constant := 18;
   Max_Line     : constant := 16;
   Client_Count : constant := 300;

   Client_Error : Boolean := False with Atomic;

   package String_Lists is
     new Ada.Containers.Indefinite_Doubly_Linked_Lists (String);

   package Debug_Dumps is
     new Ada.Task_Attributes (String_Lists.List, String_Lists.Empty_List);

   procedure Debug_Output (Text : String);

   procedure Error_Callback
     (Socket : Net.Socket_Type'Class; Message : String);
   --  The callback procedure which is called for every socket error

   procedure Event_Callback
     (Action : Net.Log.Event_Type; Socket : Net.Socket_Type'Class);

   function CB (Request : Status.Data) return Response.Data;

   subtype Count is Long_Integer;

   protected Interval_Timer is
      procedure Reset;
      procedure Stamp;
      function Statistic_Image (Timed : Boolean) return String;

   private
      Last : Ada.Calendar.Time;
      Start : Ada.Calendar.Time;

      Counter   : Count;
      Max_Index : Count;
      Min_Index : Count;

      Max : Duration;
      Min : Duration;
   end Interval_Timer;

   --------
   -- CB --
   --------

   function CB (Request : Status.Data) return Response.Data is
      P : constant AWS.Parameters.List := AWS.Status.Parameters (Request);
   begin
      Interval_Timer.Stamp;

      return Response.Build
        (MIME.Text_HTML, "Data:" & AWS.Parameters.Get (P, "PARAM"));
   end CB;

   Counter : Utils.Counter (100_000);

   ------------------
   -- Debug_Output --
   ------------------

   procedure Debug_Output (Text : String) is
      Value : Natural;
   begin
      Counter.Increment (Value => Value);

      declare
         Cnt_Img : constant String := Natural'Image (Value);
      begin
         Debug_Dumps.Reference.Append
           (Cnt_Img (Cnt_Img'Last - 4 .. Cnt_Img'Last)  & ' ' & Text);
      end;
   end Debug_Output;

   --------------------
   -- Error_Callback --
   --------------------

   procedure Error_Callback
     (Socket : Net.Socket_Type'Class; Message : String)
   is
      FD : constant Integer := Socket.Get_FD;
   begin
      if FD = Net.No_Socket then
         Debug_Output (Message & ASCII.LF);
      else
         Debug_Output
           (Socket.Get_Port'Img & Socket.Peer_Port'Img & FD'Img & ' '
            & Message & ASCII.LF);
      end if;

   exception
      when E : others =>
         Debug_Output
           (Message & ' ' & Exception_Message (E) & ASCII.LF);
   end Error_Callback;

   --------------------
   -- Event_Callback --
   --------------------

   procedure Event_Callback
     (Action : Net.Log.Event_Type; Socket : Net.Socket_Type'Class)
   is
      FD : constant Integer := Socket.Get_FD;
   begin
      if FD = Net.No_Socket then
         Debug_Output (Action'Img & " closed socket" & ASCII.LF);
      else
         Debug_Output
           (Action'Img
            & (if Net.Log."=" (Action, Net.Log.Connect)
               then Socket.Peer_Port'Img & Socket.Get_Port'Img
               else Socket.Get_Port'Img & Socket.Peer_Port'Img)
            & FD'Img & ' ' & ASCII.LF);
      end if;

   exception
      when E : others =>
         Debug_Output
           (Action'Img & FD'Img & ' ' & Exception_Message (E) & ASCII.LF);
   end Event_Callback;

   --------------------
   -- Interval_Timer --
   --------------------

   protected body Interval_Timer is

      -----------
      -- Reset --
      -----------

      procedure Reset is
      begin
         Counter   := 0;
         Max_Index := 0;
         Min_Index := 0;
         Max       := 0.0;
         Min       := Duration'Last;
      end Reset;

      -----------
      -- Stamp --
      -----------

      procedure Stamp is
         use type Ada.Calendar.Time;

         Now : constant Ada.Calendar.Time := Ada.Calendar.Clock;
         Interval : Duration;
      begin
         if Counter = 0 then
            Start := Now;
         else
            Interval := Now - Last;

            if Interval > Max then
               Max       := Interval;
               Max_Index := Counter;

            elsif Interval < Min then
               Min       := Interval;
               Min_Index := Counter;
            end if;
         end if;

         Last    := Now;
         Counter := Counter + 1;
      end Stamp;

      ---------------------
      -- Statistic_Image --
      ---------------------

      function Statistic_Image (Timed : Boolean) return String is
         use type Ada.Calendar.Time;
         Result : constant String := "Counter:" & Count'Image (Counter);
      begin
         if Timed then
            return Result & ASCII.LF
              & "Min: index" & Count'Image (Min_Index)
                & "; interval:" & Duration'Image (Min) & " sec." & ASCII.LF
              & "Max: index" & Count'Image (Max_Index)
                & "; interval:" & Duration'Image (Max) & " sec." & ASCII.LF
              & "Average:"
                & Duration'Image (Duration (Counter) / (Last - Start))
                & " requests in second.";

         else
            return Result;

         end if;
      end Statistic_Image;

   end Interval_Timer;

   ---------
   -- Run --
   ---------

   procedure Run (Timed : Boolean := False; Debug : Boolean := False) is

      task type Client is
         entry Start (Name : String);
         entry Pause;
         entry Stop;
      end Client;

      WS  : Server.HTTP;
      Cfg : Config.Object;
      Sfg : Net.SSL.Config;

      Clients : array (1 .. Max_Client) of Client;
      Hang : array (Clients'Range) of Boolean := (others => False);

      Delay_Time : Duration := 60.0;

      procedure Dump_Task (Id : Task_Identification.Task_Id; Hang : Boolean);

      ------------
      -- Client --
      ------------

      task body Client is
         Name : Unbounded_String;
         Connect : AWS.Client.HTTP_Connection;
      begin
         accept Start (Name : String) do
            Client.Name := To_Unbounded_String (Name);
         end Start;

         AWS.Client.Create
           (Connect,
            AWS.Server.Status.Local_URL (WS),
            Timeouts => AWS.Client.Timeouts
              (Connect => 5.0,
               Send => 15.0, Receive => 15.0, Response => 15.0));

         for K in 1 .. Client_Count loop
            begin
               declare
                  K_Img : constant String
                    := Utils.Image (K) & '-' & To_String (Name);

                  R : AWS.Response.Data;
                  Expected : constant String := "Data:" & K_Img;
               begin
                  AWS.Client.Get (Connect, R, "/toto?PARAM=" & K_Img);

                  if Expected /= String'(AWS.Response.Message_Body (R)) then
                     Text_IO.Put_Line
                       ("nok " & K_Img
                        & " expected " & Expected
                        & " -> found "
                        & String'(AWS.Response.Message_Body (R)));

                     Client_Error := True;
                     exit;
                  end if;
               end;

            exception
               when E : others =>
                  Text_IO.Put_Line
                    ("client " & To_String (Name)
                     & " request " & Utils.Image (K) & " aborted.");
                  Text_IO.Put_Line
                    (" => " & Exceptions.Exception_Information (E));

                  Client_Error := True;
                  exit;
            end;
         end loop;

         AWS.Client.Close (Connect);

         accept Pause;

         accept Stop;
      end Client;

      ---------------
      -- Dump_Task --
      ---------------

      procedure Dump_Task (Id : Task_Identification.Task_Id; Hang : Boolean) is
         Dump : String_Lists.List;
         DFO  : Text_IO.File_Type;
      begin
         if not Debug then
            return;
         end if;

         Dump := Debug_Dumps.Value (Id);

         Text_IO.Create (DFO, Name => Task_Identification.Image (Id));

         if Hang then
            Text_IO.Put_Line (DFO, "HANG");
         end if;

         Text_IO.Put_Line (DFO, Net.SSL.Version);

         for Line of Dump loop
            Text_IO.Put (DFO, Line);
         end loop;

         Text_IO.Close (DFO);
      end Dump_Task;

   begin -- Run
      Interval_Timer.Reset;

      if Debug then
         Net.SSL.Set_Debug (7, Debug_Output'Access);

         Net.Log.Start
           (Write => null,
            Event => Event_Callback'Access,
            Error => Error_Callback'Access);
      end if;

      Config.Set.Server_Name        (Cfg, "Heavy Loaded");
      Config.Set.Server_Port        (Cfg, 0);
      Config.Set.Security           (Cfg, True);
      Config.Set.Max_Connection     (Cfg, Max_Line);
      Config.Set.Session            (Cfg, True);
      Config.Set.TLS_Ticket_Support (Cfg, False);

      --  Set SSL session size a bit less than number of clients, to provoke
      --  session extrusion.

      Config.Set.SSL_Session_Cache_Size (Cfg, Max_Client - 1);

      Server.Start (WS, CB'Access, Cfg);

      Ada.Text_IO.Put_Line ("server started."); Ada.Text_IO.Flush;

      delay 1.0;

      for K in Clients'Range loop
         Clients (K).Start ("client" & Utils.Image (K));
         Text_IO.Put_Line ("client " & Utils.Image (K) & " started.");
      end loop;

      for K in Clients'Range loop
         select Clients (K).Pause;
         or delay Delay_Time;
            Text_IO.Put_Line ("Client" & K'Img & " hangs.");
            Hang (K) := True;
            Client_Error := True;
            Delay_Time := Delay_Time / 3;
         end select;
      end loop;

      if Client_Error then
         --  Dump debug output of the all clients and server lines

         for K in Clients'Range loop
            Dump_Task (Clients (K)'Identity, Hang (K));
         end loop;

         declare
            Tasks : constant Server.Task_Id_Array := Server.Line_Tasks (WS);
         begin
            for K in Tasks'Range loop
               Dump_Task (Tasks (K), False);
            end loop;
         end;
      end if;

      for K in Clients'Range loop
         Text_IO.Put ("client " & Utils.Image (K));

         if Hang (K) then
            abort Clients (K);
            Text_IO.Put_Line (" aborted.");
         else
            Clients (K).Stop;
            Text_IO.Put_Line (" stopped.");
         end if;
      end loop;

      Server.Shutdown (WS);
      Ada.Text_IO.Put_Line ("server stopped.");

      Ada.Text_IO.Put_Line (Interval_Timer.Statistic_Image (Timed));

   exception
      when E : others =>
         Text_IO.Put_Line
           ("main task" & ASCII.LF & Exceptions.Exception_Information (E));
   end Run;

end S_HLoad_Pack;