tomcat-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Rodrigo Ruiz <rr...@gridsystems.com>
Subject Re: How can I display number of requests over the past n seconds?
Date Wed, 04 Aug 2004 08:28:54 GMT
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


Mime
View raw message