aboutsummaryrefslogtreecommitdiff
path: root/src/share/demo/java2d/J2DBench/src/j2dbench/report/TCChartReporter.java
blob: 5a7b98ad479681fc498b72723589518bd9ce5b4e (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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
package j2dbench.report;

import java.io.IOException;
import java.nio.file.DirectoryIteratorException;
import java.nio.file.DirectoryStream;
import java.nio.file.FileSystem;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.Path;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Vector;

/**
 * The class reads J2DBench scores and reports them into output stream in format applicable for TeamCity charts.
 * The directory, where J2DBench result files placed, is specified via command line parameter like follows:
 * <p>
 * <code>-basexml | -b <xml file path></code>
 * </p>
 * This directory must contain one file with pattern <code>*{openjdk}*.{res}</code> which is considered as a container
 * of reference scores and several <code>*{jbsdk}*.{res}</code>.
 * <p>
 * <p> Names of these files have several mandatory fields separated by <code>"_"</code> and look like
 * <code>osName_jdkName_renderName_*.res</code>
 * </p>
 * <p>
 * If any of score is less than corresponding reference value by 5% then exit code <code>1</code> is returned otherwise
 * exit code <code>0</code> is returned.
 * <p>
 * Standard output will contain scores in format required for TeamCity charts.
 * <p>
 * Created by vprovodin on 13/02/2017.
 */
public class TCChartReporter {

    private static final DecimalFormat decimalFormat =
            new DecimalFormat("0.00");

    private static FileSystem defaultFileSystem = FileSystems.getDefault();

    private static double getMeasurementError(String osName) {
        return osName.toLowerCase().contains("linux") ? 0.15: 0.1;
    }

    /**
     * Level at which tests are grouped to be displayed in summary
     */
    private static final int LEVEL = 2;

    /**
     * Holds the groups and corresponding group-display-names
     */
    private static List<String> groups = new ArrayList<>();
    private static Map<String, Double> referenceValues = new HashMap<>();
    private static boolean testFailed = false;

    private static void printUsage() {
        String usage =
                "\njava TCChartReporter [options]      " +
                        "                                      \n\n" +
                        "where options include:                " +
                        "                                      \n" +
                        "    -basexml | -b <xml file path>     " +
                        "path to base-build result";
        System.out.println(usage);
        System.exit(0);
    }

    /**
     * String = getTestResultsTableForSummary()
     */
    private static double generateTestCaseReport(
            Object key,
            Map<String, J2DAnalyzer.ResultHolder> testCaseResult,
            Map<String, Integer> testCaseResultCount) {

        Integer curTestCountObj = testCaseResultCount.get(key.toString());
        int curTestCount = 0;
        if (curTestCountObj != null) {
            curTestCount = curTestCountObj;
        }

        double totalScore = 0;

        for (int i = 0; i < curTestCount; i++) {
            J2DAnalyzer.ResultHolder resultTCR = testCaseResult.get(key.toString() + "_" + i);
            totalScore = totalScore + resultTCR.getScore();
        }

        return totalScore;
    }

    /**
     * Generate Testcase Summary Report for TC - *.out
     */
    private static void generateTestCaseSummaryReport(
            String OJRname,
            Map<String, Double> consoleResult,
            Map<String, J2DAnalyzer.ResultHolder> testCaseResult,
            Map<String, Integer> testCaseResultCount,
            boolean rememberReference) {

        String curGroupName, curTestName;

        Object[] groupNameArray = groups.toArray();

        Object[] testCaseList = consoleResult.keySet().toArray();
        Arrays.sort(testCaseList);

        for (Object aGroupNameArray : groupNameArray) {

            double value;
            curGroupName = aGroupNameArray.toString();

            for (Object aTestCaseList : testCaseList) {

                curTestName = aTestCaseList.toString();

                if (curTestName.contains(curGroupName)) {

                    value = generateTestCaseReport(curTestName, testCaseResult, testCaseResultCount);

                    System.out.println("##teamcity[buildStatisticValue key='" + OJRname + "." + curTestName
                            + "' value='" + decimalFormat.format(value) + "']\n");
                    System.out.println("key='" + OJRname + "." + curTestName
                            + "' value=" + decimalFormat.format(value) + "\n");
                    if (rememberReference) {
                        referenceValues.put(curTestName, value);
                    } else {
                        double refValue = referenceValues.getOrDefault(curTestName, 0.);
                        if (Math.abs(value/refValue - 1) >= getMeasurementError(OJRname)) {
                            System.err.println(OJRname);
                            System.err.println(curTestName);
                            System.err.println("\treferenceValue=" + refValue);
                            System.err.println("\t   actualValue=" + value);
                            System.err.println("\t          diff:" + ((value / refValue - 1) * 100));
                            testFailed = true;
                        }
                    }
                }
            }
        }
    }

    /**
     * main
     */
    public static void main(String args[]) {

        String baseXML = null;
        int group = 2;

        /* ---- Analysis Mode ----
            BEST    = 1;
            WORST   = 2;
            AVERAGE = 3;
            MIDAVG  = 4;
         ------------------------ */
        int analyzerMode = 4;

        try {

            for (int i = 0; i < args.length; i++) {

                if (args[i].startsWith("-basexml") ||
                        args[i].startsWith("-b")) {
                    i++;
                    baseXML = args[i];
                }
            }
        } catch (Exception e) {
            printUsage();
        }

        XMLHTMLReporter.setGroupLevel(group);
        J2DAnalyzer.setMode(analyzerMode);
        if (baseXML != null) {
            generateComparisonReport(defaultFileSystem.getPath(baseXML));
        } else {
            printUsage();
        }

        if (testFailed)
            System.exit(1);
    }

    /**
     * Add Test Group to the list
     */
    private static void addGroup(String testName) {

        String testNameSplit[] = testName.replace('.', '_').split("_");
        StringBuilder group = new StringBuilder(testNameSplit[0]);
        for (int i = 1; i < LEVEL; i++) {
            group.append(".").append(testNameSplit[i]);
        }

        if (!groups.contains(group.toString()))
            groups.add(group.toString());
    }

    private static List<Path> listResFiles(Path dir, String pattern) throws IOException {
        List<Path> result = new ArrayList<>();
        try (DirectoryStream<Path> stream = Files.newDirectoryStream(dir, pattern)) {
            for (Path entry : stream) {
                result.add(entry);
            }
        } catch (DirectoryIteratorException ex) {
            throw ex.getCause();
        }
        return result;
    }

    /**
     * Generate the reports from the base & target result XML
     */
    private static void generateComparisonReport(Path directoryToResFiles) {

        List<Path> jbsdkFiles, openjdkFiles;

        try {
            jbsdkFiles = listResFiles(directoryToResFiles, "*{jbsdk,jbre}*.{res}");
            openjdkFiles = listResFiles(directoryToResFiles, "*{openjdk}*.{res}");
        } catch (IOException e) {
            e.printStackTrace();
            return;
        }

        readScores(openjdkFiles.get(0), true);

        for (Path file : jbsdkFiles) {
            readScores(file, false);
        }
    }

    private static void readScores(Path file, boolean rememberReference) {
        String fileName = file.getName(file.getNameCount() - 1).toString();
        String[] fileNameComponents = fileName.split("_");
        String osName = fileNameComponents[0];
        String jdkName = fileNameComponents[1];
        String renderName = fileNameComponents[2];
        String resultXMLFileName = file.toString();

        J2DAnalyzer.results = new Vector();
        J2DAnalyzer.readResults(resultXMLFileName);
        J2DAnalyzer.SingleResultSetHolder baseSRSH =
                (J2DAnalyzer.SingleResultSetHolder) J2DAnalyzer.results.elementAt(0);
        Enumeration baseEnum_ = baseSRSH.getKeyEnumeration();
        Vector<String> baseKeyvector = new Vector<>();
        while (baseEnum_.hasMoreElements()) {
            baseKeyvector.add((String) baseEnum_.nextElement());
        }
        String baseKeys[] = new String[baseKeyvector.size()];
        baseKeyvector.copyInto(baseKeys);
        J2DAnalyzer.sort(baseKeys);

        Map<String, Double> consoleBaseRes = new HashMap<>();

        Map<String, J2DAnalyzer.ResultHolder> testCaseBaseResult = new HashMap<>();
        Map<String, Integer> testCaseResultCount = new HashMap<>();

        for (String baseKey : baseKeys) {

            J2DAnalyzer.ResultHolder baseTCR =
                    baseSRSH.getResultByKey(baseKey);

            Integer curTestCountObj = testCaseResultCount.get(baseTCR.getName());
            int curTestCount = 0;
            if (curTestCountObj != null) {
                curTestCount = curTestCountObj;
            }
            curTestCount++;
            testCaseBaseResult.put(baseTCR.getName() + "_" + (curTestCount - 1), baseTCR);
            testCaseResultCount.put(baseTCR.getName(), curTestCount);

            addGroup(baseTCR.getName());

            Double curTotalScoreObj = consoleBaseRes.get(baseTCR.getName());
            double curTotalScore = 0;
            if (curTotalScoreObj != null) {
                curTotalScore = curTotalScoreObj;
            }
            curTotalScore = curTotalScore + baseTCR.getScore();
            consoleBaseRes.put(baseTCR.getName(), curTotalScore);
        }

        generateTestCaseSummaryReport(osName + "." + jdkName + "." + renderName,
                consoleBaseRes,
                testCaseBaseResult,
                testCaseResultCount,
                rememberReference);

    }
}