开发者

How do I make "simple" throughput servlet-filter?

开发者 https://www.devze.com 2022-12-31 20:32 出处:网络
I\'m looking to create a filter that can give me two things: number of request pr minute, and average responsetime pr minute. I already got the individual readings, I\'m just not sure how to add them

I'm looking to create a filter that can give me two things: number of request pr minute, and average responsetime pr minute. I already got the individual readings, I'm just not sure how to add them up.

My filter captures every request, and it records开发者_运维知识库 the time each request takes:

public void doFilter(ServletRequest request, ...()  
{       
    long start = System.currentTimeMillis(); 
    chain.doFilter(request, response);
    long stop = System.currentTimeMillis();

    String time = Util.getTimeDifferenceInSec(start, stop);
}

This information will be used to create some pretty Google Chart charts. I don't want to store the data in any database. Just a way to get current numbers out when requested

As this is a high volume application; low overhead is essential.

I'm assuming my applicationserver doesn't provide this information.


I did something similar once. If I remember well, I had something like

public class StatisticsFilter implements ... 
{
    public static Statistics stats;

    public class PeriodicDumpStat extends Thread
    {
       ...
    }

    public void doFilter(ServletRequest request, ...()  
    {       
      long start = System.currentTimeMillis(); 
      chain.doFilter(request, response);
      long stop = System.currentTimeMillis();
      stats.add( stop - start ); 
    }

    public void init()
    {
       Thread t = new PeriodicDumpStat();
       t.setDaemon( true );
       t.start();
    }
}

(That's only a sketch)

Make sure that the Statistics object is correctly synchronize, as it will be accessed concurrently.

I had a background DumpStatistics thread that was periodically dumping the stats in an XML file, to be processed later. For better encapsulation, I had the thread as an inner class. You can of course use Runnable as well. As @Trevor Tippins pointed out, it's also good to flag the thread as daemon thread.

I also used Google Chart and had actually another ShowStatisticsServlet that would rad the XML file and turn the data into a nice Chart. The servlet would not depends on the filter, but only on the XML file, so both were actually decoupled. The XML file can be created as a temporary file with File.createTempFile. (Another variant would be of course to keep all data in memory, but storing the data was handy for us to backup the results of perf. tests and analyze them later)

Some colleague claimed that the synchronization in the Statistics object would "kill" the app performance, but in practice it was really neglectable. The overhead to dump the file as well, given that it was done each 10 sec or so.

Hope it helps, or give you some ideas.

PS: And as @William Louth commented, you should write such infrastructure code only if you can't solve your issue with an existing solution. In my case, I was also benchmarking the internal time of my code, not only the complete request processing time.

0

精彩评论

暂无评论...
验证码 换一张
取 消