Re: How can I display number of requests over the past n seconds?

2004-08-04 Thread Rodrigo Ruiz
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: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]


Re: How can I display number of requests over the past n seconds?

2004-08-02 Thread Justin Ruthenbeck
More academic exercise than recommendation, but...
Write your own implementation of a Map/HashMap
specific to java.util.Date objects.
Identify your smallest time increment (5 seconds in your email).
Identify largest time increment (60 seconds)
Create (60/5)+1 buckets for your hash function
Write your hash function such that each date object -- representing one 
request each -- gets placed in the right bucket.  If the bucket contains 
aged dates, which you should know by simple counting (not comparison), 
empty it before placing a new one in.

To display results, simply add up the content size of the appropriate 
buckets and return.

This implementation is more lightweight than others suggested because the 
process of pruning is built into your implementation (instead of using 
external threads or comparisons each time you insert)... you're trading 
memory usage for decreased processing time.  On the other hand, it 
doesn't *strictly* tell you how many requests came in the last X seconds 
-- only how many came since the last time unit started.  It's also only 
reasonable for numbers like you suggested -- Tim or Yoav's suggestions 
would be better if your range is 5 seconds to 60 minutes, for example.

justin
At 07:10 AM 7/29/2004, you wrote:
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

__
Justin Ruthenbeck
Software Engineer, NextEngine Inc.
justinr - AT - nextengine DOT com
Confidential. See:
http://www.nextengine.com/confidentiality.php
__
-
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]


How can I display number of requests over the past n seconds?

2004-07-29 Thread tom ly
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 

Re: How can I display number of requests over the past n seconds?

2004-07-29 Thread Tim Funk
You could use a list of java.util.Dates with the oldest date at the beginning 
of the list and the newest at the end of the List. Push new Dates onto the 
list and pull (expired) dates from the front of the list accoring to your 
threshold.

The number of hits per unit time is the size of each list.
-Tim
tom ly wrote:
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.  

-
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]


RE: How can I display number of requests over the past n seconds?

2004-07-29 Thread Shapira, Yoav
Hi,
You would need to modify your filter.  One approach would be to have
separate counters, one for each time period, with a monitor thread for
each one.  The monitor thread would sleep for the duration of the timer,
then reset it.  The Filter would initialize these counters and threads,
then increment each timer by one every time a request comes in.  The JSP
would simply read the counter values.

Yoav Shapira
Millennium Research Informatics


-Original Message-
From: tom ly [mailto:[EMAIL PROTECTED]
Sent: Thursday, July 29, 2004 10:11 AM
To: [EMAIL PROTECTED]
Subject: How can I display number of requests over the past n seconds?

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

-
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]