KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > jmeter > monitor > util > Stats


1 // $Header: /home/cvs/jakarta-jmeter/src/monitor/components/org/apache/jmeter/monitor/util/Stats.java,v 1.5.2.1 2004/10/11 01:01:22 sebb Exp $
2
/*
3  * Copyright 2004 The Apache Software Foundation.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17 package org.apache.jmeter.monitor.util;
18
19 import org.apache.jmeter.monitor.model.Connector;
20 import org.apache.jmeter.monitor.model.Status;
21
22 /**
23  *
24  * Description:<p>
25  * Stats is responsible for calculating the load and
26  * health of a given server. It uses tomcat's status
27  * servlet results. A schema was generated for the
28  * XML output and JAXB was used to generate classes.
29  * <p>
30  * The equations are:<p>
31  * memory weight = (int)(33 * (used/max))<br>
32  * thread weight = (int)(67 * (current/max))<p>
33  * The load factors are stored in the properties
34  * files. Simply change the values in the properties
35  * to change how load is calculated. The defaults
36  * values are memory (33) and threads (67). The sum
37  * of the factors must equal 100.
38  */

39 public class Stats
40 {
41
42     public static final int DEAD = 0;
43     public static final int ACTIVE = 2;
44     public static final int WARNING = 1;
45     public static final int HEALTHY = 3;
46
47     public static final int DEFAULT_MEMORY_FACTOR = 50;
48     public static final int DEFAULT_THREAD_FACTOR = 50;
49     public static final double HEALTHY_PER = 0.00;
50     public static final double ACTIVE_PER = 0.25;
51     public static final double WARNING_PER = 0.67;
52
53     /**
54      * The method is responsible for taking a status
55      * object and calculating an int value from 1 to
56      * 100. We use a combination of free memory and
57      * free threads. Since memory is a bigger risk,
58      * it counts for 2/3.<p>
59      * @param stat
60      * @return calculated load value
61      */

62     public static int calculateLoad(Status stat){
63         if (stat != null){
64             // equation for calculating the weight
65
// w = (int)(33 * (used/max))
66
long totMem = stat.getJvm().getMemory().getTotal();
67             long usedMem = stat.getJvm().getMemory().getFree();
68             double memdiv = (double)usedMem/(double)totMem;
69             double memWeight = DEFAULT_MEMORY_FACTOR * memdiv;
70
71             Connector cntr = (Connector)stat.getConnector().get(0);
72             int maxThread = cntr.getThreadInfo().getMaxThreads();
73             int curThread = cntr.getThreadInfo().getCurrentThreadsBusy();
74             double thdiv = (double)curThread/(double)maxThread;
75             double threadWeight = DEFAULT_THREAD_FACTOR * thdiv;
76             return (int)(memWeight + threadWeight);
77         } else {
78             return 0;
79         }
80     }
81     
82     /**
83      * Method should calculate if the server is:
84      * dead, active, warning or healthy. We do
85      * this by looking at the current busy threads.
86      * <ol>
87      * <li> free &gt; spare is healthy
88      * <li> free &lt; spare is active
89      * <li> busy threads &gt; 75% is warning
90      * <li> none of the above is dead
91      * </ol>
92      * @param stat
93      * @return integer representing the status
94      */

95     public static int calculateStatus(Status stat){
96         if (stat != null){
97             Connector cntr = (Connector)stat.getConnector().get(0);
98             int max = cntr.getThreadInfo().getMaxThreads();
99             int current = cntr.getThreadInfo().getCurrentThreadsBusy();
100             //int spare = cntr.getThreadInfo().getMaxSpareThreads();
101
double per = (double)current/(double)max;
102             if (per > WARNING_PER){
103                 return WARNING;
104             } else if (per >= ACTIVE_PER && per <= WARNING_PER){
105                 return ACTIVE;
106             } else if (per < ACTIVE_PER && per > HEALTHY_PER){
107                 return HEALTHY;
108             } else {
109                 return DEAD;
110             }
111         } else {
112             return DEAD;
113         }
114     }
115
116     /**
117      * Method will calculate the memory load:
118      * used / max = load. The load value is an
119      * integer between 1 and 100. It is the
120      * percent memory used.
121      * @param stat
122      * @return memory load
123      */

124     public static int calculateMemoryLoad(Status stat){
125         double load = 0;
126         if (stat != null){
127             double total = (double)stat.getJvm().getMemory().getTotal();
128             double used = (double)stat.getJvm().getMemory().getFree();
129             load = (used/total);
130         }
131         return (int)(load * 100);
132     }
133
134     /**
135      * Method will calculate the thread load:
136      * busy / max = load. The value is an
137      * integer between 1 and 100. It is the
138      * percent busy.
139      * @param stat
140      * @return thread load
141      */

142     public static int calculateThreadLoad(Status stat){
143         int load = 0;
144         if (stat != null){
145             Connector cntr = (Connector)stat.getConnector().get(0);
146             double max = (double)cntr.getThreadInfo().getMaxThreads();
147             double current =
148                 (double)cntr.getThreadInfo().getCurrentThreadsBusy();
149             load = (int)((current/max) * 100);
150         }
151         return load;
152     }
153     
154 }
155
Popular Tags