00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031 package org.objectweb.proactive.benchmarks.timit.result;
00032
00033 import java.util.Iterator;
00034
00035 import org.jdom.Attribute;
00036 import org.jdom.Document;
00037 import org.jdom.Element;
00038 import org.jdom.filter.ElementFilter;
00039 import org.objectweb.proactive.benchmarks.timit.util.BenchmarkStatistics;
00040 import org.objectweb.proactive.benchmarks.timit.util.EventStatistics;
00041 import org.objectweb.proactive.benchmarks.timit.util.HierarchicalTimerStatistics;
00042 import org.objectweb.proactive.benchmarks.timit.util.XMLHelper;
00043
00050 public class BenchmarkResultWriter {
00051
00052 private Document document;
00053
00054 private Element eTimit;
00055
00056 private String filename;
00057
00064 public BenchmarkResultWriter(String filename) {
00065 this.eTimit = new Element("timit");
00066 this.document = new Document(this.eTimit);
00067 this.filename = filename;
00068 }
00069
00073 public Element getRoot() {
00074 return this.eTimit;
00075 }
00076
00086 public void addResult(BenchmarkStatistics bstats, String name) {
00087 Element benchResult = new Element("BenchmarkStatistics");
00088 this.eTimit.addContent(benchResult);
00089 benchResult.setAttribute(new Attribute("name", name));
00090 benchResult.setAttribute(new Attribute("date", ""
00091 + (new java.sql.Timestamp(System.currentTimeMillis()))));
00092
00093
00094 Element eTimers = new Element("timers");
00095 benchResult.addContent(eTimers);
00096 this.fillTimersResults(eTimers, bstats.getTimerStatistics());
00097
00098
00099 Element eEvents = new Element("events");
00100 benchResult.addContent(eEvents);
00101 this.fillEventsResults(eEvents, bstats.getEventsStatistics());
00102
00103
00104 Element informationElement = new Element("Information");
00105 informationElement.addContent("" + bstats.getInformation());
00106 benchResult.addContent(informationElement);
00107 }
00108
00113 public void writeResult() {
00114
00115 XMLHelper.writeFile(this.document, this.filename);
00116 }
00117
00122 public void removeExtremums() {
00123 Document doc = XMLHelper.readFile(this.filename);
00124 Element root = doc.getRootElement();
00125 Iterator<Element> it = root.getDescendants(new ElementFilter("timers"));
00126
00127
00128 double vMax = Double.MIN_VALUE;
00129 double vMin = Double.MAX_VALUE;
00130 Element max = null;
00131 Element min = null;
00132 int size = 0;
00133
00134 while( it.hasNext() ) {
00135 size++;
00136 Element timers = it.next();
00137 Element eTimer = timers.getChild("timer");
00138 if( eTimer == null ) { continue; }
00139 double value = Double.valueOf(eTimer.getAttributeValue("avg"));
00140 if( value > vMax ) {
00141 vMax = value;
00142 max = timers;
00143 }
00144 if( value < vMin ) {
00145 vMin = value;
00146 min = timers;
00147 }
00148 }
00149
00150
00151 if( size >= 3 && max != null && min != null ) {
00152 max.getParentElement().detach();
00153 min.getParentElement().detach();
00154 }
00155
00156
00157 XMLHelper.writeFile(doc, this.filename);
00158 }
00159
00160
00161
00162
00163
00164
00165
00166
00170 private void fillTimersResults(Element eTimers,
00171 HierarchicalTimerStatistics timer) {
00172
00173 if (timer == null) {
00174 return;
00175 }
00176
00177 Element currentElement = null, rootElement = null, parentElement = null;
00178 Attribute nameAttr = null, minAttr, avgAttr, maxAttr, devAttr;
00179 String[] nameArray = timer.getNameArray();
00180 int i, j, k, nb = timer.getNb();
00181 String rootName = nameArray[0];
00182
00183 for (i = 0; i < nb; i++) {
00184 for (j = 0; j < nb; j++) {
00185 for (k = 0; k < nb; k++) {
00186
00187 if (timer.getMin(i, j, k) != -1) {
00188 if (nameArray[k] != null) {
00189 if (nameArray[k].equals(rootName)) {
00190 currentElement = new Element("timer");
00191 nameAttr = new Attribute("name", rootName);
00192 rootElement = currentElement;
00193 } else {
00194 if (nameArray[j] != null) {
00195 if (nameArray[k].equals(nameArray[j])) {
00196 currentElement = new Element("timer");
00197 nameAttr = new Attribute("name",
00198 nameArray[j]);
00199
00200 if (rootElement == null) {
00201 throw new IllegalStateException(
00202 "-- Timer "
00203 + nameArray[j]
00204 + " has a null root. Please check your start-stop pairs for this timer.");
00205 }
00206 rootElement.addContent(currentElement);
00207 parentElement = currentElement;
00208 } else {
00209 currentElement = new Element("timer");
00210 nameAttr = new Attribute("name",
00211 nameArray[k]);
00212 parentElement
00213 .addContent(currentElement);
00214 }
00215
00216 }
00217 }
00218 if (nameAttr != null) {
00219 currentElement.setAttribute(nameAttr);
00220 }
00221 minAttr = new Attribute("min", timer.getFormMin(i,
00222 j, k));
00223 if (currentElement != null) {
00224 currentElement.setAttribute(minAttr);
00225 }
00226 }
00227 }
00228
00229 if (timer.getAverage(i, j, k) != -1) {
00230 avgAttr = new Attribute("avg", timer.getFormAverage(i,
00231 j, k));
00232 if (currentElement != null) {
00233 currentElement.setAttribute(avgAttr);
00234 }
00235 }
00236
00237 if (timer.getMax(i, j, k) != -1) {
00238 maxAttr = new Attribute("max", timer
00239 .getFormMax(i, j, k));
00240 if (currentElement != null) {
00241 currentElement.setAttribute(maxAttr);
00242 }
00243 }
00244
00245 if (timer.getDeviation(i, j, k) != -1) {
00246 devAttr = new Attribute("dev", timer.getFormDeviation(
00247 i, j, k));
00248 if (currentElement != null) {
00249 currentElement.setAttribute(devAttr);
00250 }
00251 }
00252 }
00253 }
00254 }
00255 if (rootElement != null) {
00256 eTimers.addContent(rootElement);
00257 }
00258
00259 }
00260
00264 private void fillEventsResults(Element eEvents, EventStatistics events) {
00265
00266 if (events == null) {
00267 return;
00268 }
00269
00270 Element currentElement;
00271 Attribute nameAttr;
00272 String eventValue;
00273
00274
00275 for (int i = 0; i < events.getNb(); i++) {
00276 currentElement = new Element("event");
00277 eEvents.addContent(currentElement);
00278 nameAttr = new Attribute("name", events.getName(i));
00279 eventValue = events.getEventValue(i).toString();
00280 currentElement.setAttribute(nameAttr);
00281
00282 try {
00283 currentElement.setText("" + Double.valueOf(eventValue));
00284 } catch (NumberFormatException e) {
00285 currentElement.setText(".\n" + eventValue);
00286 }
00287 }
00288 }
00289 }