summaryrefslogtreecommitdiff
path: root/delay_until/delay_until.adb
blob: 8253c52bbbd6351098e95e936357784c130e96e3 (plain)
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
with Ada.Real_Time;
with Ada.Text_Io;

use type Ada.Real_Time.Time;
use type Ada.Real_Time.Time_Span;


procedure Delay_Until_Test is
   Next_Wakeup : Ada.Real_Time.Time;
   Period      : constant Ada.Real_Time.Time_Span := Ada.Real_Time.Nanoseconds (16666667);
--   Period      : constant Ada.Real_Time.Time_Span := Ada.Real_Time.Nanoseconds      (20000);

   Iterations : constant := 1000;
   Actual_Span : array (1..Iterations) of Ada.Real_Time.Time_Span;
   Average_Span : Ada.Real_Time.Time_Span := Ada.Real_Time.Time_Span_Zero;
   Max_Span     : Ada.Real_Time.Time_Span := Ada.Real_Time.Time_Span_First;
   Min_Span     : Ada.Real_Time.Time_Span := Ada.Real_Time.Time_Span_Last;

begin
   Ada.Text_Io.Put_Line (Integer'Image (Iterations) & " iterations with " &
                         Duration'Image (Ada.Real_Time.To_Duration(Period)) & "s period");
   Next_Wakeup := Ada.Real_Time.Clock + Period;
   for Count in 1..Iterations loop
      delay until Next_Wakeup;

      Actual_Span (Count) := Ada.Real_Time.Clock - (Next_Wakeup - Period);
      Average_Span := Average_Span + (Actual_Span (Count) / Iterations);
      if Actual_Span (Count) > Max_Span then
         Max_Span := Actual_Span (Count);
      end if;
      if Actual_Span (Count) < Min_Span then
         Min_Span := Actual_Span (Count);
      end if;

      Next_Wakeup := Next_Wakeup + Period;
   end loop;

   Ada.Text_Io.Put_Line ("Average delay is" & Duration'Image (Ada.Real_Time.To_Duration (Average_Span)) & "s");
   Ada.Text_Io.Put_Line ("Maximum delay is" & Duration'Image (Ada.Real_Time.To_Duration (Max_Span)) & "s");
   Ada.Text_Io.Put_Line ("Minimum delay is" & Duration'Image (Ada.Real_Time.To_Duration (Min_Span)) & "s");
end Delay_Until_Test;