View Javadoc

1   package org.apache.directmemory.measures;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import static java.lang.String.format;
23  
24  import java.text.DecimalFormat;
25  import java.util.HashMap;
26  import java.util.Map;
27  import java.util.concurrent.atomic.AtomicLong;
28  
29  import org.slf4j.Logger;
30  import org.slf4j.LoggerFactory;
31  
32  public class MonitorServiceImpl
33      implements MonitorService
34  {
35  
36      private final AtomicLong hits = new AtomicLong( 0 );
37  
38      private long totalTime = 0;
39  
40      private long min = -1;
41  
42      private long max = -1;
43  
44      public final String name;
45  
46      private static final Logger LOG = LoggerFactory.getLogger( MonitorServiceImpl.class );
47  
48      //TODO: MONITORS looks like a good candidate to become a private field
49      public static final Map<String, MonitorServiceImpl> MONITORS = new HashMap<String, MonitorServiceImpl>();
50  
51      public MonitorServiceImpl( String name )
52      {
53          this.name = name;
54      }
55  
56      public long start()
57      {
58          return System.nanoTime();
59      }
60  
61      public long stop( long begunAt )
62      {
63          hits.incrementAndGet();
64          final long lastAccessed = System.nanoTime();
65          final long elapsed = lastAccessed - begunAt;
66          totalTime += elapsed;
67          if ( elapsed > max && hits.get() > 0 )
68          {
69              max = elapsed;
70          }
71          if ( elapsed < min && hits.get() > 0 )
72          {
73              min = elapsed;
74          }
75          return elapsed;
76      }
77  
78      public long hits()
79      {
80          return hits.get();
81      }
82  
83      public long totalTime()
84      {
85          return totalTime;
86      }
87  
88      public long average()
89      {
90          return hits.get() > 0 ? totalTime / hits.get() : 0;
91      }
92  
93      public String toString()
94      {
95          return format( "%1$s hits: %2$d, avg: %3$s ms, tot: %4$s seconds", name, hits.get(),
96                         new DecimalFormat( "####.###" ).format( (double) average() / 1000000 ),
97                         new DecimalFormat( "####.###" ).format( (double) totalTime / 1000000000 ) );
98      }
99  
100     public void dump( String prefix )
101     {
102         for ( MonitorServiceImpl monitor : MonitorServiceImpl.MONITORS.values() )
103         {
104             if ( monitor.name.startsWith( prefix ) )
105             {
106                 LOG.info( monitor.toString() );
107             }
108         }
109     }
110 
111     public void dump()
112     {
113         dump( "" );
114     }
115 
116     public String getName()
117     {
118         return name;
119     }
120 
121     public AtomicLong getHits()
122     {
123         return hits;
124     }
125 
126     public long getTotalTime()
127     {
128         return totalTime;
129     }
130 
131     @Override
132     public void addToTotalTime( long time )
133     {
134         totalTime += time;
135     }
136 
137     public long getMin()
138     {
139         return min;
140     }
141 
142     public void setMin( long min )
143     {
144         this.min = min;
145     }
146 
147     public long getMax()
148     {
149         return max;
150     }
151 
152     public void setMax( long max )
153     {
154         this.max = max;
155     }
156 
157 
158 }