org/objectweb/proactive/benchmarks/timit/result/BenchmarkResultWriter.java

00001 /* 
00002  * ################################################################
00003  * 
00004  * ProActive: The Java(TM) library for Parallel, Distributed, 
00005  *            Concurrent computing with Security and Mobility
00006  * 
00007  * Copyright (C) 1997-2007 INRIA/University of Nice-Sophia Antipolis
00008  * Contact: proactive@objectweb.org
00009  * 
00010  * This library is free software; you can redistribute it and/or
00011  * modify it under the terms of the GNU Lesser General Public
00012  * License as published by the Free Software Foundation; either
00013  * version 2.1 of the License, or any later version.
00014  *  
00015  * This library is distributed in the hope that it will be useful,
00016  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00017  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00018  * Lesser General Public License for more details.
00019  * 
00020  * You should have received a copy of the GNU Lesser General Public
00021  * License along with this library; if not, write to the Free Software
00022  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
00023  * USA
00024  *  
00025  *  Initial developer(s):               The ProActive Team
00026  *                        http://www.inria.fr/oasis/ProActive/contacts.html
00027  *  Contributor(s): 
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         // Timer statistics
00094         Element eTimers = new Element("timers");
00095         benchResult.addContent(eTimers);
00096         this.fillTimersResults(eTimers, bstats.getTimerStatistics());
00097 
00098         // Event statistics
00099         Element eEvents = new Element("events");
00100         benchResult.addContent(eEvents);
00101         this.fillEventsResults(eEvents, bstats.getEventsStatistics());
00102 
00103         // Add informations from workers
00104         Element informationElement = new Element("Information");
00105         informationElement.addContent("" + bstats.getInformation());
00106         benchResult.addContent(informationElement);
00107     }
00108     
00113     public void writeResult() {
00114         // Save modification into file
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         // Look for the max and min values...
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         // ... end remove them if there is more than 3 runs
00151         if( size >= 3 && max != null && min != null ) {
00152             max.getParentElement().detach();
00153             min.getParentElement().detach();
00154         }
00155         
00156         // then save the result
00157         XMLHelper.writeFile(doc, this.filename);
00158     }
00159     
00160 
00161     
00162     
00163     //
00164     // -- PRIVATE METHODS ----------------------------------------------------
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                                         // If there is a root error
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         // Add events observers
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 }

Generated on Mon Jan 22 15:16:04 2007 for ProActive by  doxygen 1.5.1