BasicTransientMonitor.java 5.94 KB
Newer Older
Matt Tucker's avatar
Matt Tucker committed
1 2 3 4 5
/**
 * $RCSfile$
 * $Revision$
 * $Date$
 *
6
 * Copyright (C) 2004 Jive Software. All rights reserved.
Matt Tucker's avatar
Matt Tucker committed
7
 *
8 9
 * This software is published under the terms of the GNU Public License (GPL),
 * a copy of which is included in this distribution.
Matt Tucker's avatar
Matt Tucker committed
10
 */
11

Matt Tucker's avatar
Matt Tucker committed
12 13 14 15 16 17 18 19 20 21
package org.jivesoftware.net.spi;

import org.jivesoftware.net.Monitor;
import org.jivesoftware.util.CircularLinkedList;

import java.util.Date;
import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;

/**
22
 * Implements a transient (in-memory) generic monitor.
Matt Tucker's avatar
Matt Tucker committed
23 24 25 26 27
 *
 * @author Iain Shigeoka
 */
public class BasicTransientMonitor implements Monitor {

28 29
    private static final int DEFAULT_FRAME_SIZE = 20;

Matt Tucker's avatar
Matt Tucker committed
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219
    private long totalSamples;
    private long totalTime;
    private long lastSampleTime = -1;
    private long firstSampleTime = -1;
    private Date startUpTime;
    private int frameSize;
    private CircularLinkedList frameList = new CircularLinkedList();
    private ReadWriteLock frameLock = new ReentrantReadWriteLock();

    public BasicTransientMonitor(){
        startUpTime = new Date();
        frameSize = DEFAULT_FRAME_SIZE;
    }

    public void addSample(long quantity, Date startTime, Date endTime) {
        addSample(quantity,startTime.getTime(),endTime.getTime());
    }

    public void addSample(long quantity) {
        if (lastSampleTime < 0){
            lastSampleTime = startUpTime.getTime();
        }
        addSample(quantity,lastSampleTime,System.currentTimeMillis());
    }

    private void addSample(long quantity, long startTime, long endTime){
        totalSamples += quantity;
        totalTime += endTime - startTime;
        lastSampleTime = endTime;
        if (firstSampleTime == -1){
            firstSampleTime = startTime;
        }
        frameLock.writeLock().lock();
        try {
            Sample sample = new Sample(quantity, endTime - startTime);
            if (frameList.size() < frameSize){
                frameList.add(sample);
            } else {
                // overwrite oldest sample
                // the newest sample is at next() and the oldest at prev()
                // so move the pointer to one back using prev() then set that
                // value to be the youngest sample so the samples are again
                // in their natural order.
                frameList.prev();
                frameList.setNext(sample);
            }
        } finally {
            frameLock.writeLock().unlock();
        }
    }

    public long getTotal() {
        return totalSamples;
    }

    public long getTotalTime() {
        return totalTime;
    }

    public float getRate() {
        return (float)totalSamples / (float)(totalTime * 1000);
    }

    public Date getFirstSampleDate() {
        Date time = startUpTime;
        if (firstSampleTime > 0){
            time = new Date(firstSampleTime);
        }
        return time;
    }

    public Date getLastSampleDate() {
        Date time = null;
        if (lastSampleTime > 0){
            time = new Date(lastSampleTime);
        } else {
            time = startUpTime;
        }
        return time;
    }

    public int getFrameSize() {
        return frameSize;
    }

    public void setFrameSize(int newSize) {
        frameLock.writeLock().lock();
        try {
            while (frameList.size() > newSize){
                frameList.prev();
                frameList.remove();
            }
        } finally {
            frameLock.writeLock().unlock();
        }
        frameSize = newSize;
    }

    public long getFrameTotal() {
        long quantity = 0;
        if (frameList.size() > 0){
            frameLock.writeLock().lock();
            try {
                frameList.mark();
                while (frameList.getPassCount() == 0){
                    Sample sample = (Sample) frameList.next();
                    quantity += sample.getQuantity();
                }
            } finally {
                frameLock.writeLock().lock();
            }
        }
        return quantity;
    }

    public long getFrameTotalTime() {
        long time = 0;
        if (frameList.size() > 0){
            frameLock.writeLock().lock();
            try {
                frameList.mark();
                while (frameList.getPassCount() == 0){
                    Sample sample = (Sample) frameList.next();
                    time += sample.getTime();
                }
            } finally {
                frameLock.writeLock().lock();
            }
        }
        return time;
    }

    public float getFrameRate() {
        float time = 0;
        float quantity = 0;
        if (frameList.size() > 0){
            frameLock.writeLock().lock();
            try {
                frameList.mark();
                while (frameList.getPassCount() == 0){
                    Sample sample = (Sample) frameList.next();
                    time += sample.getTime();
                    quantity += sample.getQuantity();
                }
            } finally {
                frameLock.writeLock().lock();
            }
        }
        return quantity / time * 1000;
    }

    /**
     * <p>Represents a single sample event for use in the frame circular list.</p>
     *
     * @author Iain Shigeoka
     */
    private class Sample{
        private long q;
        private long t;

        /**
         * <p>Create a sample with given quantity and time duration.</p>
         *
         * @param quantity The quantity of the sample
         * @param time The time in milliseconds the sample took
         */
        Sample(long quantity, long time){
            q = quantity;
            t = time;
        }

        /**
         * <p>Returns the quantity of the sample.</p>
         *
         * @return Quantity of the sample
         */
        long getQuantity(){
            return q;
        }

        /**
         * <p>Returns the total time of the sample.</p>
         *
         * @return Time of the sample in milliseconds
         */
        long getTime(){
            return t;
        }
    }
}