RecordKeeper.java :  » Cache » whirlycache » com » whirlycott » cache » Java Open Source

Java Open Source » Cache » whirlycache 
whirlycache » com » whirlycott » cache » RecordKeeper.java
/*
 Copyright 2004      Philip Jacob <phil@whirlycott.com>
 Seth Fitzsimmons <seth@note.amherst.edu>

 Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License.
 You may obtain a copy of the License at

 http://www.apache.org/licenses/LICENSE-2.0

 Unless required by applicable law or agreed to in writing, software
 distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 See the License for the specific language governing permissions and
 limitations under the License.
 */

package com.whirlycott.cache;

import java.io.Serializable;
import java.util.concurrent.atomic.AtomicLong;

/**
 * Encapsulates runtime stats for a Cache.
 *
 * @author phil
 */
public class RecordKeeper implements Serializable {

    /**
     *
     */
    private static final long serialVersionUID = -8354128118267818665L;

    /**
     * The total number of cache hits.
     */
    private final AtomicLong hits = new AtomicLong();

    /**
     * The total number of cache questions.
     */
    private final AtomicLong totalOperations = new AtomicLong();

    /**
     * Total operations as of when this tune cycle started
     */
    private long totalOperationsStartTuneCycle;

    /**
     * Millisecond timestamp of when this tune cycle started.
     */
    private long startTuneCycle;

    private long queriesPerSecond;

    /**
     * @return Returns the hits.
     */
    public long getHits() {
        return hits.get();
    }

    /**
     * @param hits The hits to set.
     */
    public void setHits(final long hits) {
        this.hits.set(hits);
    }

    /**
     * @return Returns the totalOperations.
     */
    public long getTotalOperations() {
        return totalOperations.get();
    }

    /**
     * Increment the total operation counter.
     */
    public void incrementTotalOperations() {
        totalOperations.incrementAndGet();
    }

    /**
     * Increment hits.
     */
    public void incrementHits() {
        hits.incrementAndGet();
    }

    public void startTuneCycle() {
        totalOperationsStartTuneCycle = totalOperations.get();
        startTuneCycle = System.currentTimeMillis();
    }

    public void calculateQueriesPerSecond() {
        long sleepTime = System.currentTimeMillis() - startTuneCycle;

        if (sleepTime > 0L) {
            queriesPerSecond = (getTotalOperations() - totalOperationsStartTuneCycle) / (sleepTime / 1000L);
        } else {
            queriesPerSecond = 0;
        }
    }

    public long getQueriesPerSecond() {
        return queriesPerSecond;
    }

    /**
     * Reset the values.
     */
    public void reset() {
        totalOperations.set(0);
        hits.set(0);
    }
}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.