AuroraWrapper.java revision 1088:7e62d98d4625
1/*
2 * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.  Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26package jdk.nashorn.internal.performance;
27
28import java.io.File;
29import java.io.FileOutputStream;
30import java.io.IOException;
31import javax.xml.parsers.DocumentBuilder;
32import javax.xml.parsers.DocumentBuilderFactory;
33import javax.xml.parsers.ParserConfigurationException;
34import javax.xml.transform.OutputKeys;
35import javax.xml.transform.Transformer;
36import javax.xml.transform.TransformerConfigurationException;
37import javax.xml.transform.TransformerException;
38import javax.xml.transform.TransformerFactory;
39import javax.xml.transform.dom.DOMSource;
40import javax.xml.transform.stream.StreamResult;
41import org.w3c.dom.Document;
42import org.w3c.dom.Element;
43import org.w3c.dom.NodeList;
44import org.xml.sax.SAXException;
45
46@SuppressWarnings("javadoc")
47public class AuroraWrapper {
48
49    public static String fileName = "report.xml";
50
51    public static void deleteReportDocument() {
52        final File f = new File(fileName);
53        if (f.exists()) {
54            f.delete();
55        }
56    }
57
58    public static Document createOrOpenDocument() throws ParserConfigurationException, SAXException, IOException {
59        final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
60        final DocumentBuilder documentBuilder = factory.newDocumentBuilder();
61
62        Document document;
63        final File f = new File(fileName);
64        if (!f.exists()) {
65            document = documentBuilder.newDocument();
66            final Element root = document.createElement("entity");
67            document.appendChild(root);
68            root.setAttribute("type", "REFWORKLOADRUN");
69            root.setAttribute("name", "default");
70        } else {
71            document = documentBuilder.parse(f);
72        }
73
74        return document;
75    }
76
77    public static void addBenchmarkResults(final Document doc, final Element root, final String name, final String score, final String higherBetter) {
78        final Element results = addEntity(doc, root, name, "BENCHMARK_RESULTS");
79        addAttribute(doc, results, "benchmark", name);
80        addAttribute(doc, results, "is_higher_better", higherBetter);
81
82        final Element iteration = addEntity(doc, results, "1", "ITERATION");
83        addAttribute(doc, iteration, "score", score);
84        addAttribute(doc, iteration, "successful", "true");
85
86        addConfig(doc, results, name);
87    }
88
89    public static Element getRootEntity(final org.w3c.dom.Document doc) {
90        final Element rootEntity = doc.getDocumentElement();
91        Element resultsEntity = null;
92
93        final NodeList entities = rootEntity.getChildNodes();
94        for (int i = 0; i < entities.getLength(); i++) {
95            if (entities.item(i).getNodeName().equals("entity")) {
96                resultsEntity = (Element)entities.item(i);
97                break;
98            }
99        }
100
101        if (resultsEntity == null) {
102            resultsEntity = addResults(doc);
103        }
104        //System.out.println(resultsEntity);
105        return resultsEntity;
106    }
107
108    public static void addAttribute(final Document doc, final Element entity, final String attributeName, final String attributeValue) {
109        final Element attr = doc.createElement("attribute");
110        entity.appendChild(attr);
111        attr.setAttribute("name", attributeName);
112        attr.setTextContent(attributeValue);
113    }
114
115    public static Element addEntity(final Document doc, final Element entity, final String entityName, final String entityType) {
116        final Element newEntity = doc.createElement("entity");
117        entity.appendChild(newEntity);
118
119        if (entityType != null) {
120            newEntity.setAttribute("type", entityType);
121        }
122        if (entityName != null) {
123            newEntity.setAttribute("name", entityName);
124        }
125        return newEntity;
126    }
127
128    public static Element addResults(final Document doc) {
129
130        String _benchmark = "nashorn-octaneperf";
131
132        final String vmName = java.lang.System.getProperties().getProperty("java.vm.name");
133        try {
134            String vmType;
135            if (vmName.toLowerCase().contains("client")) {
136                vmType = "client";
137            } else {
138                vmType = "server";
139            }
140            _benchmark += "-" + vmType;
141        } catch (final Exception e) {
142            // In case VM name has different format
143        }
144
145        final Element root = doc.getDocumentElement();
146
147        final Element result = doc.createElement("entity");
148
149        root.appendChild(result);
150        result.setAttribute("name", _benchmark);
151        result.setAttribute("type", "BENCHMARK_RESULTS");
152
153        addAttribute(doc, result, "benchmark", _benchmark);
154        addAttribute(doc, result, "score", "0");
155        addAttribute(doc, result, "mean", "0");
156        addAttribute(doc, result, "stdev", "0");
157        addAttribute(doc, result, "var", "0");
158        addAttribute(doc, result, "attempts", "1");
159        addAttribute(doc, result, "successes", "1");
160        addAttribute(doc, result, "failures", "0");
161        addAttribute(doc, result, "jvmOptions", "");
162        addAttribute(doc, result, "is_workload", "0");
163        addAttribute(doc, result, "is_higher_better", "1");
164
165        addConfig(doc, result, _benchmark);
166
167        final Element iteration = addEntity(doc, result, "1", "ITERATION");
168        addAttribute(doc, iteration, "score", "0");
169        addAttribute(doc, iteration, "successful", "true");
170
171        return result;
172    }
173
174    public static void addConfig(final Document doc, final Element result, final String _benchmark) {
175        final Element config = addEntity(doc, result, "default", "BENCHMARK_CONFIG");
176        addAttribute(doc, config, "settings", "benchmarks=" + _benchmark + "\ncomponent=j2se\niterations=1\n");
177        addAttribute(doc, config, "info", "");
178    }
179
180    public static void addResults(final Document doc, final String _benchmark, final String _score) throws TransformerConfigurationException, TransformerException, IOException {
181        final Element result = getRootEntity(doc);
182
183        addBenchmarkResults(doc, result, _benchmark, _score, "1");
184
185        final TransformerFactory tranformerFactory = TransformerFactory.newInstance();
186        final Transformer tr = tranformerFactory.newTransformer();
187        tr.setOutputProperty(OutputKeys.INDENT, "yes");
188        try (FileOutputStream fos = new FileOutputStream(fileName)) {
189            tr.transform(new DOMSource(doc), new StreamResult(fos));
190        }
191    }
192
193    /**
194     * Test
195     */
196    @SuppressWarnings("UseSpecificCatch")
197    public static void main(final String... args) {
198        try {
199            deleteReportDocument();
200            Document document = createOrOpenDocument();
201            addResults(document, "benchmark1", "0.01");
202            document = createOrOpenDocument();
203            addResults(document, "benchmark2", "0.02");
204            document = createOrOpenDocument();
205            addResults(document, "benchmark3", "0.03");
206
207            final TransformerFactory tranformerFactory = TransformerFactory.newInstance();
208            final Transformer tr = tranformerFactory.newTransformer();
209            tr.setOutputProperty(OutputKeys.INDENT, "yes");
210            tr.transform(new DOMSource(document), new StreamResult(System.out));
211        } catch (final Exception e) {
212            e.printStackTrace();
213        }
214    }
215}
216