org/objectweb/proactive/core/util/timer/EWMATimer.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.util.Random;
00034 
00035 
00041 public class EWMATimer extends AverageMicroTimer {
00042     protected double alpha;
00043     protected double average;
00044 
00045     public EWMATimer(String s, double alpha) {
00046         super(s);
00047         this.alpha = alpha;
00048     }
00049 
00050     public void stop() {
00051         if (running) {
00052             timer.stop();
00053             currentElapsed += timer.getCumulatedTime();
00054             this.total += currentElapsed;
00055             this.nbrValues++;
00056             this.average = ((this.average * this.alpha) +
00057                 ((1 - this.alpha) * currentElapsed));
00058             currentElapsed = 0;
00059             running = false;
00060         }
00061     }
00062 
00063     public double getAverage() {
00064         return this.average;
00065     }
00066 
00067     public void reset() {
00068         super.reset();
00069         this.average = 0;
00070     }
00071 
00072     public static void main(String[] args) {
00073         int max = 100;
00074         Random r = new Random();
00075         long rValue;
00076         double[] ewmaMemory = new double[max];
00077         double[] ewmaMemory2 = new double[max];
00078         double[] tMemory = new double[max];
00079 
00080         //we use multilple  timers to show the difference between a classical average one and a EWMA one
00081         EWMATimer ewmat = new EWMATimer("EWMA", 0.9);
00082         EWMATimer ewmat2 = new EWMATimer("EWMA", 0.5);
00083         TimerWithMemory tm = new TimerWithMemory("Memory");
00084         AverageMicroTimer t = new AverageMicroTimer("MicroTimer");
00085         for (int i = 0; i < 100; i++) {
00086             ewmat.start();
00087             t.start();
00088             ewmat2.start();
00089             tm.start();
00090             try {
00091                 Thread.sleep(r.nextInt(200));
00092             } catch (InterruptedException e) {
00093                 e.printStackTrace();
00094             }
00095             ewmat.stop();
00096             t.stop();
00097             ewmat2.stop();
00098             tm.stop();
00099             ewmaMemory[i] = ewmat.getAverage();
00100             ewmaMemory2[i] = ewmat2.getAverage();
00101             tMemory[i] = t.getAverage();
00102         }
00103         long[] memory = tm.getMemory();
00104         System.err.println("Dumping memory ");
00105         for (int i = 0; i < ewmaMemory.length; i++) {
00106             System.out.println(tMemory[i] + " " + ewmaMemory[i] + " " +
00107                 ewmaMemory2[i] + " " + memory[i]);
00108         }
00109         System.err.println(
00110             "Command in gnuplot: plot 'file' using 1 title 'Average' with lp, 'file' using 2 title 'EMWA 0.9' with lp, 'file' using 3 title 'EWMA 0.5' with lp, 'file' using 4 title 'Raw' with lp");
00111     }
00112 }

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