KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > activemq > perf > PerfRate


1 /**
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one or more
4  * contributor license agreements. See the NOTICE file distributed with
5  * this work for additional information regarding copyright ownership.
6  * The ASF licenses this file to You under the Apache License, Version 2.0
7  * (the "License"); you may not use this file except in compliance with
8  * the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18 package org.apache.activemq.perf;
19
20 /**
21  * @version $Revision: 1.3 $
22  */

23 public class PerfRate{
24     
25     protected int totalCount;
26     protected int count;
27     protected long startTime=System.currentTimeMillis();
28     
29     /**
30      * @return Returns the count.
31      */

32     public int getCount(){
33         return totalCount;
34     }
35
36     synchronized public void increment(){
37         totalCount++;
38         count++;
39     }
40
41     public int getRate(){
42         long endTime=System.currentTimeMillis();
43         long totalTime=endTime-startTime;
44         int result=(int) ((count*1000)/totalTime);
45         return result;
46     }
47     
48     /**
49      * Resets the rate sampling.
50      */

51     synchronized public PerfRate cloneAndReset() {
52         PerfRate rc = new PerfRate();
53         rc.totalCount = totalCount;
54         rc.count=count;
55         rc.startTime=startTime;
56         count=0;
57         startTime=System.currentTimeMillis();
58         return rc;
59     }
60     
61     /**
62      * Resets the rate sampling.
63      */

64     public void reset() {
65         count=0;
66         startTime=System.currentTimeMillis();
67     }
68
69     /**
70      * @return Returns the totalCount.
71      */

72     public int getTotalCount(){
73         return totalCount;
74     }
75     
76     /**
77      * @param totalCount
78      * The totalCount to set.
79      */

80     public void setTotalCount(int totalCount){
81         this.totalCount=totalCount;
82     }
83 }
84
Popular Tags