Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  //
  //  ========================================================================
  //  Copyright (c) 1995-2013 Mort Bay Consulting Pty. Ltd.
  //  ------------------------------------------------------------------------
  //  All rights reserved. This program and the accompanying materials
  //  are made available under the terms of the Eclipse Public License v1.0
  //  and Apache License v2.0 which accompanies this distribution.
  //
  //      The Eclipse Public License is available at
 //      http://www.eclipse.org/legal/epl-v10.html
 //
 //      The Apache License v2.0 is available at
 //      http://www.opensource.org/licenses/apache2.0.php
 //
 //  You may elect to redistribute this code under either of these licenses.
 //  ========================================================================
 //
 
 package org.eclipse.jetty.server.handler;
 
 
 
 
 public class StatisticsHandler extends HandlerWrapper
 {
     private final AtomicLong _statsStartedAt = new AtomicLong();
     
     private final CounterStatistic _requestStats = new CounterStatistic();
     private final SampleStatistic _requestTimeStats = new SampleStatistic();
     private final CounterStatistic _dispatchedStats = new CounterStatistic();
     private final SampleStatistic _dispatchedTimeStats = new SampleStatistic();
     private final CounterStatistic _suspendStats = new CounterStatistic();
 
     private final AtomicInteger _resumes = new AtomicInteger();
     private final AtomicInteger _expires = new AtomicInteger();
     
     private final AtomicInteger _responses1xx = new AtomicInteger();
     private final AtomicInteger _responses2xx = new AtomicInteger();
     private final AtomicInteger _responses3xx = new AtomicInteger();
     private final AtomicInteger _responses4xx = new AtomicInteger();
     private final AtomicInteger _responses5xx = new AtomicInteger();
     private final AtomicLong _responsesTotalBytes = new AtomicLong();
 
     private final ContinuationListener _onCompletion = new ContinuationListener()
     {
         public void onComplete(Continuation continuation)
         {
             final Request request = ((AsyncContinuation)continuation).getBaseRequest();
             final long elapsed = System.currentTimeMillis()-request.getTimeStamp();
             
             .decrement();
             .set(elapsed);
             
             updateResponse(request);
             
             if (!continuation.isResumed())
                 .decrement();
         }
 
         public void onTimeout(Continuation continuation)
         {
             .incrementAndGet();
         }
     };
    
    
Resets the current request statistics.
 
     public void statsReset()
     {
         .set(System.currentTimeMillis());
         
         .reset();
         .reset();
         .reset();
         .reset();
         .reset();
 
         .set(0);
         .set(0);
         .set(0);
         .set(0);
         .set(0);
         .set(0);
         .set(0);
         .set(0L);
    }
    @Override
    public void handle(String pathRequest requestHttpServletRequest httpRequestHttpServletResponse httpResponsethrows IOExceptionServletException
    {
        .increment();
        final long start;
        AsyncContinuation continuation = request.getAsyncContinuation();
        if (continuation.isInitial())
        {
            // new request
            .increment();
            start = request.getTimeStamp();
        }
        else
        {
            // resumed request
            start = System.currentTimeMillis();
            .decrement();
            if (continuation.isResumed())
                .incrementAndGet();
        }
        try
        {
            super.handle(pathrequesthttpRequesthttpResponse);
        }
        finally
        {
            final long now = System.currentTimeMillis();
            final long dispatched=now-start;
            
            .decrement();
            .set(dispatched);
            
            if (continuation.isSuspended())
            {
                if (continuation.isInitial())
                    continuation.addContinuationListener();
                .increment();
            }
            else if (continuation.isInitial())
            {
                .decrement();
                .set(dispatched);
                updateResponse(request);
            }
            // else onCompletion will handle it.
        }
    }
    private void updateResponse(Request request)
    {
        Response response = request.getResponse();
        switch (response.getStatus() / 100)
        {
            case 1:
                .incrementAndGet();
                break;
            case 2:
                .incrementAndGet();
                break;
            case 3:
                .incrementAndGet();
                break;
            case 4:
                .incrementAndGet();
                break;
            case 5:
                .incrementAndGet();
                break;
            default:
                break;
        }
    }
    @Override
    protected void doStart() throws Exception
    {
        super.doStart();
        statsReset();
    }

    

Returns:
the number of requests handled by this handler since statsReset() was last called, excluding active requests
See also:
getResumes()
    public int getRequests()
    {
        return (int).getTotal();
    }

    

Returns:
the number of requests currently active. since statsReset() was last called.
    public int getRequestsActive()
    {
        return (int).getCurrent();
    }

    

Returns:
the maximum number of active requests since statsReset() was last called.
    public int getRequestsActiveMax()
    {
        return (int).getMax();
    }

    

Returns:
the maximum time (in milliseconds) of request handling since statsReset() was last called.
    public long getRequestTimeMax()
    {
        return .getMax();
    }

    

Returns:
the total time (in milliseconds) of requests handling since statsReset() was last called.
    public long getRequestTimeTotal()
    {
        return .getTotal();
    }

    

Returns:
the mean time (in milliseconds) of request handling since statsReset() was last called.
See also:
getRequestTimeTotal()
getRequests()
    public double getRequestTimeMean()
    {
        return .getMean();
    }

    

Returns:
the standard deviation of time (in milliseconds) of request handling since statsReset() was last called.
See also:
getRequestTimeTotal()
getRequests()
    public double getRequestTimeStdDev()
    {
        return .getStdDev();
    }

    

Returns:
the number of dispatches seen by this handler since statsReset() was last called, excluding active dispatches
    public int getDispatched()
    {
        return (int).getTotal();
    }

    

Returns:
the number of dispatches currently in this handler since statsReset() was last called, including resumed requests
    public int getDispatchedActive()
    {
        return (int).getCurrent();
    }

    

Returns:
the max number of dispatches currently in this handler since statsReset() was last called, including resumed requests
    public int getDispatchedActiveMax()
    {
        return (int).getMax();
    }

    

Returns:
the maximum time (in milliseconds) of request dispatch since statsReset() was last called.
    public long getDispatchedTimeMax()
    {
        return .getMax();
    }
    
    

Returns:
the total time (in milliseconds) of requests handling since statsReset() was last called.
    public long getDispatchedTimeTotal()
    {
        return .getTotal();
    }

    

Returns:
the mean time (in milliseconds) of request handling since statsReset() was last called.
See also:
getRequestTimeTotal()
getRequests()
    public double getDispatchedTimeMean()
    {
        return .getMean();
    }
    
    

Returns:
the standard deviation of time (in milliseconds) of request handling since statsReset() was last called.
See also:
getRequestTimeTotal()
getRequests()
    public double getDispatchedTimeStdDev()
    {
        return .getStdDev();
    }
    
    

Returns:
the number of requests handled by this handler since statsReset() was last called, including resumed requests
See also:
getResumes()
    public int getSuspends()
    {
        return (int).getTotal();
    }

    

Returns:
the number of requests currently suspended. since statsReset() was last called.
    public int getSuspendsActive()
    {
        return (int).getCurrent();
    }

    

Returns:
the maximum number of current suspended requests since statsReset() was last called.
    public int getSuspendsActiveMax()
    {
        return (int).getMax();
    }
    
    

Returns:
the number of requests that have been resumed
See also:
getExpires()
    public int getResumes()
    {
        return .get();
    }

    

Returns:
the number of requests that expired while suspended.
See also:
getResumes()
    public int getExpires()
    {
        return .get();
    }

    

Returns:
the number of responses with a 1xx status returned by this context since statsReset() was last called.
    public int getResponses1xx()
    {
        return .get();
    }

    

Returns:
the number of responses with a 2xx status returned by this context since statsReset() was last called.
    public int getResponses2xx()
    {
        return .get();
    }

    

Returns:
the number of responses with a 3xx status returned by this context since statsReset() was last called.
    public int getResponses3xx()
    {
        return .get();
    }

    

Returns:
the number of responses with a 4xx status returned by this context since statsReset() was last called.
    public int getResponses4xx()
    {
        return .get();
    }

    

Returns:
the number of responses with a 5xx status returned by this context since statsReset() was last called.
    public int getResponses5xx()
    {
        return .get();
    }

    

Returns:
the milliseconds since the statistics were started with statsReset().
    public long getStatsOnMs()
    {
        return System.currentTimeMillis() - .get();
    }
    
    

Returns:
the total bytes of content sent in responses
    public long getResponsesBytesTotal()
    {
        return .get();
    }
    
    public String toStatsHTML()
    {   
        StringBuilder sb = new StringBuilder();
        sb.append("<h1>Statistics:</h1>\n");
        sb.append("Statistics gathering started ").append(getStatsOnMs()).append("ms ago").append("<br />\n");
        sb.append("<h2>Requests:</h2>\n");
        sb.append("Total requests: ").append(getRequests()).append("<br />\n");
        sb.append("Active requests: ").append(getRequestsActive()).append("<br />\n");
        sb.append("Max active requests: ").append(getRequestsActiveMax()).append("<br />\n");
        sb.append("Total requests time: ").append(getRequestTimeTotal()).append("<br />\n");
        sb.append("Mean request time: ").append(getRequestTimeMean()).append("<br />\n");
        sb.append("Max request time: ").append(getRequestTimeMax()).append("<br />\n");
        sb.append("Request time standard deviation: ").append(getRequestTimeStdDev()).append("<br />\n");
        
        sb.append("<h2>Dispatches:</h2>\n");
        sb.append("Total dispatched: ").append(getDispatched()).append("<br />\n");
        sb.append("Active dispatched: ").append(getDispatchedActive()).append("<br />\n");
        sb.append("Max active dispatched: ").append(getDispatchedActiveMax()).append("<br />\n");
        sb.append("Total dispatched time: ").append(getDispatchedTimeTotal()).append("<br />\n");
        sb.append("Mean dispatched time: ").append(getDispatchedTimeMean()).append("<br />\n");
        sb.append("Max dispatched time: ").append(getDispatchedTimeMax()).append("<br />\n");
        sb.append("Dispatched time standard deviation: ").append(getDispatchedTimeStdDev()).append("<br />\n");
        sb.append("Total requests suspended: ").append(getSuspends()).append("<br />\n");
        sb.append("Total requests expired: ").append(getExpires()).append("<br />\n");
        sb.append("Total requests resumed: ").append(getResumes()).append("<br />\n");
        
        sb.append("<h2>Responses:</h2>\n");
        sb.append("1xx responses: ").append(getResponses1xx()).append("<br />\n");
        sb.append("2xx responses: ").append(getResponses2xx()).append("<br />\n");
        sb.append("3xx responses: ").append(getResponses3xx()).append("<br />\n");
        sb.append("4xx responses: ").append(getResponses4xx()).append("<br />\n");
        sb.append("5xx responses: ").append(getResponses5xx()).append("<br />\n");
        sb.append("Bytes sent total: ").append(getResponsesBytesTotal()).append("<br />\n");
        return sb.toString();
    }
New to GrepCode? Check out our FAQ X