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
package Torello.Java.Build;

import static Torello.Java.C.*;

import Torello.Java.StringParse;

import java.util.Arrays;

/**
 * This Build-Processor keeps a record of how long the various build-stages take to run to
 * completion.  That work is done by the methods in this class.
 * 
 * <BR />
 * <EMBED CLASS='external-html' DATA-FILE-ID=INTERNAL_CLASS_NOTE>
 */
public class Timers
{
    // ********************************************************************************************
    // ********************************************************************************************
    // Simple & Private Timer-Arrays
    // ********************************************************************************************
    // ********************************************************************************************


    // Array for keeping a record of the start-time for steps 2 through 8.  This array will be
    // INDEX-BY THE STEP-NUMBER (which again, is #2 through #8)
    //
    // In all factuality, array index[0] and index[1] are simply not used!  (Note, Stage01 - a.ka.
    // the 'Compiler' Stage doesn't keep a timer.)

    private final long[] start   = new long[9];
    private final long[] end     = new long[9];

    Timers()
    {
        Arrays.fill(start, 0);
        Arrays.fill(end, 0);
    }


    // ********************************************************************************************
    // ********************************************************************************************
    // Main Timer Methods
    // ********************************************************************************************
    // ********************************************************************************************


    // RunBuild.java uses this to make sure that start[0] is filled
    void touch() { start[0] = System.currentTimeMillis(); }

    void startStage02() { start[2] = System.currentTimeMillis(); }
    void startStage03() { start[3] = System.currentTimeMillis(); }
    void startStage04() { start[4] = System.currentTimeMillis(); }
    void startStage05() { start[5] = System.currentTimeMillis(); }
    void startStage06() { start[6] = System.currentTimeMillis(); }
    void startStage07() { start[7] = System.currentTimeMillis(); }
    void startStage08() { start[8] = System.currentTimeMillis(); }

    void endStage02()
    { end[2] = System.currentTimeMillis();  PRINT_STEP_TIME(2, true); }

    void endStage03()
    { end[3] = System.currentTimeMillis();  PRINT_STEP_TIME(3, true); }

    void endStage04()
    { end[4] = System.currentTimeMillis();  PRINT_STEP_TIME(4, true); }

    void endStage05()
    { end[5] = System.currentTimeMillis();  PRINT_STEP_TIME(5, true); }

    void endStage06()
    { end[6] = System.currentTimeMillis();  PRINT_STEP_TIME(6, true); }

    void endStage07()
    { end[7] = System.currentTimeMillis();  PRINT_STEP_TIME(7, true); }

    void endStage08()
    { end[8] = System.currentTimeMillis();  PRINT_STEP_TIME(8, true); }


    // ********************************************************************************************
    // ********************************************************************************************
    // Printers
    // ********************************************************************************************
    // ********************************************************************************************


    // This is an adjustment for the length of the output nChars of spaces.  These characters
    // cannot be seen on the screen.

    private static final int PRN_LEN_1 = (BPURPLE_BKGND + RESET + BCYAN + RESET).length();

    private void PRINT_STEP_TIME(final int stage, final boolean printStars)
    {
        if ((stage < 2) || (stage > 8)) throw new Error("Stage Value Parameter was: " + stage);

        String stepStr =
            BPURPLE_BKGND + " " + RESET + " Stage " + stage + ": " + 
            BCYAN + StringParse.commas(end[stage] - start[stage]) + RESET +
            " milli-seconds, " + Printing.STAGES.get(stage);

        System.out.println(
            (printStars ? (Printing.TIMER_SUMMARY_LINE + '\n') : "") +
            stepStr +

            // NOTE: The extra '-1' is to get the closing '*' on the last character, not after it.
            StringParse.nChars(' ', Printing.LEN - stepStr.length() + PRN_LEN_1 - 1) +
            BPURPLE_BKGND + " " + RESET +

            (printStars ? ('\n' + Printing.TIMER_SUMMARY_LINE + '\n') : "")
        );
    }

    // As per above, this is an 'nChars' adjustment.  These characters cannot be seen on the screen
    private static final int PRN_LEN_2 = (BPURPLE_BKGND + RESET + BCYAN + RESET).length();

    // Called by Build.java at the very end of the main method
    void PRINT_TIMES()
    {
        System.out.println('\n' + Printing.TIMER_SUMMARY_LINE);

        boolean printedAtLeastOne = false;

        for (int i=2; i <=8; i++)

            if (start[i] != 0)
            {
                PRINT_STEP_TIME(i, false);
                printedAtLeastOne = true;
            }

        long total = System.currentTimeMillis() - start[0];

        String summaryStr =
            BPURPLE_BKGND + " " + RESET + " TOTAL BUILD TIME: " +
            BCYAN + StringParse.commas(total) + RESET + " milli-seconds";

        System.out.println(
            (printedAtLeastOne ? (Printing.TIMER_SUMMARY_LINE + '\n') : "") +
            summaryStr +

            // NOTE: The extra '-1' is to get the closing ' ' on the last character, not after it.
            StringParse.nChars(' ', Printing.LEN - summaryStr.length() + PRN_LEN_2 - 1) +

            BPURPLE_BKGND + ' ' + RESET + '\n' +
            Printing.TIMER_SUMMARY_LINE + '\n'
        );
    }
}