All Downloads are FREE. Search and download functionalities are using the official Maven repository.

Alachisoft.NCache.Common.Stats.HPTimeStats Maven / Gradle / Ivy

There is a newer version: 5.3.3
Show newest version
package Alachisoft.NCache.Common.Stats;

import com.alachisoft.ncache.serialization.core.io.ICompactSerializable;
import com.alachisoft.ncache.serialization.core.io.NCacheObjectInput;
import com.alachisoft.ncache.serialization.core.io.NCacheObjectOutput;

import java.io.IOException;

/**
 * Class that is useful in capturing statistics. It uses High performnace counters for the measurement of the time.
 */
public class HPTimeStats implements ICompactSerializable {

    private static double _frequency;

    static {
//C# TO JAVA CONVERTER TODO TASK: There is no preprocessor in Java:
//#if !MONO
//        long freq = 0;
//        tangible.RefObject tempRef_freq = new tangible.RefObject(freq);
//        Win32.QueryPerformanceFrequency(tempRef_freq);
//        freq = tempRef_freq.argvalue;
//        _frequency = (double) freq / (double) 1000;
//#else
        //@UH
//#endif
    }

    /**
     * Total number of samples collected for the statistics.
     */
    private long _runCount;
    /**
     * Timestamp for the begining of a sample.
     */
    private long _lastStart;
    /**
     * Timestamp for the end of a sample.
     */
    private long _lastStop;
    /**
     * Total time spent in sampling, i.e., acrued sample time.
     */
    private double _totalTime;
    /**
     * Best time interval mesaured during sampling.
     */
    private double _bestTime;
    /**
     * Worst time interval mesaured during sampling.
     */
    private double _worstTime;
    /**
     * Avg. time interval mesaured during sampling.
     */
    private double _avgTime;
    /**
     * Total number of samples collected for the statistics.
     */
    private long _totalRunCount;
    private float _avgCummulativeOperations;
    private double _worstThreshHole = Double.MAX_VALUE;
    private long _worstOccurance;

    /**
     * Constructor
     */
    public HPTimeStats() {

        Reset();
    }

    /**
     * Constructor
     */
    public HPTimeStats(double worstThreshHoleValue) {
        Reset();
        _worstThreshHole = worstThreshHoleValue;
    }

    /**
     * Returns the total numbre of runs in the statistics capture.
     */
    public final long getRuns() {
        synchronized (this) {
            return _runCount;
        }
    }

    public final void setRuns(long value) {
        synchronized (this) {
            _runCount = value;
        }
    }

    /**
     * Gets or sets the threshhold value for worst case occurance count.
     */
    public final double getWorstThreshHoldValue() {
        return _worstThreshHole;
    }

    public final void setWorstThreshHoldValue(double value) {
        _worstThreshHole = value;
    }

    /**
     * Gets the number of total worst cases occured.
     */
    public final long getTotalWorstCases() {
        return _worstOccurance;
    }

    /**
     * Returns the total time iterval spent in sampling
     */
    public final double getTotal() {
        synchronized (this) {
            return _totalTime;
        }
    }

    /**
     * Returns the time interval for the last sample
     */
    public final double getCurrent() {
        synchronized (this) {
            return (double) (_lastStop - _lastStart);
        }
    }

    /**
     * Returns the best time interval mesaured during sampling
     */
    public final double getBest() {
        synchronized (this) {
            return _bestTime;
        }
    }
    //set { lock(this){ _bestTime = value; } }

    /**
     * Returns the avg. time interval mesaured during sampling
     */
    public final double getAvg() {
        synchronized (this) {
            return _avgTime;
        }
    }
    //set { lock(this){ _avgTime = value; } }

    public final float getAvgOperations() {
        synchronized (this) {
            return _avgCummulativeOperations;
        }
    }

    /**
     * Returns the worst time interval mesaured during sampling
     */
    public final double getWorst() {
        synchronized (this) {
            return _worstTime;
        }
    }
    //set { lock(this){ _worstTime = value; } }

    /**
     * Resets the statistics collected so far.
     */
    public final void Reset() {
        _runCount = 0;
        //_lastStart = _lastStop = 0;
        _totalTime = _bestTime = _worstTime = _worstOccurance = 0;
        _avgTime = 0;
        _avgCummulativeOperations = 0;
    }

    /**
     * Timestamps the start of a sampling interval.
     */
    public final void BeginSample() {
//C# TO JAVA CONVERTER TODO TASK: There is no preprocessor in Java:
//#if !MONO
        //_lastStart = (DateTime.Now.Ticks - 621355968000000000) / 10000;
//        tangible.RefObject tempRef__lastStart = new tangible.RefObject(_lastStart);
//        Win32.QueryPerformanceCounter(tempRef__lastStart);
//        _lastStart = tempRef__lastStart.argvalue;
        //Basit: Need to change
        _lastStart = System.currentTimeMillis();
        _lastStart = _lastStart;
//#else
        //@UH
//#endif
    }

    /**
     * Timestamps the end of interval and calculates the sample time
     */
    public final void EndSample() {
//C# TO JAVA CONVERTER TODO TASK: There is no preprocessor in Java:
//#if !MONO
        synchronized (this) {
            //_lastStop = (DateTime.Now.Ticks - 621355968000000000) / 10000;
//            tangible.RefObject tempRef__lastStop = new tangible.RefObject(_lastStop);
//            Win32.QueryPerformanceCounter(tempRef__lastStop);
//            _lastStop = tempRef__lastStop.argvalue;
            //Basit: Need to change
            _lastStop = System.currentTimeMillis();
            _lastStop = _lastStop;
            AddSampleTime(getCurrent());
        }
//#else
        //@UH
//#endif
    }

    /**
     * Timestamps the end of interval and calculates the sample time
     */
    public final void EndSample(int runcount) {
//C# TO JAVA CONVERTER TODO TASK: There is no preprocessor in Java:
//#if !MONO
        synchronized (this) {
            //_lastStop = (DateTime.Now.Ticks - 621355968000000000) / 10000;
//            tangible.RefObject tempRef__lastStop = new tangible.RefObject(_lastStop);
//            Win32.QueryPerformanceCounter(tempRef__lastStop);
//            _lastStop = tempRef__lastStop.argvalue;
            //Basit: Need to change
            _lastStop = System.currentTimeMillis();
            _lastStop = _lastStop;
            AddSampleTime(getCurrent(), runcount);
        }
//#else
        //@UH
//#endif
    }

    /**
     * Adds a specified sample time to the statistics and updates the run count
     *
     * @param time sample time in milliseconds.
     */
    public final void AddSampleTime(double time) {
        synchronized (this) {

            _runCount++;
            _totalRunCount++;

            if (_runCount == 1) {
                _avgTime = _totalTime = _bestTime = _worstTime = time;
            } else {
                _totalTime += time;
                if (time < _bestTime) {
                    _bestTime = time;
                }
                if (time > _worstTime) {
                    _worstTime = time;
                }
                if (time > _worstThreshHole) {
                    _worstOccurance += 1;
                }
                _avgTime = (double) _totalTime / (double) _runCount;
            }


            if (_totalTime < 1000) {
                _avgCummulativeOperations = _runCount;
            } else {
                _avgCummulativeOperations = (float) _runCount * 1000 / (float) _totalTime;
            }

        }
    }

    /**
     * Adds a specified sample time to the statistics and updates the run count
     *
     * @param time sample time in milliseconds.
     */
    public final void AddSampleTime(double time, int runcount) {
        synchronized (this) {

            _runCount += runcount;
            _totalRunCount += runcount;

            if (_runCount == 1) {
                _avgTime = _totalTime = _bestTime = _worstTime = time;
            } else {
                _totalTime += time;
                if (time < _bestTime) {
                    _bestTime = time;
                }
                if (time > _worstTime) {
                    _worstTime = time;
                }
                if (time > _worstThreshHole) {
                    _worstOccurance += 1;
                }
                _avgTime = (float) _totalTime / (float) _runCount;
            }

            if (_totalTime < 1000) {
                _avgCummulativeOperations = _runCount;
            } else {
                _avgCummulativeOperations = (float) _runCount * 1000 / (float) _totalTime;
            }
        }
    }

    /**
     * Gets the total run count for the samples
     */
    public final long getTotalRunCount() {
        return _totalRunCount;
    }

    /**
     * Override converts to string equivalent.
     *
     * @return
     */
    @Override
    public String toString() {
        synchronized (this) {
            String retval = "[Runs: " + _runCount + ", ";
            retval += "Best(ms): " + _bestTime + ", ";
            retval += "Avg.(ms): " + _avgTime + ", ";
            retval += "Worst(ms): " + _worstTime + ", ";
            retval += "WorstThreshHole(ms): " + _worstThreshHole + ", ";
            retval += "Worst cases: " + _worstOccurance + "]";

            return retval;
        }
    }

    //C# TO JAVA CONVERTER TODO TASK: There is no preprocessor in Java:
    ///#region ICompactSerializable Members
    public final void deserialize(NCacheObjectInput reader) throws IOException {
        _runCount = reader.readLong();
        _avgTime = reader.readDouble();
        _bestTime = reader.readDouble();
        _lastStart = reader.readLong();
        _lastStop = reader.readLong();
        _worstThreshHole = reader.readDouble();
        _worstTime = reader.readDouble();
        _totalRunCount = reader.readLong();
        _totalTime = reader.readDouble();
        _worstOccurance = reader.readLong();
        _avgCummulativeOperations = reader.readFloat();
    }

    public final void serialize(NCacheObjectOutput writer) throws IOException {
        writer.writeLong(_runCount);
        writer.writeDouble(_avgTime);
        writer.writeDouble(_bestTime);
        writer.writeLong(_lastStart);
        writer.writeLong(_lastStop);
        writer.writeDouble(_worstThreshHole);
        writer.writeDouble(_worstTime);
        writer.writeLong(_totalRunCount);
        writer.writeDouble(_totalTime);
        writer.writeLong(_worstOccurance);
        writer.writeFloat(_avgCummulativeOperations);

    }
//C# TO JAVA CONVERTER TODO TASK: There is no preprocessor in Java:
    ///#endregion
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy