View Javadoc

    1   // ========================================================================
    2   // Copyright 2009-2009 Mort Bay Consulting Pty. Ltd.
    3   // ------------------------------------------------------------------------
    4   // Licensed under the Apache License, Version 2.0 (the "License");
    5   // you may not use this file except in compliance with the License.
    6   // You may obtain a copy of the License at
    7   // http://www.apache.org/licenses/LICENSE-2.0
    8   // Unless required by applicable law or agreed to in writing, software
    9   // distributed under the License is distributed on an "AS IS" BASIS,
    10  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    11  // See the License for the specific language governing permissions and
    12  // limitations under the License.
    13  // ========================================================================
    14
    15  package org.mortbay.jetty.handler;
    16
    17  import java.io.IOException;
    18  import javax.servlet.ServletException;
    19  import javax.servlet.http.HttpServletRequest;
    20  import javax.servlet.http.HttpServletResponse;
    21
    22  import org.mortbay.jetty.HttpConnection;
    23  import org.mortbay.jetty.Response;
    24
    25  public class StatisticsHandler extends AbstractStatisticsHandler
    26  {
    27      private transient long _statsStartedAt;
    28      private transient int _requests;
    29      private transient long _minRequestTime;
    30      private transient long _maxRequestTime;
    31      private transient long _totalRequestTime;
    32      private transient int _requestsActive;
    33      private transient int _requestsActiveMax;
    34      private transient int _responses1xx; // Informal
    35      private transient int _responses2xx; // Success
    36      private transient int _responses3xx; // Redirection
    37      private transient int _responses4xx; // Client Error
    38      private transient int _responses5xx; // Server Error
    39
    40      public void handle(String target, HttpServletRequest request, HttpServletResponse response, int dispatch) throws IOException, ServletException
    41      {
    42          synchronized (this)
    43          {
    44              ++_requests;
    45              ++_requestsActive;
    46              if (_requestsActive > _requestsActiveMax)
    47                  _requestsActiveMax = _requestsActive;
    48          }
    49
    50          long requestStartTime = System.currentTimeMillis();
    51          try
    52          {
    53              super.handle(target, request, response, dispatch);
    54          }
    55          finally
    56          {
    57              long requestTime = System.currentTimeMillis() - requestStartTime;
    58
    59              synchronized (this)
    60              {
    61                  _requestsActive--;
    62                  if (_requestsActive < 0) _requestsActive = 0;
    63
    64                  _totalRequestTime += requestTime;
    65                  if (requestTime < _minRequestTime || _minRequestTime == 0)
    66                      _minRequestTime = requestTime;
    67                  if (requestTime > _maxRequestTime)
    68                      _maxRequestTime = requestTime;
    69
    70                  Response jettyResponse = (response instanceof Response) ? (Response) response : HttpConnection.getCurrentConnection().getResponse();
    71                  switch (jettyResponse.getStatus() / 100)
    72                  {
    73                      case 1:
    74                          _responses1xx++;
    75                          break;
    76                      case 2:
    77                          _responses2xx++;
    78                          break;
    79                      case 3:
    80                          _responses3xx++;
    81                          break;
    82                      case 4:
    83                          _responses4xx++;
    84                          break;
    85                      case 5:
    86                          _responses5xx++;
    87                          break;
    88                      default:
    89                          break;
    90                  }
    91              }
    92          }
    93      }
    94
    95      public void statsReset()
    96      {
    97          synchronized (this)
    98          {
    99              _statsStartedAt = System.currentTimeMillis();
    100             _requests = 0;
    101             _minRequestTime = 0;
    102             _maxRequestTime = 0;
    103             _totalRequestTime = 0;
    104             _requestsActiveMax = _requestsActive;
    105             _requestsActive = 0;
    106             _responses1xx = 0;
    107             _responses2xx = 0;
    108             _responses3xx = 0;
    109             _responses4xx = 0;
    110             _responses5xx = 0;
    111         }
    112     }
    113
    114     public int getRequests()
    115     {
    116         synchronized (this)
    117         {
    118             return _requests;
    119         }
    120     }
    121
    122     public int getRequestsActive()
    123     {
    124         synchronized (this)
    125         {
    126             return _requestsActive;
    127         }
    128     }
    129
    130     public int getRequestsActiveMax()
    131     {
    132         synchronized (this)
    133         {
    134             return _requestsActiveMax;
    135         }
    136     }
    137
    138     public int getResponses1xx()
    139     {
    140         synchronized (this)
    141         {
    142             return _responses1xx;
    143         }
    144     }
    145
    146     public int getResponses2xx()
    147     {
    148         synchronized (this)
    149         {
    150             return _responses2xx;
    151         }
    152     }
    153
    154     public int getResponses3xx()
    155     {
    156         synchronized (this)
    157         {
    158             return _responses3xx;
    159         }
    160     }
    161
    162     public int getResponses4xx()
    163     {
    164         synchronized (this)
    165         {
    166             return _responses4xx;
    167         }
    168     }
    169
    170     public int getResponses5xx()
    171     {
    172         synchronized (this)
    173         {
    174             return _responses5xx;
    175         }
    176     }
    177
    178     public long getStatsOnMs()
    179     {
    180         synchronized (this)
    181         {
    182             return System.currentTimeMillis() - _statsStartedAt;
    183         }
    184     }
    185
    186     public long getRequestTimeMin()
    187     {
    188         synchronized (this)
    189         {
    190             return _minRequestTime;
    191         }
    192     }
    193
    194     public long getRequestTimeMax()
    195     {
    196         synchronized (this)
    197         {
    198             return _maxRequestTime;
    199         }
    200     }
    201
    202     public long getRequestTimeTotal()
    203     {
    204         synchronized (this)
    205         {
    206             return _totalRequestTime;
    207         }
    208     }
    209
    210     public long getRequestTimeAverage()
    211     {
    212         synchronized (this)
    213         {
    214             return _requests == 0 ? 0 : (_totalRequestTime / _requests);
    215         }
    216     }
    217 }