View Javadoc

1   /*
2    * Jour - java profiler and monitoring library
3    *
4    * Copyright (C) 2004 Jour team
5    *
6    * This library is free software; you can redistribute it and/or
7    * modify it under the terms of the GNU Library General Public
8    * License as published by the Free Software Foundation; either
9    * version 2 of the License, or (at your option) any later version.
10   *
11   * This library is distributed in the hope that it will be useful,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14   * Library General Public License for more details.
15   *
16   * You should have received a copy of the GNU Library General Public
17   * License along with this library; if not, write to the
18   * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19   * Boston, MA  02111-1307, USA.
20   */
21  package net.sf.jour.statistic;
22  
23  import net.sf.jour.rt.agent.ProfilerEvent;
24  
25  import org.apache.log4j.Logger;
26  
27  /***
28   * TODO Add docs
29   *
30   * Created on 13.12.2004
31   * Contributing Author(s):
32   *
33   *   Misha Lifschitz <mishalifschitz at users.sourceforge.net> (Inital implementation)
34   *   Vlad Skarzhevskyy <vlads at users.sourceforge.net> (Inital implementation)
35   *
36   * @author vlads
37   * @version $Revision: 1.4 $ ($Author: vlads $)  $Date: 2004/12/16 17:40:19 $
38   */
39  public class MethodAccumulation extends TimeBaseAccumulationList {
40  
41      protected static final Logger log = Logger.getLogger(MethodAccumulation.class);
42      
43      private int firstExecNumber = 1;
44  
45      private int countFirst = 0;
46  
47      private double firstExec;
48  
49      private long exceptionCount = 0;
50      
51      public static final String ATTRIBUTE_NAME = "MethodAccumulation.NAME";
52      
53      /***
54       * 
55       */
56      public MethodAccumulation(String name) {
57          super();
58          setName(name);
59      }
60  
61      public void reset() {
62          super.reset();
63          this.firstExec = 0;
64          this.countFirst = 0;
65          this.exceptionCount = 0;
66      }
67      
68      public void setName(String name) {
69          setAttribute(ATTRIBUTE_NAME, name);
70      }
71      
72      public void add(ProfilerEvent startMethodEvent, ProfilerEvent endMethodEvent) {
73          double time = endMethodEvent.getTimestamp() - startMethodEvent.getTimestamp();
74          
75          if (endMethodEvent.getThrowable() != null) {
76              this.exceptionCount ++;
77          }
78          
79          if (this.countFirst <= firstExecNumber) {
80              this.countFirst ++;
81              this.firstExec += time;
82          } else {
83              super.add(endMethodEvent.getTimestamp(), new Double(time));            
84          }
85      }
86      
87      /* (non-Javadoc)
88       * @see net.sf.jour.statistic.Accumulation#getCount()
89       */
90      public long getCount() {
91          return super.getCount() + this.countFirst;
92      }
93      
94      public double getFirstRunElapsedTime() {
95          if (this.firstExecNumber == 0) {
96              return this.firstExec;
97          }
98          return this.firstExec / this.firstExecNumber;
99      }
100     
101     public String getName() {
102         return (String)getAttribute(ATTRIBUTE_NAME);
103     }
104     /***
105      * @return Returns the firstExec.
106      */
107     public double getFirstExec() {
108         return firstExec;
109     }
110     /***
111      * @param firstExec The firstExec to set.
112      */
113     public void setFirstExec(double firstExec) {
114         this.firstExec = firstExec;
115     }
116     /***
117      * @return Returns the exceptionCount.
118      */
119     public long getExceptionCount() {
120         return exceptionCount;
121     }
122 }