org/objectweb/proactive/core/util/timer/AverageMicroTimer.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 
00038 import org.objectweb.proactive.core.util.profiling.Timer;
00039 
00040 
00045 public class AverageMicroTimer implements Timer, Serializable {
00046     protected String name;
00047 
00048     //the number of values in tis timer so far
00049     protected int nbrValues;
00050 
00051     //the total time measured by this timer
00052     protected long total;
00053 
00054     //temporary counter used to measure time when pausing/resuming
00055     protected long currentElapsed;
00056     transient protected MicroTimer timer = new MicroTimer();
00057 
00058     //used to check that start has been pressed prior to a stop
00059     protected boolean running;
00060 
00061     public AverageMicroTimer() {
00062         this(AverageMicroTimer.class.getName());
00063     }
00064 
00065     public AverageMicroTimer(String name) {
00066         this.name = name;
00067     }
00068 
00069     public void start() {
00070         currentElapsed = 0;
00071         running = true;
00072         timer.start();
00073     }
00074 
00075     public void resume() {
00076         timer.start();
00077     }
00078 
00079     public void pause() {
00080         timer.stop();
00081         currentElapsed += timer.getCumulatedTime();
00082     }
00083 
00087     public void stop() {
00088         //System.out.println("AverageMicroTimer.stop()");
00089         if (running) {
00090             timer.stop();
00091             currentElapsed += timer.getCumulatedTime();
00092             this.total += currentElapsed;
00093             //                  if (tmp >= 0) {
00094             this.nbrValues++;
00095             //                  }
00096             currentElapsed = 0;
00097             running = false;
00098         }
00099     }
00100 
00104     public long getCumulatedTime() {
00105         return total;
00106     }
00107 
00108     public int getNumberOfValues() {
00109         return this.nbrValues;
00110     }
00111 
00117     public double getAverage() {
00118         return ((nbrValues > 0) ? ((double) total / nbrValues) : (-1));
00119     }
00120 
00121     public void dump() {
00122         int ln = name.length();
00123         StringBuilder tmp = new StringBuilder();
00124         tmp.append("------- ").append(name).append(" -------\n");
00125         tmp.append(this.toString());
00126         for (int i = 0; i <= (ln + 16); i++) {
00127             tmp.append("-");
00128         }
00129         System.out.println(tmp.append("\n").toString());
00130     }
00131 
00132     public String toString() {
00133         StringBuilder tmp = new StringBuilder();
00134         tmp.append("Number of measures: ").append(this.getNumberOfValues());
00135         tmp.append("\nTotal time measured: ").append(this.getCumulatedTime());
00136         tmp.append("\nAverage time: ").append(this.getAverage()).append("\n");
00137         return tmp.toString();
00138     }
00139 
00140     public String getName() {
00141         return this.name;
00142     }
00143 
00144     public void setName(String name) {
00145         this.name = name;
00146     }
00147 
00148     private void writeObject(ObjectOutputStream out) throws IOException {
00149         this.stop();
00150         out.defaultWriteObject();
00151     }
00152 
00153     private void readObject(ObjectInputStream in)
00154         throws IOException, ClassNotFoundException {
00155         in.defaultReadObject();
00156         this.timer = new MicroTimer();
00157     }
00158 
00159     public void reset() {
00160         this.currentElapsed = 0;
00161         this.nbrValues = 0;
00162         this.total = 0;
00163     }
00164 }

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