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.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
00049 protected int nbrValues;
00050
00051
00052 protected long total;
00053
00054
00055 protected long currentElapsed;
00056 transient protected MicroTimer timer = new MicroTimer();
00057
00058
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
00089 if (running) {
00090 timer.stop();
00091 currentElapsed += timer.getCumulatedTime();
00092 this.total += currentElapsed;
00093
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 }