1 16 17 package org.springframework.util; 18 19 import java.util.Date ; 20 21 28 public class ResponseTimeMonitorImpl implements ResponseTimeMonitor { 29 30 31 private final long initedMillis = System.currentTimeMillis(); 32 33 34 private volatile int accessCount; 35 36 37 private volatile int totalResponseTimeMillis = 0; 38 39 40 private volatile int bestResponseTimeMillis = Integer.MAX_VALUE; 41 42 43 private volatile int worstResponseTimeMillis = Integer.MIN_VALUE; 44 45 46 49 public Date getLoadDate() { 50 return new Date (this.initedMillis); 51 } 52 53 56 public int getAccessCount() { 57 return accessCount; 58 } 59 60 63 public long getUptimeMillis() { 64 return System.currentTimeMillis() - this.initedMillis; 65 } 66 67 70 public int getAverageResponseTimeMillis() { 71 if (getAccessCount() == 0) { 73 return 0; 74 } 75 return this.totalResponseTimeMillis / getAccessCount(); 76 } 77 78 81 public int getBestResponseTimeMillis() { 82 return bestResponseTimeMillis; 83 } 84 85 88 public int getWorstResponseTimeMillis() { 89 return worstResponseTimeMillis; 90 } 91 92 93 98 public synchronized void recordResponseTime(long responseTimeMillis) { 99 ++this.accessCount; 100 int iResponseTime = (int) responseTimeMillis; 101 this.totalResponseTimeMillis += iResponseTime; 102 if (iResponseTime < this.bestResponseTimeMillis) { 103 this.bestResponseTimeMillis = iResponseTime; 104 } 105 if (iResponseTime > this.worstResponseTimeMillis) { 106 this.worstResponseTimeMillis = iResponseTime; 107 } 108 } 109 110 114 public synchronized String toString() { 115 StringBuffer sb = new StringBuffer (); 116 sb.append("hits=[").append(getAccessCount()).append("]; "); 117 sb.append("average=[").append(getAverageResponseTimeMillis()).append("ms]; "); 118 sb.append("best=[").append(getBestResponseTimeMillis()).append("ms]; "); 119 sb.append("worst=[").append(getWorstResponseTimeMillis()).append("ms]"); 120 return sb.toString(); 121 } 122 123 } 124 | Popular Tags |