001: /*
002: Copyright 2004 Philip Jacob <phil@whirlycott.com>
003: Seth Fitzsimmons <seth@note.amherst.edu>
004:
005: Licensed under the Apache License, Version 2.0 (the "License");
006: you may not use this file except in compliance with the License.
007: You may obtain a copy of the License at
008:
009: http://www.apache.org/licenses/LICENSE-2.0
010:
011: Unless required by applicable law or agreed to in writing, software
012: distributed under the License is distributed on an "AS IS" BASIS,
013: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: See the License for the specific language governing permissions and
015: limitations under the License.
016: */
017:
018: package com.whirlycott.cache;
019:
020: import java.io.Serializable;
021: import java.util.concurrent.atomic.AtomicLong;
022:
023: /**
024: * Encapsulates runtime stats for a Cache.
025: *
026: * @author phil
027: */
028: public class RecordKeeper implements Serializable {
029:
030: /**
031: *
032: */
033: private static final long serialVersionUID = -8354128118267818665L;
034:
035: /**
036: * The total number of cache hits.
037: */
038: private final AtomicLong hits = new AtomicLong();
039:
040: /**
041: * The total number of cache questions.
042: */
043: private final AtomicLong totalOperations = new AtomicLong();
044:
045: /**
046: * Total operations as of when this tune cycle started
047: */
048: private long totalOperationsStartTuneCycle;
049:
050: /**
051: * Millisecond timestamp of when this tune cycle started.
052: */
053: private long startTuneCycle;
054:
055: private long queriesPerSecond;
056:
057: /**
058: * @return Returns the hits.
059: */
060: public long getHits() {
061: return hits.get();
062: }
063:
064: /**
065: * @param hits The hits to set.
066: */
067: public void setHits(final long hits) {
068: this .hits.set(hits);
069: }
070:
071: /**
072: * @return Returns the totalOperations.
073: */
074: public long getTotalOperations() {
075: return totalOperations.get();
076: }
077:
078: /**
079: * Increment the total operation counter.
080: */
081: public void incrementTotalOperations() {
082: totalOperations.incrementAndGet();
083: }
084:
085: /**
086: * Increment hits.
087: */
088: public void incrementHits() {
089: hits.incrementAndGet();
090: }
091:
092: public void startTuneCycle() {
093: totalOperationsStartTuneCycle = totalOperations.get();
094: startTuneCycle = System.currentTimeMillis();
095: }
096:
097: public void calculateQueriesPerSecond() {
098: long sleepTime = System.currentTimeMillis() - startTuneCycle;
099:
100: if (sleepTime > 0L) {
101: queriesPerSecond = (getTotalOperations() - totalOperationsStartTuneCycle)
102: / (sleepTime / 1000L);
103: } else {
104: queriesPerSecond = 0;
105: }
106: }
107:
108: public long getQueriesPerSecond() {
109: return queriesPerSecond;
110: }
111:
112: /**
113: * Reset the values.
114: */
115: public void reset() {
116: totalOperations.set(0);
117: hits.set(0);
118: }
119: }
|