
-- PERFORMANCE MEASUREMENT : function GET_LINE timing

with Text_Io;
use Text_Io;
with Remote_Global;
use Remote_Global; -- control optimization
with Iteration; -- obtain stable measurement
with Piwg_Io; -- output results

procedure G000001 is  -- main procedure to execute
    Cpu_Time : Duration; -- CPU time for one feature execution
    Wall_Time : Duration; -- WALL time for one feature execution
    Check_Times : constant := 10; -- inside loop count and check
    Iteration_Count : Integer; -- set and varied by ITERATION package
    Stable : Boolean; -- true when measurement stable
    Scratch_File : File_Type;
    Scratch_String : String (1 .. 20) := "abcdefghijklmnopqrst";
    Scratch_Length : Natural;

begin
--
--  Build file for " GET_LINE " test
--
    Create (Scratch_File, Out_File, "SCRATCH1");
    for Inside_Loop in 1 .. Check_Times loop
        Put_Line (Scratch_File, Scratch_String);
    end loop;
    Close (Scratch_File);
    Open (Scratch_File, In_File, "SCRATCH1");

    Iteration.Start_Control;  -- dummy to bring in pages on some machines

    delay 0.5;  -- wait for stable enviornment on some machines

    Iteration.Initialize (Iteration_Count);

    loop  -- until stable measurement, ITERATION_COUNT increases each time

--
-- Control loop
--
        Iteration.Start_Control;
        for J in 1 .. Iteration_Count loop
            Global := 0;
            for Inside_Loop in 1 .. Check_Times loop
                Global := Global + A_One;
                Remote;  
            end loop;
            Reset (Scratch_File);
        end loop;
        Iteration.Stop_Control (Global, Check_Times);

--
-- Test loop
--
-- establish the time for the GET_LINE pocdedure on 20 character strings

        Iteration.Start_Test;
        for J in 1 .. Iteration_Count loop
            Global := 0;
            for Inside_Loop in 1 .. Check_Times loop
                Global := Global + A_One;
                Remote;  
                Get_Line (Scratch_File, Scratch_String, Scratch_Length);
            end loop;
            Reset (Scratch_File);
        end loop;
        Iteration.Stop_Test (Global, Check_Times);
        Iteration.Test_Stable (Iteration_Count, Stable);
        exit when Stable;
    end loop;
--
    Iteration.Feature_Times (Cpu_Time, Wall_Time);

    Close (Scratch_File);
--
-- Printout
--
    Piwg_Io.Piwg_Output
       ("G000001", "Input/Output", Cpu_Time, Wall_Time, Iteration_Count,
        " TEXT_IO.GET_LINE reading 20 characters, time measured ",
        " A scratch file is written, then read and reset ", " ");

end G000001;