001package Torello.Java.Build;
002
003import static Torello.Java.C.*;
004
005import Torello.Java.StringParse;
006import Torello.Java.StrPrint;
007
008import java.util.Arrays;
009
010/**
011 * This Build-Processor keeps a record of how long the various build-stages take to run to
012 * completion.  That work is done by the methods in this class.
013 * 
014 * <BR />
015 * <EMBED CLASS='external-html' DATA-FILE-ID=INTERNAL_CLASS_NOTE>
016 */
017public class Timers
018{
019    // ********************************************************************************************
020    // ********************************************************************************************
021    // Simple & Private Timer-Arrays
022    // ********************************************************************************************
023    // ********************************************************************************************
024
025
026    // Array for keeping a record of the start-time for steps 2 through 8.  This array will be
027    // INDEX-BY THE STEP-NUMBER (which again, is #2 through #8)
028    //
029    // In all factuality, array index[0] and index[1] are simply not used!  (Note, Stage01 - a.ka.
030    // the 'Compiler' Stage doesn't keep a timer.)
031
032    private final long[] start   = new long[9];
033    private final long[] end     = new long[9];
034
035    Timers()
036    {
037        Arrays.fill(start, 0);
038        Arrays.fill(end, 0);
039    }
040
041
042    // ********************************************************************************************
043    // ********************************************************************************************
044    // Main Timer Methods
045    // ********************************************************************************************
046    // ********************************************************************************************
047
048
049    // RunBuild.java uses this to make sure that start[0] is filled
050    void touch() { start[0] = System.currentTimeMillis(); }
051
052    void startStage02() { start[2] = System.currentTimeMillis(); }
053    void startStage03() { start[3] = System.currentTimeMillis(); }
054    void startStage04() { start[4] = System.currentTimeMillis(); }
055    void startStage05() { start[5] = System.currentTimeMillis(); }
056    void startStage06() { start[6] = System.currentTimeMillis(); }
057    void startStage07() { start[7] = System.currentTimeMillis(); }
058    void startStage08() { start[8] = System.currentTimeMillis(); }
059
060    void endStage02()
061    { end[2] = System.currentTimeMillis();  PRINT_STEP_TIME(2, true); }
062
063    void endStage03()
064    { end[3] = System.currentTimeMillis();  PRINT_STEP_TIME(3, true); }
065
066    void endStage04()
067    { end[4] = System.currentTimeMillis();  PRINT_STEP_TIME(4, true); }
068
069    void endStage05()
070    { end[5] = System.currentTimeMillis();  PRINT_STEP_TIME(5, true); }
071
072    void endStage06()
073    { end[6] = System.currentTimeMillis();  PRINT_STEP_TIME(6, true); }
074
075    void endStage07()
076    { end[7] = System.currentTimeMillis();  PRINT_STEP_TIME(7, true); }
077
078    void endStage08()
079    { end[8] = System.currentTimeMillis();  PRINT_STEP_TIME(8, true); }
080
081
082    // ********************************************************************************************
083    // ********************************************************************************************
084    // Printers
085    // ********************************************************************************************
086    // ********************************************************************************************
087
088
089    // This is an adjustment for the length of the output nChars of spaces.  These characters
090    // cannot be seen on the screen.
091
092    private static final int PRN_LEN_1 = (BPURPLE_BKGND + RESET + BCYAN + RESET).length();
093
094    private void PRINT_STEP_TIME(final int stage, final boolean printStars)
095    {
096        if ((stage < 2) || (stage > 8)) throw new Error("Stage Value Parameter was: " + stage);
097
098        String stepStr =
099            BPURPLE_BKGND + " " + RESET + " Stage " + stage + ": " + 
100            BCYAN + StrPrint.commas(end[stage] - start[stage]) + RESET +
101            " milli-seconds, " + Printing.STAGES.get(stage);
102
103        System.out.println(
104            (printStars ? (Printing.TIMER_SUMMARY_LINE + '\n') : "") +
105            stepStr +
106
107            // NOTE: The extra '-1' is to get the closing '*' on the last character, not after it.
108            StringParse.nChars(' ', Printing.LEN - stepStr.length() + PRN_LEN_1 - 1) +
109            BPURPLE_BKGND + " " + RESET +
110
111            (printStars ? ('\n' + Printing.TIMER_SUMMARY_LINE + '\n') : "")
112        );
113    }
114
115    // As per above, this is an 'nChars' adjustment.  These characters cannot be seen on the screen
116    private static final int PRN_LEN_2 = (BPURPLE_BKGND + RESET + BCYAN + RESET).length();
117
118    // Called by Build.java at the very end of the main method
119    void PRINT_TIMES()
120    {
121        System.out.println('\n' + Printing.TIMER_SUMMARY_LINE);
122
123        boolean printedAtLeastOne = false;
124
125        for (int i=2; i <=8; i++)
126
127            if (start[i] != 0)
128            {
129                PRINT_STEP_TIME(i, false);
130                printedAtLeastOne = true;
131            }
132
133        long total = System.currentTimeMillis() - start[0];
134
135        String summaryStr =
136            BPURPLE_BKGND + " " + RESET + " TOTAL BUILD TIME: " +
137            BCYAN + StrPrint.commas(total) + RESET + " milli-seconds";
138
139        System.out.println(
140            (printedAtLeastOne ? (Printing.TIMER_SUMMARY_LINE + '\n') : "") +
141            summaryStr +
142
143            // NOTE: The extra '-1' is to get the closing ' ' on the last character, not after it.
144            StringParse.nChars(' ', Printing.LEN - summaryStr.length() + PRN_LEN_2 - 1) +
145
146            BPURPLE_BKGND + ' ' + RESET + '\n' +
147            Printing.TIMER_SUMMARY_LINE + '\n'
148        );
149    }
150}