org/objectweb/proactive/core/util/timer/CompositeAverageMicroTimer.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.core.util.timer;
00032 
00033 import java.io.IOException;
00034 import java.io.ObjectInputStream;
00035 import java.io.ObjectOutputStream;
00036 import java.io.Serializable;
00037 import java.util.HashMap;
00038 import java.util.Iterator;
00039 import java.util.Random;
00040 
00041 import org.objectweb.proactive.core.util.profiling.PAProfilerEngine;
00042 import org.objectweb.proactive.core.util.profiling.Timer;
00043 
00044 
00051 public class CompositeAverageMicroTimer extends AverageMicroTimer
00052     implements Timer, Serializable {
00053     private HashMap<String, Timer> timerMap = new HashMap<String, Timer>();
00054     private Timer activeTimer = null;
00055 
00056     private CompositeAverageMicroTimer() {
00057     }
00058 
00059     public CompositeAverageMicroTimer(String name) {
00060         this.name = name;
00061     }
00062 
00063     public void start() {
00064         this.activeTimer.start();
00065     }
00066 
00067     public void resume() {
00068         this.activeTimer.resume();
00069     }
00070 
00071     public void pause() {
00072         this.activeTimer.pause();
00073     }
00074 
00075     public void stop() {
00076         if (this.activeTimer != null) {
00077             this.activeTimer.stop();
00078         }
00079     }
00080 
00081     public void reset() {
00082         this.activeTimer.reset();
00083     }
00084 
00085     public long getCumulatedTime() {
00086         long time = 0;
00087         Iterator<Timer> it = timerMap.values().iterator();
00088         while (it.hasNext()) {
00089             Timer t = it.next();
00090             time += t.getCumulatedTime();
00091         }
00092         return time;
00093     }
00094 
00095     public int getNumberOfValues() {
00096         int values = 0;
00097         Iterator<Timer> it = timerMap.values().iterator();
00098         while (it.hasNext()) {
00099             Timer t = it.next();
00100             values += t.getNumberOfValues();
00101         }
00102         return values;
00103     }
00104 
00108     public double getAverage() {
00109         int values = 0;
00110         long time = 0;
00111         Iterator<Timer> it = timerMap.values().iterator();
00112         while (it.hasNext()) {
00113             Timer t = it.next();
00114             values += t.getNumberOfValues();
00115             time += t.getCumulatedTime();
00116         }
00117         return ((values > 0) ? ((double) time / values) : (-1));
00118     }
00119 
00120     public String toString() {
00121         StringBuilder tmp = new StringBuilder();
00122 
00123         tmp.append("Number of measures: ").append(this.getNumberOfValues());
00124         tmp.append("\nTotal time measured: ").append(this.getCumulatedTime());
00125         tmp.append("\nAverage time: ").append(this.getAverage()).append("\n");
00126         //  Now we deal with the internal timers
00127         Iterator<Timer> it = timerMap.values().iterator();
00128         while (it.hasNext()) {
00129             Timer t = it.next();
00130             tmp.append("    ").append(t.getName()).append("\n");
00131             tmp.append("        ").append("Number of measures: ").append(t.getNumberOfValues());
00132             tmp.append("\n        ").append("Total time measured: ").append(t.getCumulatedTime());
00133             tmp.append("\n        ").append("Average time: ")
00134                .append(t.getAverage()).append("\n");
00135         }
00136 
00137         return tmp.toString();
00138     }
00139 
00140     public void dump() {
00141         int ln = name.length();
00142         StringBuilder tmp = new StringBuilder();
00143         tmp.append("------- ").append(name).append(" -------\n");
00144         tmp.append(this.toString());
00145         for (int i = 0; i <= (ln + 16); i++) {
00146             tmp.append("-");
00147         }
00148         System.out.println(tmp.append("\n").toString());
00149     }
00150 
00151     public void setTimer(String name) {
00152         String realName = this.name + "." + name;
00153         this.activeTimer = timerMap.get(realName);
00154         if (this.activeTimer == null) {
00155             //need to create a new timer
00156             this.activeTimer = new AverageMicroTimer(realName);
00157             timerMap.put(realName, this.activeTimer);
00158         }
00159     }
00160 
00161     public Timer getActiveTimer() {
00162         return this.activeTimer;
00163     }
00164 
00165     private void writeObject(ObjectOutputStream out) throws IOException {
00166         this.stop();
00167         this.dump();
00168         out.defaultWriteObject();
00169     }
00170 
00171     private void readObject(ObjectInputStream in)
00172         throws IOException, ClassNotFoundException {
00173         in.defaultReadObject();
00174         System.out.println("CompositeAverageMicroTimer.readObject()");
00175 
00176         PAProfilerEngine.registerTimer(this);
00177     }
00178 
00179     public static void main(String[] args) {
00180         CompositeAverageMicroTimer timer = new CompositeAverageMicroTimer(
00181                 "Test");
00182         PAProfilerEngine.registerTimer(timer);
00183         System.out.println("Using sub-timer 1");
00184         timer.setTimer("1");
00185         Random rand = new Random();
00186         try {
00187             for (int i = 0; i < 2; i++) {
00188                 timer.start();
00189                 Thread.sleep(400 + rand.nextInt(1000));
00190                 timer.stop();
00191             }
00192             System.out.println("Using sub-timer 2");
00193             timer.setTimer("2");
00194             for (int i = 0; i < 2; i++) {
00195                 timer.start();
00196                 Thread.sleep(400 + rand.nextInt(1000));
00197                 timer.stop();
00198             }
00199         } catch (Exception e) {
00200             e.printStackTrace();
00201         }
00202     }
00203 }

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