View Javadoc

    1   package org.mortbay.jetty.handler;
    2
    3   import java.io.IOException;
    4   import java.util.concurrent.atomic.AtomicInteger;
    5   import java.util.concurrent.atomic.AtomicLong;
    6   import javax.servlet.ServletException;
    7   import javax.servlet.http.HttpServletRequest;
    8   import javax.servlet.http.HttpServletResponse;
    9
    10  import org.mortbay.jetty.HttpConnection;
    11  import org.mortbay.jetty.Response;
    12
    13  /**
    14   * @version $Revision$ $Date: 2009-06-17 07:36:08 +1000 (Wed, 17 Jun 2009) $
    15   */
    16  public class AtomicStatisticsHandler extends AbstractStatisticsHandler
    17  {
    18      private transient final AtomicLong _statsStartedAt = new AtomicLong();
    19      private transient final AtomicInteger _requests = new AtomicInteger();
    20      private transient final AtomicLong _minRequestTime = new AtomicLong();
    21      private transient final AtomicLong _maxRequestTime = new AtomicLong();
    22      private transient final AtomicLong _totalRequestTime = new AtomicLong();
    23      private transient final AtomicInteger _requestsActive = new AtomicInteger();
    24      private transient final AtomicInteger _requestsActiveMax = new AtomicInteger();
    25      private transient final AtomicInteger _responses1xx = new AtomicInteger();
    26      private transient final AtomicInteger _responses2xx = new AtomicInteger();
    27      private transient final AtomicInteger _responses3xx = new AtomicInteger();
    28      private transient final AtomicInteger _responses4xx = new AtomicInteger();
    29      private transient final AtomicInteger _responses5xx = new AtomicInteger();
    30
    31      public void handle(String target, HttpServletRequest request, HttpServletResponse response, int dispatch) throws IOException, ServletException
    32      {
    33          _requests.incrementAndGet();
    34          int actives = _requestsActive.incrementAndGet();
    35          // Update the max value, using a non-blocking algorithm
    36          int oldMaxActives = _requestsActiveMax.get();
    37          while (actives > oldMaxActives)
    38          {
    39              if (_requestsActiveMax.compareAndSet(oldMaxActives, actives)) break;
    40              oldMaxActives = _requestsActiveMax.get();
    41          }
    42
    43          long requestStartTime = System.currentTimeMillis();
    44          try
    45          {
    46              super.handle(target, request, response, dispatch);
    47          }
    48          finally
    49          {
    50              long requestTime = System.currentTimeMillis() - requestStartTime;
    51
    52              // Set to 0 if the value is negative, using a non-blocking algorithm
    53              actives = _requestsActive.decrementAndGet();
    54              while (actives < 0)
    55              {
    56                  if (_requestsActive.compareAndSet(actives, 0)) break;
    57                  actives = _requestsActive.get();
    58              }
    59
    60              // Update the times, using a non-blocking algorithm
    61              long oldMinTime = _minRequestTime.get();
    62              while (requestTime < oldMinTime)
    63              {
    64                  if (_minRequestTime.compareAndSet(oldMinTime, requestTime)) break;
    65                  oldMinTime = _minRequestTime.get();
    66              }
    67              long oldMaxTime = _maxRequestTime.get();
    68              while (requestTime > oldMaxTime)
    69              {
    70                  if (_maxRequestTime.compareAndSet(oldMaxTime, requestTime)) break;
    71                  oldMaxTime = _maxRequestTime.get();
    72              }
    73              _totalRequestTime.addAndGet(requestTime);
    74
    75              Response jettyResponse = (response instanceof Response) ? (Response) response : HttpConnection.getCurrentConnection().getResponse();
    76              switch (jettyResponse.getStatus() / 100)
    77              {
    78                  case 1:
    79                      _responses1xx.incrementAndGet();
    80                      break;
    81                  case 2:
    82                      _responses2xx.incrementAndGet();
    83                      break;
    84                  case 3:
    85                      _responses3xx.incrementAndGet();
    86                      break;
    87                  case 4:
    88                      _responses4xx.incrementAndGet();
    89                      break;
    90                  case 5:
    91                      _responses5xx.incrementAndGet();
    92                      break;
    93                  default:
    94                      break;
    95              }
    96          }
    97      }
    98
    99      public void statsReset()
    100     {
    101         _statsStartedAt.set(System.currentTimeMillis());
    102         _requests.set(0);
    103         _minRequestTime.set(Long.MAX_VALUE);
    104         _maxRequestTime.set(0L);
    105         _totalRequestTime.set(0L);
    106         _requestsActive.set(0);
    107         _requestsActiveMax.set(0);
    108         _responses1xx.set(0);
    109         _responses2xx.set(0);
    110         _responses3xx.set(0);
    111         _responses4xx.set(0);
    112         _responses5xx.set(0);
    113     }
    114
    115     public int getRequests()
    116     {
    117         return _requests.get();
    118     }
    119
    120     public int getRequestsActive()
    121     {
    122         return _requestsActive.get();
    123     }
    124
    125     public int getRequestsActiveMax()
    126     {
    127         return _requestsActiveMax.get();
    128     }
    129
    130     public int getResponses1xx()
    131     {
    132         return _responses1xx.get();
    133     }
    134
    135     public int getResponses2xx()
    136     {
    137         return _responses2xx.get();
    138     }
    139
    140     public int getResponses3xx()
    141     {
    142         return _responses3xx.get();
    143     }
    144
    145     public int getResponses4xx()
    146     {
    147         return _responses4xx.get();
    148     }
    149
    150     public int getResponses5xx()
    151     {
    152         return _responses5xx.get();
    153     }
    154
    155     public long getStatsOnMs()
    156     {
    157         return System.currentTimeMillis() - _statsStartedAt.get();
    158     }
    159
    160     public long getRequestTimeMin()
    161     {
    162         return _minRequestTime.get();
    163     }
    164
    165     public long getRequestTimeMax()
    166     {
    167         return _maxRequestTime.get();
    168     }
    169
    170     public long getRequestTimeTotal()
    171     {
    172         return _totalRequestTime.get();
    173     }
    174
    175     public long getRequestTimeAverage()
    176     {
    177         int requests = getRequests();
    178         return requests == 0 ? 0 : getRequestTimeTotal() / requests;
    179     }
    180 }