KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > whirlycott > cache > RecordKeeper


1 /*
2  Copyright 2004 Philip Jacob <phil@whirlycott.com>
3  Seth Fitzsimmons <seth@note.amherst.edu>
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
18 package com.whirlycott.cache;
19
20 import java.io.Serializable JavaDoc;
21
22 /**
23  * Encapsulates runtime stats for a Cache.
24  *
25  * @author phil
26  */

27 public class RecordKeeper implements Serializable JavaDoc {
28
29     /**
30      *
31      */

32     private static final long serialVersionUID = -8354128118267818665L;
33
34     /** The total number of cache hits. */
35     private volatile long hits;
36
37     /** Lock used for the hits value. */
38     private final Object JavaDoc hitLock = new Object JavaDoc();
39
40     /** The total number of cache questions. */
41     private volatile long totalOperations;
42
43     /** Lock used for the operation value. */
44     private final Object JavaDoc operationLock = new Object JavaDoc();
45
46     private long totalOperationsStartTuneCycle;
47
48     private long queriesPerSecond;
49
50     /**
51      * @return Returns the hits.
52      */

53     public long getHits() {
54         return hits;
55     }
56
57     /**
58      * @param hits
59      * The hits to set.
60      */

61     public void setHits(final long hits) {
62         this.hits = hits;
63     }
64
65     /**
66      * @return Returns the totalOperations.
67      */

68     public long getTotalOperations() {
69         return totalOperations;
70     }
71
72     /** Increment the total operation counter. */
73     public void incrementTotalOperations() {
74         synchronized (operationLock) {
75             totalOperations++;
76         }
77     }
78
79     /** Increment hits. */
80     public void incrementHits() {
81         synchronized (hitLock) {
82             hits++;
83         }
84     }
85
86     public void startTuneCycle() {
87         totalOperationsStartTuneCycle = totalOperations;
88     }
89
90     public void calculateQueriesPerSecond(final long sleepTime) {
91         if (sleepTime > 0L) {
92             queriesPerSecond = (totalOperations - totalOperationsStartTuneCycle) / (sleepTime / 1000L);
93         } else {
94             queriesPerSecond = 0;
95         }
96     }
97
98     public long getQueriesPerSecond() {
99         return queriesPerSecond;
100     }
101
102     /**
103      * Reset the values.
104      */

105     public void reset() {
106         totalOperations = 0L;
107         hits = 0L;
108     }
109 }
110
Popular Tags