1 10 package org.mmbase.bridge.jsp.taglib.debug; 11 12 import java.io.IOException ; 13 import javax.servlet.jsp.JspTagException ; 14 15 import java.util.*; 16 17 import org.mmbase.bridge.jsp.taglib.ContextReferrerTag; 18 import org.mmbase.bridge.jsp.taglib.util.Attribute; 19 20 import org.mmbase.util.logging.Logger; 21 import org.mmbase.util.logging.Logging; 22 23 24 class LongContainer { 25 long value; 26 LongContainer() { 27 value = 0; 28 } 29 } 30 31 39 40 public class TimerTag extends ContextReferrerTag { 41 42 private static final Logger log = Logging.getLoggerInstance(TimerTag.class); 43 44 private List timers; 45 private List timerIds; 46 private Map totalTimes; 47 48 private int lastTimer; 49 private Attribute name = Attribute.NULL; 50 51 public void setName(String n) throws JspTagException { 52 name = getAttribute(n); 53 } 54 55 62 63 64 public int startTimer(String id, String id2) throws JspTagException { 65 if (id == null) { 66 return startTimer(id2); 67 } else { 68 return startTimer(id + ":" + id2); 69 } 70 } 71 72 75 76 public int startTimer(String id) throws JspTagException { 77 if (log.isDebugEnabled()) { 78 log.debug("Starting timer " + name.getString(this) + ": " + id); 79 } 80 timers.add(new Long (System.currentTimeMillis())); 81 if (totalTimes.get(id) == null) { 82 totalTimes.put(id, new LongContainer()); 83 } 84 timerIds.add(id); 85 return timers.size() - 1; 86 } 87 88 91 92 public long haltTimer(int handle) throws JspTagException { 93 long duration = System.currentTimeMillis() - ((Long )timers.get(handle)).longValue(); 94 String id = (String )timerIds.get(handle); 95 if (log.isDebugEnabled()) { 96 log.debug("Timer " + (name != Attribute.NULL ? name.getString(this) + ":" : "") + id + ": " + (double)duration / 1000 + " s"); 97 } 98 ((LongContainer)totalTimes.get(id)).value += duration; 99 return duration; 100 } 101 102 105 public int doStartTag() throws JspTagException { 106 log.info("Starting timer " + name.getString(this)); 107 timers = new ArrayList(1); 108 timerIds = new ArrayList(1); 109 totalTimes = new HashMap(); 110 lastTimer = 0; 111 startTimer(getId(), getClass().getName()); 112 return EVAL_BODY_BUFFERED; 113 } 114 115 118 119 public int doAfterBody() throws JspTagException { 120 haltTimer(0); 121 String result = "Timer " + name.getString(this) + " totals:\n"; 122 Iterator i = totalTimes.keySet().iterator(); 123 124 while (i.hasNext()) { 125 String key = (String )i.next(); 126 result += " " + key + ": " + (double)(((LongContainer) totalTimes.get(key)).value) + " ms\n"; 127 } 128 log.info(result); 129 130 try { 131 if (bodyContent != null) { 132 bodyContent.writeOut(bodyContent.getEnclosingWriter()); 133 } 134 return SKIP_BODY; 135 } catch (IOException ioe){ 136 throw new JspTagException (ioe.toString()); 137 } 138 } 139 public int doEndTag() throws JspTagException { 140 timers = null; 141 timerIds = null; 142 totalTimes = null; 143 return super.doEndTag(); 144 } 145 146 } 147 148 | Popular Tags |