summaryrefslogtreecommitdiff
path: root/src/main/java/com/android/vts/job/VtsPerformanceJobServlet.java
blob: ebd529452b4524d53c85ed9e35d1e96ce0b11fd7 (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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
/*
 * 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.job;

import com.android.vts.entity.TestEntity;
import com.android.vts.util.EmailHelper;
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 com.android.vts.util.TaskQueueHelper;
import com.google.appengine.api.datastore.DatastoreService;
import com.google.appengine.api.datastore.DatastoreServiceFactory;
import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;
import com.google.appengine.api.datastore.Query;
import com.google.appengine.api.taskqueue.Queue;
import com.google.appengine.api.taskqueue.QueueFactory;
import com.google.appengine.api.taskqueue.TaskOptions;
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 java.util.logging.Logger;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/** Represents the notifications service which is automatically called on a fixed schedule. */
public class VtsPerformanceJobServlet extends BaseJobServlet {
    protected static final Logger logger =
            Logger.getLogger(VtsPerformanceJobServlet.class.getName());

    private static final String PERFORMANCE_JOB_URL = "/cron/vts_performance_job";
    private static final String MEAN = "Mean";
    private static final String MAX = "Max";
    private static final String MIN = "Min";
    private static final String MIN_DELTA = "ΔMin (%)";
    private static final String MAX_DELTA = "ΔMax (%)";
    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 String SUBJECT_PREFIX = "Daily Performance Digest: ";
    private static final String LAST_WEEK = "Last Week";
    private static final String LABEL_STYLE = "font-family: arial";
    private static final String SUBTEXT_STYLE = "font-family: arial; font-size: 12px";
    private static final String TABLE_STYLE =
            "width: 100%; border-collapse: collapse; border: 1px solid black; font-size: 12px; font-family: arial;";
    private static final String SECTION_LABEL_STYLE =
            "border: 1px solid black; border-bottom: none; background-color: lightgray;";
    private static final String COL_LABEL_STYLE =
            "border: 1px solid black; border-bottom-width: 2px; border-top: 1px dotted gray; background-color: lightgray;";
    private static final String HEADER_COL_STYLE =
            "border-top: 1px dotted gray; border-right: 2px solid black; text-align: right; background-color: lightgray;";
    private static final String INNER_CELL_STYLE =
            "border-top: 1px dotted gray; border-right: 1px dotted gray; text-align: right;";
    private static final String OUTER_CELL_STYLE =
            "border-top: 1px dotted gray; border-right: 2px solid black; text-align: right;";

    private static final DecimalFormat FORMATTER;

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

    /**
     * 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 testName The name of the test whose profiling data to summarize.
     * @param perfSummaries List of PerformanceSummary objects for each profiling run (in reverse
     *     chronological order).
     * @param labels List of string labels for use as the column headers.
     * @returns An HTML string containing labeled table summaries.
     */
    public static String getPerformanceSummary(
            String testName, List<PerformanceSummary> perfSummaries, List<String> labels) {
        if (perfSummaries.size() == 0) return "";
        PerformanceSummary now = perfSummaries.get(0);
        String tableHTML = "<p style='" + LABEL_STYLE + "'><b>";
        tableHTML += testName + "</b></p>";
        for (String profilingPoint : now.getProfilingPointNames()) {
            ProfilingPointSummary summary = now.getProfilingPointSummary(profilingPoint);
            tableHTML += "<table cellpadding='2' style='" + TABLE_STYLE + "'>";

            // Format header rows
            String[] headerRows = new String[] {profilingPoint, summary.yLabel};
            int colspan = labels.size() * 4;
            for (String content : headerRows) {
                tableHTML += "<tr><td colspan='" + colspan + "'>" + content + "</td></tr>";
            }

            // Format section labels
            tableHTML += "<tr>";
            for (int i = 0; i < labels.size(); i++) {
                String content = labels.get(i);
                tableHTML += "<th style='" + SECTION_LABEL_STYLE + "' ";
                if (i == 0) tableHTML += "colspan='1'";
                else if (i == 1) tableHTML += "colspan='3'";
                else tableHTML += "colspan='4'";
                tableHTML += ">" + content + "</th>";
            }
            tableHTML += "</tr>";

            String deltaString;
            String bestCaseString;
            String subtext;
            switch (now.getProfilingPointSummary(profilingPoint).getRegressionMode()) {
                case VTS_REGRESSION_MODE_DECREASING:
                    deltaString = MAX_DELTA;
                    bestCaseString = MAX;
                    subtext = HIGHER_IS_BETTER;
                    break;
                default:
                    deltaString = MIN_DELTA;
                    bestCaseString = MIN;
                    subtext = LOWER_IS_BETTER;
                    break;
            }

            // Format column labels
            tableHTML += "<tr>";
            for (int i = 0; i < labels.size(); i++) {
                if (i > 1) {
                    tableHTML += "<th style='" + COL_LABEL_STYLE + "'>" + deltaString + "</th>";
                }
                if (i == 0) {
                    tableHTML += "<th style='" + COL_LABEL_STYLE + "'>";
                    tableHTML += summary.xLabel + "</th>";
                } else if (i > 0) {
                    tableHTML += "<th style='" + COL_LABEL_STYLE + "'>" + bestCaseString + "</th>";
                    tableHTML += "<th style='" + COL_LABEL_STYLE + "'>" + MEAN + "</th>";
                    tableHTML += "<th style='" + COL_LABEL_STYLE + "'>" + STD + "</th>";
                }
            }
            tableHTML += "</tr>";

            // Populate data cells
            for (StatSummary stats : summary) {
                String label = stats.getLabel();
                tableHTML += "<tr><td style='" + HEADER_COL_STYLE + "'>" + label;
                tableHTML += "</td><td style='" + INNER_CELL_STYLE + "'>";
                tableHTML += FORMATTER.format(stats.getBestCase()) + "</td>";
                tableHTML += "<td style='" + INNER_CELL_STYLE + "'>";
                tableHTML += FORMATTER.format(stats.getMean()) + "</td>";
                tableHTML += "<td style='" + OUTER_CELL_STYLE + "'>";
                if (stats.getCount() < 2) {
                    tableHTML += " - </td>";
                } else {
                    tableHTML += FORMATTER.format(stats.getStd()) + "</td>";
                }
                for (int i = 1; i < perfSummaries.size(); i++) {
                    PerformanceSummary oldPerfSummary = perfSummaries.get(i);
                    if (oldPerfSummary.hasProfilingPoint(profilingPoint)) {
                        StatSummary baseline =
                                oldPerfSummary
                                        .getProfilingPointSummary(profilingPoint)
                                        .getStatSummary(label);
                        tableHTML +=
                                PerformanceUtil.getBestCasePerformanceComparisonHTML(
                                        baseline,
                                        stats,
                                        "",
                                        "",
                                        INNER_CELL_STYLE,
                                        OUTER_CELL_STYLE);
                    } else tableHTML += "<td></td><td></td><td></td><td></td>";
                }
                tableHTML += "</tr>";
            }
            tableHTML += "</table>";
            tableHTML += "<i style='" + SUBTEXT_STYLE + "'>" + subtext + "</i><br><br>";
        }
        return tableHTML;
    }

    @Override
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
        DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
        Queue queue = QueueFactory.getDefaultQueue();
        Query q = new Query(TestEntity.KIND).setKeysOnly();
        List<TaskOptions> tasks = new ArrayList<>();
        for (Entity test : datastore.prepare(q).asIterable()) {
            if (test.getKey().getName() == null) {
                continue;
            }
            TaskOptions task =
                    TaskOptions.Builder.withUrl(PERFORMANCE_JOB_URL)
                            .param("testKey", KeyFactory.keyToString(test.getKey()))
                            .method(TaskOptions.Method.POST);
            tasks.add(task);
        }
        TaskQueueHelper.addToQueue(queue, tasks);
    }

    @Override
    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws IOException {
        String testKeyString = request.getParameter("testKey");
        Key testKey;
        try {
            testKey = KeyFactory.stringToKey(testKeyString);
        } catch (IllegalArgumentException e) {
            logger.log(Level.WARNING, "Invalid key specified: " + testKeyString);
            return;
        }

        long nowMicro = TimeUnit.MILLISECONDS.toMicros(System.currentTimeMillis());

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

        // Add yesterday as a baseline time interval for analysis
        long oneDayAgo = nowMicro - 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 = nowMicro - 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(
                testKey.getName(), oneWeekAgo - oneWeek, nowMicro, null, summaries);

        List<PerformanceSummary> nonEmptySummaries = new ArrayList<>();
        List<String> labels = new ArrayList<>();
        labels.add("");
        for (PerformanceSummary perfSummary : summaries) {
            if (perfSummary.size() == 0) continue;
            nonEmptySummaries.add(perfSummary);
            labels.add(perfSummary.label);
        }
        String body = getPerformanceSummary(testKey.getName(), nonEmptySummaries, labels);
        if (body == null || body.equals("")) {
            return;
        }
        List<String> emails = EmailHelper.getSubscriberEmails(testKey);
        if (emails.size() == 0) {
            return;
        }
        String subject = SUBJECT_PREFIX + testKey.getName();
        EmailHelper.send(emails, subject, body);
    }
}