View Javadoc

1   //
2   //  ========================================================================
3   //  Copyright (c) 1995-2013 Mort Bay Consulting Pty. Ltd.
4   //  ------------------------------------------------------------------------
5   //  All rights reserved. This program and the accompanying materials
6   //  are made available under the terms of the Eclipse Public License v1.0
7   //  and Apache License v2.0 which accompanies this distribution.
8   //
9   //      The Eclipse Public License is available at
10  //      http://www.eclipse.org/legal/epl-v10.html
11  //
12  //      The Apache License v2.0 is available at
13  //      http://www.opensource.org/licenses/apache2.0.php
14  //
15  //  You may elect to redistribute this code under either of these licenses.
16  //  ========================================================================
17  //
18  
19  package org.eclipse.jetty.server;
20  
21  import java.io.IOException;
22  import java.util.Arrays;
23  import java.util.concurrent.atomic.AtomicLong;
24  
25  import org.eclipse.jetty.io.Connection;
26  import org.eclipse.jetty.util.annotation.ManagedObject;
27  import org.eclipse.jetty.util.annotation.ManagedOperation;
28  import org.eclipse.jetty.util.component.AbstractLifeCycle;
29  import org.eclipse.jetty.util.component.ContainerLifeCycle;
30  import org.eclipse.jetty.util.component.Dumpable;
31  import org.eclipse.jetty.util.statistic.CounterStatistic;
32  import org.eclipse.jetty.util.statistic.SampleStatistic;
33  
34  @ManagedObject("Connector Statistics")
35  public class ConnectorStatistics extends AbstractLifeCycle implements Dumpable, Connection.Listener
36  {
37      private final AtomicLong _startMillis = new AtomicLong(-1L);
38      private final CounterStatistic _connectionStats = new CounterStatistic();
39      private final SampleStatistic _messagesIn = new SampleStatistic();
40      private final SampleStatistic _messagesOut = new SampleStatistic();
41      private final SampleStatistic _connectionDurationStats = new SampleStatistic();
42  
43      @Override
44      public void onOpened(Connection connection)
45      {
46          connectionOpened();
47      }
48  
49      @Override
50      public void onClosed(Connection connection)
51      {
52          connectionClosed(System.currentTimeMillis()-connection.getCreatedTimeStamp(),connection.getMessagesIn(),connection.getMessagesOut());
53      }
54  
55      public int getBytesIn()
56      {
57          // TODO
58          return -1;
59      }
60  
61      public int getBytesOut()
62      {
63          // TODO
64          return -1;
65      }
66  
67      public int getConnections()
68      {
69          return (int)_connectionStats.getTotal();
70      }
71  
72      public long getConnectionsDurationMax()
73      {
74          return _connectionDurationStats.getMax();
75      }
76  
77      public double getConnectionsDurationMean()
78      {
79          return _connectionDurationStats.getMean();
80      }
81  
82      public double getConnectionsDurationStdDev()
83      {
84          return _connectionDurationStats.getStdDev();
85      }
86  
87      public long getConnectionsDurationTotal()
88      {
89          return _connectionDurationStats.getTotal();
90      }
91  
92      public int getConnectionsMessagesInMax()
93      {
94          return (int)_messagesIn.getMax();
95      }
96  
97      public double getConnectionsMessagesInMean()
98      {
99          return _messagesIn.getMean();
100     }
101 
102     public double getConnectionsMessagesInStdDev()
103     {
104         return _messagesIn.getStdDev();
105     }
106 
107     public int getConnectionsOpen()
108     {
109         return (int)_connectionStats.getCurrent();
110     }
111 
112     public int getConnectionsOpenMax()
113     {
114         return (int)_connectionStats.getMax();
115     }
116 
117     public int getMessagesIn()
118     {
119         return (int)_messagesIn.getTotal();
120     }
121 
122     public int getMessagesOut()
123     {
124         return (int)_messagesIn.getTotal();
125     }
126 
127     public long getStartedMillis()
128     {
129         long start = _startMillis.get();
130         return start < 0 ? 0 : System.currentTimeMillis() - start;
131     }
132 
133     @Override
134     public void doStart()
135     {
136         reset();
137     }
138 
139     @Override
140     public void doStop()
141     {
142     }
143 
144     public void reset()
145     {
146         _startMillis.set(System.currentTimeMillis());
147         _messagesIn.reset();
148         _messagesOut.reset();
149         _connectionStats.reset();
150         _connectionDurationStats.reset();
151     }
152 
153     public void connectionOpened()
154     {
155         if (isStarted())
156         {
157             _connectionStats.increment();
158         }
159     }
160 
161     public void connectionUpgraded(int messagesIn, int messagesOut)
162     {
163         if (isStarted())
164         {
165             _messagesIn.set(messagesIn);
166             _messagesOut.set(messagesOut);
167         }
168     }
169 
170     public void connectionClosed(long duration, int messagesIn, int messagesOut)
171     {
172         if (isStarted())
173         {
174             _messagesIn.set(messagesIn);
175             _messagesOut.set(messagesOut);
176             _connectionStats.decrement();
177             _connectionDurationStats.set(duration);
178         }
179     }
180 
181 
182     @Override
183     @ManagedOperation("dump thread state")
184     public String dump()
185     {
186         return ContainerLifeCycle.dump(this);
187     }
188 
189     @Override
190     public void dump(Appendable out, String indent) throws IOException
191     {
192         ContainerLifeCycle.dumpObject(out,this);
193         ContainerLifeCycle.dump(out,indent,Arrays.asList(new String[]{"connections="+_connectionStats,"duration="+_connectionDurationStats,"in="+_messagesIn,"out="+_messagesOut}));
194     }
195 }