1 16 package org.apache.taglibs.benchmark; 17 18 import java.io.*; 19 import javax.servlet.*; 20 import javax.servlet.jsp.*; 21 import javax.servlet.jsp.tagext.*; 22 23 32 33 public class Duration extends BodyTagSupport { 34 35 private long repeat = 1; private boolean output = false; 38 private long start, end; private boolean running; private long exclude; private long exStart; 43 public void release() { 44 init(); 45 } 46 47 public int doStartTag() throws JspException { 48 start = System.currentTimeMillis(); 49 running = true; 50 51 return EVAL_BODY_TAG; 52 } 53 54 public int doEndTag() throws JspException { 55 try { 56 end = System.currentTimeMillis(); 58 running = false; 59 60 if (output) 62 getPreviousOut().write(getBodyContent().getString()); 63 64 pageContext.getOut().print(end - start - exclude); 66 67 } catch (IOException ex) { 68 throw new JspTagException(ex.getMessage()); 69 } 70 return EVAL_PAGE; 71 } 72 73 public int doAfterBody() throws JspException { 74 if (--repeat > 0) 75 return EVAL_BODY_TAG; 76 else 77 return SKIP_BODY; 78 } 79 80 81 public void pauseTimer() throws JspTagException { 82 if (!running) { 83 92 throw new JspTagException( 93 "can't <exclude> an already excluded section " 94 + "or otherwise pause a paused <duration> timer"); 95 } 96 97 exStart = System.currentTimeMillis(); 99 running = false; 100 } 101 102 103 public void restartTimer() throws JspTagException { 104 if (running) { 105 throw new JspTagException( 107 "unexpected use of Duration.restartTimer() on active clock"); 108 } 109 110 long exEnd = System.currentTimeMillis(); 112 exclude += exEnd - exStart; 113 running = true; 114 } 115 116 117 private void init() { 118 start = end = exclude = exStart = 0; 119 repeat = 1; 120 running = output = false; 121 } 122 123 124 126 public void setRepeat(long x) throws JspTagException { 127 if (x < 0) 128 throw new JspTagException( 129 "'repeat' in <benchmark:duration> cannot be negative"); 130 repeat = x; 131 } 132 133 public long getRepeat() { 134 return repeat; 135 } 136 137 public void setOutput(boolean x) { 138 output = x; 139 } 140 141 public boolean getOutput() { 142 return output; 143 } 144 } 145 | Popular Tags |