View Javadoc

1   /* ==========================================
2    * JperfProbe : Java Performance Probes
3    * ==========================================
4    *
5    * Project Info:  http://jperfprobe.sourceforge.net/
6    * Project Lead:  Tor-Erik Larsen (http://sourceforge.net/users/uptime62)
7    *
8    * (C) Copyright 2005, by Tor-Erik Larsen and Contributors.
9    *
10   * This library is free software; you can redistribute it and/or modify it
11   * under the terms of the GNU Lesser General Public License as published by
12   * the Free Software Foundation; either version 2.1 of the License, or
13   * (at your option) any later version.
14   *
15   * This library is distributed in the hope that it will be useful, but
16   * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
17   * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
18   * License for more details.
19   *
20   * You should have received a copy of the GNU Lesser General Public License
21   * along with this library; if not, write to the Free Software Foundation, Inc.,
22   * 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
23   */
24  
25  package net.sf.jperfprobe;
26  
27  import org.slf4j.*;
28  
29  
30  /**
31   * DefaultProbe implements a Probe. It is possible to start and stop a probe
32   * The probe is identified by a name. The probe uses nano seconds for measuring time.
33   */
34  public class DefaultProbe implements Probe {
35      final Logger log = LoggerFactory.getLogger(DefaultProbe.class);
36      
37      // indicates if the probe is running.
38      private boolean running;
39  
40      // start time stamp of this probe.
41      private long startTime;
42  
43      // elapsed time in probe.
44      private long timeElapsed;
45  
46      // name of probe.
47      private final String name;
48  
49      // probe enable flag.
50      private boolean enabled = true;
51  
52      // name of the initiating thread
53      //private String threadName;
54  
55      // how to get the time
56      private Time time = TimeFactory.getTime();
57  
58      /**
59       * Constructor of probe
60       *
61       * @param name name of probe
62       */
63      public DefaultProbe(String name) {
64          this.name = name;
65          //this.threadName = Thread.currentThread().getName();
66      }
67  
68      protected DefaultProbe(String name, Time tim) {
69          this(name);
70          this.time = tim;
71      }
72  
73      /**
74       * Get elapsed time for this probe in nanos.
75       *
76       * @return elapsed time
77       */
78      public long getElapsed() {
79          return timeElapsed;
80      }
81  
82      /**
83       * Start the probe.
84       * The probe will only start if it is enabled and not running.
85       * Nothing will happen if it is running.
86       */
87      public void start() {
88          if (enabled && !running) {
89              startTime = time.getNanos();
90              running = true;
91          }
92      }
93  
94      /**
95       * Stop the probe. It is not poosible to stop the probe if it is disabled
96       */
97      public void stop() {
98          if (enabled) {
99              long ti = this.time.getNanos() - startTime;
100             if (running && ti > 0) {
101                 // make sure timeelapsed > 0
102                 timeElapsed = ti;
103                 running = false;
104             }
105         }
106     }
107 
108     /**
109      * Is the probe enabled.
110      *
111      * @return enabled flag
112      */
113     public boolean isEnabled() {
114         return enabled;
115     }
116 
117     /**
118      * Enable the probe.
119      */
120     public void enable() {
121         enabled = true;
122     }
123 
124     /**
125      * Disable the probe.
126      */
127     public void disable() {
128         enabled = false;
129         running = false;
130     }
131 
132     /**
133      * Is the probe running (by start).
134      *
135      * @return running flag
136      */
137     public boolean isRunning() {
138         return running;
139     }
140 
141     public String getName() {
142         return name;
143     }
144 
145     public Time getTime() {
146         return time;
147     }
148 
149     public void setTime(Time time) {
150         this.time = time;
151     }
152 
153     /**
154      * Get all info from probe to string
155      *
156      * @return string with all probe info
157      */
158     @Override
159     public String toString() {
160         return "probe name=" + name + ", elapsed:" + timeElapsed + " , running:" + running;
161     }
162 }