org/objectweb/proactive/core/util/timer/TimerWithMemory.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.Serializable;
00034 
00035 import org.objectweb.proactive.core.util.profiling.Timer;
00036 
00037 
00041 public class TimerWithMemory extends AverageMicroTimer implements Timer,
00042     Serializable {
00043     protected long[] memory;
00044     protected int position;
00045 
00046     public TimerWithMemory() {
00047         this.memory = new long[10];
00048         this.position = 0;
00049     }
00050 
00051     public TimerWithMemory(String name) {
00052         this();
00053         this.name = name;
00054     }
00055 
00056     public void stop() {
00057         if (running) {
00058             timer.stop();
00059             currentElapsed += timer.getCumulatedTime();
00060             this.total += currentElapsed;
00061             this.nbrValues++;
00062             this.addToMemory(currentElapsed);
00063             currentElapsed = 0;
00064             running = false;
00065         }
00066     }
00067 
00072     protected void addToMemoryTest(long time) {
00073         this.total += time;
00074         this.nbrValues++;
00075         this.addToMemory(time);
00076     }
00077 
00078     protected void addToMemory(long time) {
00079         if (this.position == this.memory.length) {
00080             //not enough space, need to increase the array
00081             long[] tmp = new long[2 * this.memory.length];
00082             System.arraycopy(memory, 0, tmp, 0, memory.length);
00083             this.memory = tmp;
00084         }
00085         this.memory[position] = time;
00086         this.position++;
00087     }
00088 
00093     public long[] getMemory() {
00094         if (this.position > 0) {
00095             long[] tmp = new long[this.position];
00096             System.arraycopy(memory, 0, tmp, 0, this.position);
00097             return tmp;
00098         } else {
00099             return null;
00100         }
00101     }
00102 
00103     public double getVariance() {
00104         double average = this.getAverage();
00105         double total2 = 0;
00106         for (int i = 0; i < this.position; i++) {
00107             total2 += Math.pow(memory[i], 2);
00108         }
00109         return ((total2 / this.position) - Math.pow(average, 2));
00110     }
00111 
00112     public double getStandardDeviation() {
00113         return Math.sqrt(this.getVariance());
00114     }
00115 
00116     public void reset() {
00117         super.reset();
00118         this.memory = new long[10];
00119         this.position = 0;
00120     }
00121 
00122     public void dump() {
00123         System.out.println("Dumping memory");
00124         for (int i = 0; i < this.position; i++) {
00125             System.out.print(memory[i] + " ");
00126         }
00127         System.out.println();
00128     }
00129 
00130     public static void main(String[] args) {
00131         TimerWithMemory mt = new TimerWithMemory("Test");
00132         for (int i = 0; i < 10; i++) {
00133             mt.addToMemoryTest(i);
00134         }
00135         mt.dump();
00136         System.out.println("Requesting memory and displaying it");
00137         long[] m = mt.getMemory();
00138         for (int i = 0; i < m.length; i++) {
00139             System.out.print(m[i] + " ");
00140         }
00141         System.out.println();
00142         System.out.println("Average is " + mt.getAverage());
00143         System.out.println("Variance is " + mt.getVariance());
00144 
00145         System.out.println("Standard deviation is " +
00146             mt.getStandardDeviation());
00147     }
00148 }

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