KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > protomatter > util > OpTimer


1 package com.protomatter.util;
2
3 /**
4  * {{{ The Protomatter Software License, Version 1.0
5  * derived from The Apache Software License, Version 1.1
6  *
7  * Copyright (c) 1998-2002 Nate Sammons. All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  *
13  * 1. Redistributions of source code must retain the above copyright
14  * notice, this list of conditions and the following disclaimer.
15  *
16  * 2. Redistributions in binary form must reproduce the above copyright
17  * notice, this list of conditions and the following disclaimer in
18  * the documentation and/or other materials provided with the
19  * distribution.
20  *
21  * 3. The end-user documentation included with the redistribution,
22  * if any, must include the following acknowledgment:
23  * "This product includes software developed for the
24  * Protomatter Software Project
25  * (http://protomatter.sourceforge.net/)."
26  * Alternately, this acknowledgment may appear in the software itself,
27  * if and wherever such third-party acknowledgments normally appear.
28  *
29  * 4. The names "Protomatter" and "Protomatter Software Project" must
30  * not be used to endorse or promote products derived from this
31  * software without prior written permission. For written
32  * permission, please contact support@protomatter.com.
33  *
34  * 5. Products derived from this software may not be called "Protomatter",
35  * nor may "Protomatter" appear in their name, without prior written
36  * permission of the Protomatter Software Project
37  * (support@protomatter.com).
38  *
39  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
40  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
41  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
42  * DISCLAIMED. IN NO EVENT SHALL THE PROTOMATTER SOFTWARE PROJECT OR
43  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
45  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
46  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
47  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
48  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
49  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
50  * SUCH DAMAGE. }}}
51  */

52
53 /**
54  * A benchmarking utility class. Basically encapsulates a name,
55  * start time and end time.<P>
56  *
57  * Usually used like this:<P>
58  *
59  * <PRE>
60  * OpTimer timer = new OpTimer("Some long operation");
61  * ...
62  * // a lot of junk
63  * ...
64  * timer.stop();
65  * Syslog.debug(this, timer);
66  * </PRE><P>
67  *
68  * This class can be used in conjunction with the
69  * {@link OpTimerUtil OpTimerUtil} class, which can
70  * parse log files for timer traces and produce
71  * aggregate timing information.
72  *
73  * @see OpTimerUtil
74  */

75 public class OpTimer
76 implements java.io.Serializable JavaDoc
77 {
78   private String JavaDoc name = null;
79   private long start = 0;
80   private long stop = 0;
81   private boolean showThread = true;
82   private String JavaDoc threadName = null;
83
84   /**
85    * Create a new timer with the given name. The timer
86    * is started when it is created.
87    */

88   public OpTimer(String JavaDoc name)
89   {
90     this(name, true);
91   }
92
93   /**
94    * Create a new timer with the given name. The timer
95    * is started when it is created.
96    */

97   public OpTimer(String JavaDoc name, boolean showThread)
98   {
99     this.name = name;
100     this.showThread = showThread;
101     start();
102   }
103
104   /**
105    * Re-start the timer. There's no need to call
106    * this method if you're not re-using timer instances.
107    * Returns this timer for convenience.
108    */

109   public OpTimer start()
110   {
111     this.start = System.currentTimeMillis();
112     this.stop = 0;
113     if (showThread)
114       this.threadName = Thread.currentThread().getName();
115     return this;
116   }
117
118   /**
119    * Return elapsed time.
120    */

121   public long elapsed()
122   {
123     return (this.stop != 0)
124          ? (this.stop - this.start)
125          : (System.currentTimeMillis() - this.start);
126   }
127
128   /**
129    * Get the name of this timer.
130    */

131   public String JavaDoc getName()
132   {
133     return this.name; }
134
135   /**
136    * Set the name of this timer.
137    */

138   public void setName(String JavaDoc name)
139   {
140     this.name = name;
141   }
142
143   /**
144    * Get the start time of this timer.
145    */

146   public long startTime()
147   {
148     return this.start;
149   }
150
151   /**
152    * Get the time this timer was stopped. If
153    * the timer is still running, the stop time
154    * will be <TT>0</TT>.
155    */

156   public long stopTime()
157   {
158     return this.stop;
159   }
160
161   /**
162    * Stop the timer. Returns this timer instance for convenience.
163    */

164   public OpTimer stop()
165   {
166     this.stop = System.currentTimeMillis();
167     return this;
168   }
169
170   public String JavaDoc toString()
171   {
172     long now = System.currentTimeMillis();
173     return "OpTimer[" + this.name + ", " +
174       (showThread ? ("thread=" + threadName + ", ") : "") +
175       ((stop == 0) ? "still running, " : "") +
176       "took " +
177       ((this.stop != 0)
178       ? (this.stop - this.start)
179       : (now - this.start)) +
180       "ms]";
181   }
182 }
183
Popular Tags