KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > taglibs > benchmark > Duration


1 /*
2  * Copyright 1999,2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

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 /**
24  *
25  * A tag handler for the <benchmark:duration> tag, which determines
26  * how long it takes to compute its body (minus the sections of its body
27  * enclosed in <benchmark:exclude> ... </benchmark:exclude>).
28  *
29  * @version 0.90
30  * @author Shawn Bayern
31  */

32
33 public class Duration extends BodyTagSupport {
34
35     private long repeat = 1; // number of repetitions
36
private boolean output = false; // do we output our contents?
37

38     private long start, end; // our own start and end times
39
private boolean running; // is our timer currently running?
40
private long exclude; // time to subtract from timer
41
private long exStart; // start of exclusion timer (tmp)
42

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         // first, stop the clock
57
end = System.currentTimeMillis();
58         running = false;
59
60         // next, output the body contents if appropriate
61
if (output)
62         getPreviousOut().write(getBodyContent().getString());
63
64         // finally, output duration
65
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     /** Temporarily pauses our timer, which must already be running. */
81     public void pauseTimer() throws JspTagException {
82     if (!running) {
83         /*
84          * Display an error message customized for the common case,
85          * since this situation is possible but erroneous. I've
86          * somewhat arbitrarily caught the error here instead of in
87          * <exclude> itself on the premise that if other tags are
88          * plugged in, we can localize this handling in the parent class.
89          * This may be stupid, but it's 5:16 in the morning, so what
90          * do I know?
91          */

92         throw new JspTagException(
93         "can't &lt;exclude&gt; an already excluded section "
94         + "or otherwise pause a paused &lt;duration&gt; timer");
95     }
96
97     // start a separate exclusion timer
98
exStart = System.currentTimeMillis();
99     running = false;
100     }
101
102     /** Restarts our timer, which must already be paused. */
103     public void restartTimer() throws JspTagException {
104     if (running) {
105         // shouldn't happen unless new custom tags are added later
106
throw new JspTagException(
107         "unexpected use of Duration.restartTimer() on active clock");
108     }
109
110     // end the current exclusion timer and compute exclusion
111
long exEnd = System.currentTimeMillis();
112     exclude += exEnd - exStart;
113     running = true;
114     }
115
116     /** Initializes the timer. */
117     private void init() {
118     start = end = exclude = exStart = 0;
119     repeat = 1;
120     running = output = false;
121     }
122
123
124     // accessors
125

126     public void setRepeat(long x) throws JspTagException {
127     if (x < 0)
128         throw new JspTagException(
129         "'repeat' in &lt;benchmark:duration&gt; 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