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