00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
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
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 }