summaryrefslogtreecommitdiff
path: root/src/main/java/com/android/vts/servlet/ShowPerformanceDigestServlet.java
blob: 5a70738cfab5b3ecaf702f662c4f44261bdad1db (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
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
/*
 * Copyright (c) 2016 Google Inc. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you
 * may not use this file except in compliance with the License. You may
 * obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
 * implied. See the License for the specific language governing
 * permissions and limitations under the License.
 */

package com.android.vts.servlet;

import com.android.vts.util.DatastoreHelper;
import com.android.vts.util.PerformanceSummary;
import com.android.vts.util.PerformanceUtil;
import com.android.vts.util.ProfilingPointSummary;
import com.android.vts.util.StatSummary;
import java.io.IOException;
import java.math.RoundingMode;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.logging.Level;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/** Servlet for producing tabular performance summaries. */
public class ShowPerformanceDigestServlet extends BaseServlet {
    private static final String PERF_DIGEST_JSP = "WEB-INF/jsp/show_performance_digest.jsp";

    private static final String MEAN = "Mean";
    private static final String MIN = "Min";
    private static final String MAX = "Max";
    private static final String MEAN_DELTA = "ΔMean (%)";
    private static final String HIGHER_IS_BETTER =
            "Note: Higher values are better. Maximum is the best-case performance.";
    private static final String LOWER_IS_BETTER =
            "Note: Lower values are better. Minimum is the best-case performance.";
    private static final String STD = "Std";

    private static final DecimalFormat FORMATTER;

    /** Initialize the decimal formatter. */
    static {
        FORMATTER = new DecimalFormat("#.##");
        FORMATTER.setRoundingMode(RoundingMode.HALF_UP);
    }

    @Override
    public PageType getNavParentType() {
        return PageType.TOT;
    }

    @Override
    public List<Page> getBreadcrumbLinks(HttpServletRequest request) {
        List<Page> links = new ArrayList<>();
        String testName = request.getParameter("testName");
        links.add(new Page(PageType.TABLE, testName, "?testName=" + testName));
        links.add(new Page(PageType.PERFORMANCE_DIGEST, "?testName=" + testName));
        return links;
    }

    /**
     * Generates an HTML summary of the performance changes for the profiling results in the
     * specified table.
     *
     * <p>Retrieves the past 24 hours of profiling data and compares it to the 24 hours that
     * preceded it. Creates a table representation of the mean and standard deviation for each
     * profiling point. When performance degrades, the cell is shaded red.
     *
     * @param profilingPoint The name of the profiling point to summarize.
     * @param testSummary The ProfilingPointSummary object to compare against.
     * @param perfSummaries List of PerformanceSummary objects for each profiling run (in reverse
     *     chronological order).
     * @param sectionLabels HTML string for the section labels (i.e. for each time interval).
     * @returns An HTML string for a table comparing the profiling point results across time
     *     intervals.
     */
    public static String getPerformanceSummary(
            String profilingPoint,
            ProfilingPointSummary testSummary,
            List<PerformanceSummary> perfSummaries,
            String sectionLabels) {
        String tableHTML = "<table>";

        // Format section labels
        tableHTML += "<tr>";
        tableHTML += "<th class='section-label grey lighten-2'>";
        tableHTML += testSummary.yLabel + "</th>";
        tableHTML += sectionLabels;
        tableHTML += "</tr>";

        String bestCaseString;
        switch (testSummary.getRegressionMode()) {
            case VTS_REGRESSION_MODE_DECREASING:
                bestCaseString = MAX;
                break;
            default:
                bestCaseString = MIN;
                break;
        }

        // Format column labels
        tableHTML += "<tr>";
        for (int i = 0; i <= perfSummaries.size() + 1; i++) {
            if (i > 1) {
                tableHTML += "<th class='section-label grey lighten-2'>" + MEAN_DELTA + "</th>";
            }
            if (i == 0) {
                tableHTML += "<th class='section-label grey lighten-2'>";
                tableHTML += testSummary.xLabel + "</th>";
            } else if (i > 0) {
                tableHTML += "<th class='section-label grey lighten-2'>" + bestCaseString + "</th>";
                tableHTML += "<th class='section-label grey lighten-2'>" + MEAN + "</th>";
                tableHTML += "<th class='section-label grey lighten-2'>" + STD + "</th>";
            }
        }
        tableHTML += "</tr>";

        // Populate data cells
        for (StatSummary stats : testSummary) {
            String label = stats.getLabel();
            tableHTML += "<tr><td class='axis-label grey lighten-2'>" + label;
            tableHTML += "</td><td class='cell inner-cell'>";
            tableHTML += FORMATTER.format(stats.getBestCase()) + "</td>";
            tableHTML += "<td class='cell inner-cell'>";
            tableHTML += FORMATTER.format(stats.getMean()) + "</td>";
            tableHTML += "<td class='cell outer-cell'>";
            if (stats.getCount() < 2) {
                tableHTML += " - </td>";
            } else {
                tableHTML += FORMATTER.format(stats.getStd()) + "</td>";
            }
            for (PerformanceSummary prevPerformance : perfSummaries) {
                if (prevPerformance.hasProfilingPoint(profilingPoint)) {
                    StatSummary baseline =
                            prevPerformance
                                    .getProfilingPointSummary(profilingPoint)
                                    .getStatSummary(label);
                    tableHTML +=
                            PerformanceUtil.getAvgCasePerformanceComparisonHTML(
                                    baseline, stats, "cell inner-cell", "cell outer-cell", "", "");
                } else {
                    tableHTML += "<td></td><td></td><td></td><td></td>";
                }
            }
            tableHTML += "</tr>";
        }
        tableHTML += "</table>";
        return tableHTML;
    }

    @Override
    public void doGetHandler(HttpServletRequest request, HttpServletResponse response)
            throws IOException {
        RequestDispatcher dispatcher = null;
        String testName = request.getParameter("testName");
        String selectedDevice = request.getParameter("device");
        Long startTime = null;
        if (request.getParameter("startTime") != null) {
            String time = request.getParameter("startTime");
            try {
                startTime = Long.parseLong(time);
            } catch (NumberFormatException e) {
                logger.log(Level.WARNING, "Invalid start time passed to digest servlet: " + time);
            }
        }
        if (startTime == null) {
            startTime = TimeUnit.MILLISECONDS.toMicros(System.currentTimeMillis());
        }

        // Add today to the list of time intervals to analyze
        List<PerformanceSummary> summaries = new ArrayList<>();
        PerformanceSummary today =
                new PerformanceSummary(startTime - TimeUnit.DAYS.toMicros(1), startTime);
        summaries.add(today);

        // Add yesterday as a baseline time interval for analysis
        long oneDayAgo = startTime - TimeUnit.DAYS.toMicros(1);
        PerformanceSummary yesterday =
                new PerformanceSummary(oneDayAgo - TimeUnit.DAYS.toMicros(1), oneDayAgo);
        summaries.add(yesterday);

        // Add last week as a baseline time interval for analysis
        long oneWeek = TimeUnit.DAYS.toMicros(7);
        long oneWeekAgo = startTime - oneWeek;
        String spanString = "<span class='date-label'>";
        String label =
                spanString + TimeUnit.MICROSECONDS.toMillis(oneWeekAgo - oneWeek) + "</span>";
        label += " - " + spanString + TimeUnit.MICROSECONDS.toMillis(oneWeekAgo) + "</span>";
        PerformanceSummary lastWeek =
                new PerformanceSummary(oneWeekAgo - oneWeek, oneWeekAgo, label);
        summaries.add(lastWeek);
        PerformanceUtil.updatePerformanceSummary(
                testName, oneWeekAgo - oneWeek, startTime, selectedDevice, summaries);

        int colCount = 0;
        String sectionLabels = "";
        List<PerformanceSummary> nonEmptySummaries = new ArrayList<>();
        for (PerformanceSummary perfSummary : summaries) {
            if (perfSummary.size() == 0) continue;

            nonEmptySummaries.add(perfSummary);
            String content = perfSummary.label;
            sectionLabels += "<th class='section-label grey lighten-2 center' ";
            if (colCount++ == 0) sectionLabels += "colspan='3'";
            else sectionLabels += "colspan='4'";
            sectionLabels += ">" + content + "</th>";
        }

        List<String> tables = new ArrayList<>();
        List<String> tableTitles = new ArrayList<>();
        List<String> tableSubtitles = new ArrayList<>();
        if (nonEmptySummaries.size() != 0) {
            PerformanceSummary todayPerformance = nonEmptySummaries.remove(0);
            String[] profilingNames = todayPerformance.getProfilingPointNames();

            for (String profilingPointName : profilingNames) {
                ProfilingPointSummary baselinePerformance =
                        todayPerformance.getProfilingPointSummary(profilingPointName);
                String table =
                        getPerformanceSummary(
                                profilingPointName,
                                baselinePerformance,
                                nonEmptySummaries,
                                sectionLabels);
                if (table != null) {
                    tables.add(table);
                    tableTitles.add(profilingPointName);
                    switch (baselinePerformance.getRegressionMode()) {
                        case VTS_REGRESSION_MODE_DECREASING:
                            tableSubtitles.add(HIGHER_IS_BETTER);
                            break;
                        default:
                            tableSubtitles.add(LOWER_IS_BETTER);
                            break;
                    }
                }
            }
        }

        request.setAttribute("testName", testName);
        request.setAttribute("tables", tables);
        request.setAttribute("tableTitles", tableTitles);
        request.setAttribute("tableSubtitles", tableSubtitles);
        request.setAttribute("startTime", Long.toString(startTime));
        request.setAttribute("selectedDevice", selectedDevice);
        request.setAttribute("devices", DatastoreHelper.getAllBuildFlavors());

        dispatcher = request.getRequestDispatcher(PERF_DIGEST_JSP);
        try {
            dispatcher.forward(request, response);
        } catch (ServletException e) {
            logger.log(Level.SEVERE, "Servlet Exception caught : " + e.toString());
        }
    }
}