KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > web > loadbalancer > scheduler > HostStatistics


1 /*
2  * JBoss, the OpenSource WebOS
3  *
4  * Distributable under LGPL license.
5  * See terms of license at gnu.org.
6  */

7 package org.jboss.web.loadbalancer.scheduler;
8
9 /**
10  * This class holds statistics for a node.
11  *
12  * @author Thomas Peuss <jboss@peuss.de>
13  * @version $Revision: 1.3 $
14  */

15 public class HostStatistics
16 {
17   private static final int NUM_SAMPLES=50;
18
19   private volatile long numRequests;
20   private volatile long sumResponseTime;
21   private volatile int currentConnections=0;
22   private volatile int[] lastResponseTimes=new int[NUM_SAMPLES];
23   private volatile int index=0;
24
25   public void addRequest(int responseTime)
26   {
27     numRequests++;
28     sumResponseTime+=responseTime;
29
30     try
31     {
32         lastResponseTimes[index]=responseTime;
33         index++;
34     }
35     catch (ArrayIndexOutOfBoundsException JavaDoc aioobe)
36     {
37         index=0;
38     }
39   }
40
41   public void reset()
42   {
43     sumResponseTime=0L;
44     numRequests=0L;
45     lastResponseTimes=new int[NUM_SAMPLES];
46     index=0;
47   }
48
49   public int getAvgResponseTime()
50   {
51     return sumLastResponseTimes()/NUM_SAMPLES;
52   }
53
54   public long getNumRequests()
55   {
56     return numRequests;
57   }
58
59   public long getSumResponseTime()
60   {
61     return sumResponseTime;
62   }
63
64   public int getCurrentConnections()
65   {
66     return currentConnections;
67   }
68
69   public void incCurrentConnections()
70   {
71      currentConnections++;
72   }
73
74   public void decCurrentConnections()
75   {
76      currentConnections--;
77   }
78
79   public String JavaDoc toString()
80   {
81     return "[CurrentConnections="+currentConnections+", Requests="+numRequests+", AvgResponseTime="+getAvgResponseTime()+"]";
82   }
83
84   private int sumLastResponseTimes()
85   {
86      int sum=0;
87
88      for (int i=0;i<NUM_SAMPLES;++i)
89      {
90         sum+=lastResponseTimes[i];
91      }
92    
93      return sum;
94   }
95 }
96
Popular Tags