Return-Path: Delivered-To: apmail-jakarta-tomcat-user-archive@www.apache.org Received: (qmail 63644 invoked from network); 4 Aug 2004 08:59:02 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (209.237.227.199) by minotaur-2.apache.org with SMTP; 4 Aug 2004 08:59:02 -0000 Received: (qmail 89264 invoked by uid 500); 4 Aug 2004 08:27:31 -0000 Delivered-To: apmail-jakarta-tomcat-user-archive@jakarta.apache.org Received: (qmail 89249 invoked by uid 500); 4 Aug 2004 08:27:31 -0000 Mailing-List: contact tomcat-user-help@jakarta.apache.org; run by ezmlm Precedence: bulk List-Unsubscribe: List-Subscribe: List-Help: List-Post: List-Id: "Tomcat Users List" Reply-To: "Tomcat Users List" Delivered-To: mailing list tomcat-user@jakarta.apache.org Received: (qmail 89236 invoked by uid 99); 4 Aug 2004 08:27:31 -0000 X-ASF-Spam-Status: No, hits=0.0 required=10.0 tests= X-Spam-Check-By: apache.org Received: from [194.179.36.183] (HELO telesto) (194.179.36.183) by apache.org (qpsmtpd/0.27.1) with ESMTP; Wed, 04 Aug 2004 01:27:28 -0700 Received: from localhost ([213.172.39.84]) by telesto (8.12.11/8.12.11) with SMTP id i748Mu1R023299 for ; Wed, 4 Aug 2004 10:22:56 +0200 (CEST) Received: from 127.0.0.1 (AVG SMTP 6.0.0 [487]); Wed, 04 Aug 2004 10:28:54 +0200 Message-ID: <41109E46.6040603@gridsystems.com> Date: Wed, 04 Aug 2004 10:28:54 +0200 From: Rodrigo Ruiz Organization: Grid Systems User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.7) Gecko/20040616 X-Accept-Language: en-us, en To: Tomcat Users List Subject: Re: How can I display number of requests over the past n seconds? References: <20040729141054.43040.qmail@web53203.mail.yahoo.com> In-Reply-To: <20040729141054.43040.qmail@web53203.mail.yahoo.com> Mime-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset=ISO-8859-1; format=flowed X-Scanned-By: MIMEDefang 2.43 X-Virus-Checked: Checked X-Spam-Rating: minotaur-2.apache.org 1.6.2 0/1000/N Mmmm, I think people is answering more than one question ;-) If you need to be strict with respect to the timings, that is, you need exactly the number of requests in the past five seconds counting from the moment your request is done, you will need the solution proposed by Tim. Just remember to synchronize the access to the list, to prevent ConcurrentModification exceptions while you iterate over the list to get the counter values :-) If the "time buckets" idea works well for you, both Yoav and Justin proposals are good. I just prefer Yoav one because I guess the cost associated to the hash function, even if it is simple, is greater than the associated to a thread that is sleeping most of the time (I mean, in processing time terms). I think Yoav idea could be implemented like this (with a variation on my own ;-): public class Historic extends Thread { private static final int NUM_COUNTERS = 6; private static final int INTERVAL = 5000; private int[] counters = new int[NUM_COUNTERS]; private int pos = 0; public Historic() { super(); this.setDaemon(true); } public void run() { try { while (true) { sleep(INTERVAL); synchronized (this) { pos = (pos + 1) % NUM_COUNTERS; counters[pos] = 0; } } }catch (InterruptedException e) { } } public synchronized void addRequest() { counters[pos] += 1; } public int[] getCounters() { int[] copy; int posCopy; synchronized (this) { System.arraycopy(counters, 0, copy, 0, NUM_COUNTERS); posCopy = pos; } int[] values = new int[NUM_COUNTERS]; int count = 0; int index = posCopy; for (int i = 0; i < NUM_COUNTERS; i++) { count += copy[index]; values[i] = count; index = (index == 0 ? NUM_COUNTERS : index) - 1; } return values; } } Just instantiate the class and "start" it in your Filter initialization, and call addRequest() on each received request. The getCounters() gives you the number of requests in each time slice. Ah, and remember to interrupt it on Filter finalization :-) There is no guaranty that the "pos" variable will be changed exactly each five seconds, as the sleep() method is not accurate by design. In a highly loaded server with only one CPU, the time slicing will become worse. If you detect the thread scheduling is too much biased, you can raise its priority. If this is not enough, you will have to calculate the cursor position in each request, implementing the hashtable proposed by Justin: public class Historic { private static final int NUM_COUNTERS = 6; private static final int INTERVAL = 5000; private int[] counters = new int[NUM_COUNTERS]; private int pos = 0; private long ts = System.currentTimeMillis(); public Historic() { } public synchronized void addRequest() { adjust(); counters[pos] += 1; } public int[] getCounters() { int[] copy; int posCopy; synchronized (this) { adjust(); System.arraycopy(counters, 0, copy, 0, NUM_COUNTERS); posCopy = pos; } int[] values = new int[NUM_COUNTERS]; int count = 0; int index = posCopy; for (int i = 0; i < NUM_COUNTERS; i++) { count += copy[index]; values[i] = count; index = (index == 0 ? NUM_COUNTERS : index) - 1; } return values; } private void adjust() { long now = System.currentTimeMillis(); while (now - ts >= INTERVAL) { pos = (pos + 1) % NUM_COUNTERS; counters[pos] = 0; ts += INTERVAL; } } } HTH, Rodrigo Ruiz >I need to display on a .jsp page the number of requests for Tomcat in the past 5 / 10 / 15/ 30 / 45 / 60 seconds. I've already implement a Filter will count the total number of requests. I did this with a static int, which is incremented everytime a request comes in. But what should I do so that I can show number of request over past time intervals? Since the present time is always changing, "the past n seconds" is constantly changing also. >Thanks in advance, >Tom > >__________________________________________________ >Do You Yahoo!? >Tired of spam? Yahoo! Mail has the best spam protection around >http://mail.yahoo.com > > >------------------------------------------------------------------------ > >Incoming mail is certified Virus Free. >Checked by AVG anti-virus system (http://www.grisoft.com). >Version: 6.0.727 / Virus Database: 482 - Release Date: 28/07/2004 > > -- Outgoing mail is certified Virus Free. Checked by AVG anti-virus system (http://www.grisoft.com). Version: 6.0.733 / Virus Database: 487 - Release Date: 02/08/2004 --------------------------------------------------------------------- To unsubscribe, e-mail: tomcat-user-unsubscribe@jakarta.apache.org For additional commands, e-mail: tomcat-user-help@jakarta.apache.org