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

patterntesting.runtime.monitor.SimpleProfileMonitor Maven / Gradle / Ivy

Go to download

PatternTesting Runtime (patterntesting-rt) is the runtime component for the PatternTesting framework. It provides the annotations and base classes for the PatternTesting testing framework (e.g. patterntesting-check, patterntesting-concurrent or patterntesting-exception) but can be also used standalone for classpath monitoring or profiling. It uses AOP and AspectJ to perform this feat.

The newest version!
/*
 * $Id: SimpleProfileMonitor.java,v 1.20 2009/12/28 10:07:23 oboehm Exp $
 *
 * Copyright (c) 2008 by Oliver Boehm
 *
 * 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 orimplied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * (c)reated 19.12.2008 by oliver ([email protected])
 */
package patterntesting.runtime.monitor;

import java.util.*;
import java.util.concurrent.ConcurrentLinkedQueue;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.impl.LogFactoryImpl;
import org.aspectj.lang.Signature;

import patterntesting.runtime.util.SignatureHelper;

/**
 * The Class SimpleProfileMonitor.
 *
 * @author oliver
 * @since 19.12.2008
 * @version $Revision: 1.20 $
 */
public final class SimpleProfileMonitor extends AbstractProfileMonitor {

    private static final Log log = LogFactoryImpl.getLog(SimpleProfileMonitor.class);
    private SimpleProfileMonitor parent;
    private Collection childs;
    private final Signature label;
    /** start time in nanoseconds */
    private long startTime;
    /** the measured time in milliseconds */
    private double total;
    private double lastValue;
    private double min;
    private double max;
    /** number of calls (or "hits") */
    private int hits;

    /**
     * Instantiates a new simple profile monitor.
     */
    public SimpleProfileMonitor() {
        this.reset();
        this.label = null;
    }

    /**
     * @see patterntesting.runtime.monitor.ProfileMonitor#reset()
     */
    public void reset() {
        this.total = 0.0;
        this.lastValue = 0.0;
        this.min = Double.MAX_VALUE;
        this.max = 0.0;
        this.hits = 0;
        this.parent = null;
        this.childs = new ConcurrentLinkedQueue();
    }

    /**
     * Instantiates a new simple profile monitor.
     *
     * @param label the label
     * @param parent the parent
     */
    public SimpleProfileMonitor(Signature label, SimpleProfileMonitor parent) {
        this.reset();
        this.label = label;
        this.parent = parent;
        this.parent.addChild(this);
    }

    /**
     * Adds the child.
     *
     * @param child the child
     */
    protected void addChild(SimpleProfileMonitor child) {
        this.childs.add(child);
    }

    /**
     * Adds the children.
     *
     * @param labels the labels
     */
    public void addChildren(List labels) {
        for (String label : labels) {
            addChild(label);
        }
    }

    /**
     * Adds the child.
     *
     * @param label the label
     */
    public void addChild(String label) {
        try {
            Signature sig = SignatureHelper.getAsSignature(label);
            SimpleProfileMonitor child = new SimpleProfileMonitor(sig, this);
            addChild(child);
        } catch (Exception e) {
            log.debug("can't add " + label, e);
        }
    }

    /**
     * @see patterntesting.runtime.monitor.ProfileMonitor#getMonitors()
     */
    public ProfileMonitor[] getMonitors() {
        return (ProfileMonitor[]) childs.toArray(new SimpleProfileMonitor[childs
                .size()]);
    }

    /**
     * We use now the signature without the return value. That's enough.
     *
     * @param sig the sig
     *
     * @return the monitor for sig
     */
    public SimpleProfileMonitor getMonitor(Signature sig) {
        String label = SignatureHelper.getAsString(sig);
        for (SimpleProfileMonitor mon : childs) {
            String monLabel = SignatureHelper.getAsString(mon.label);
            if (label.equals(monLabel)) {
                return mon;
            }
        }
        return null;
    }

    /**
     * @see patterntesting.runtime.monitor.ProfileMonitor#start()
     */
    public void start() {
        this.startTime = System.nanoTime();
    }

    /**
     * @see patterntesting.runtime.monitor.ProfileMonitor#stop()
     */
    public void stop() {
        long time = System.nanoTime() - startTime;
        this.add(time / 1000000.0);
    }

    /**
     * @see patterntesting.runtime.monitor.ProfileMonitor#add(double)
     */
    public void add(double value) {
        this.lastValue = value;
        this.total += value;
        this.hits++;
        if (this.parent != null) {
            this.parent.add(value);
        }
        if (value < this.min) {
            this.min = value;
        }
        if (value > this.max) {
            this.max = value;
        }
    }

    /**
     * @see patterntesting.runtime.monitor.ProfileMonitor#getTotal()
     */
    public double getTotal() {
        return this.total;
    }

    /**
     * @see patterntesting.runtime.monitor.ProfileMonitor#getLastValue()
     */
    public double getLastValue() {
        return this.lastValue;
    }

    /**
     * @see patterntesting.runtime.monitor.ProfileMonitor#getMax()
     */
    public double getMax() {
        return this.max;
    }

    /**
     * @see patterntesting.runtime.monitor.ProfileMonitor#getMin()
     */
    public double getMin() {
        return this.min;
    }

    /**
     * @see patterntesting.runtime.monitor.ProfileMonitor#getHits()
     */
    public int getHits() {
        return this.hits;
    }

    /**
     * @see patterntesting.runtime.monitor.ProfileMonitor#getAvg()
     */
    public double getAvg() {
        return this.total / this.hits;
    }

    /**
     * @see patterntesting.runtime.monitor.ProfileMonitor#getLabel()
     */
    public String getLabel() {
        if (this.label == null) {
            return "root";
        } else {
            return SignatureHelper.getAsString(label);
        }
    }

    /**
     * @see java.lang.Object#toString()
     */
    @Override
    public String toString() {
        return this.getLabel() + " - " + toShortString();
    }

    /**
     * @see patterntesting.runtime.monitor.ProfileMonitor#toShortString()
     */
    public String toShortString() {
        return "total: " + this.total + " ms / avg: "
        + this.getAvg() + " ms / hits: " + this.hits;
    }

    /**
     * @see patterntesting.runtime.monitor.ProfileMonitor#toCsvHeadline()
     */
    public String toCsvHeadline() {
        return "Label; Unit; Total; Avg; Hits; Max; Min";
    }

    /**
     * @see patterntesting.runtime.monitor.ProfileMonitor#toCsvString()
     */
    public String toCsvString() {
		return this.getLabel() + "; ms; " + this.total + "; "
				+ this.getAvg() + "; " + this.hits + "; " + this.getMax() + ";"
				+ this.getMin();
	}

    /**
     * @see patterntesting.runtime.monitor.ProfileMonitor#getUnits()
     */
    public String getUnits() {
        return "ms";
    }

    /**
     * @see patterntesting.runtime.monitor.ProfileMonitor#getActive()
     */
    public double getActive() {
        return 0.0;
    }

    /**
     * @see patterntesting.runtime.monitor.ProfileMonitor#getAvgActive()
     */
    public double getAvgActive() {
        return 0.0;
    }

    /**
     * @see patterntesting.runtime.monitor.ProfileMonitor#getFirstAccess()
     */
    public Date getFirstAccess() {
        return null;
    }

    /**
     * @see patterntesting.runtime.monitor.ProfileMonitor#getLastAccess()
     */
    public Date getLastAccess() {
        return null;
    }

    /**
     * @see patterntesting.runtime.monitor.ProfileMonitor#getMaxActive()
     */
    public double getMaxActive() {
        return 0.0;
    }

}

/**
 * $Log: SimpleProfileMonitor.java,v $
 * Revision 1.20  2009/12/28 10:07:23  oboehm
 * missing Javadocs completed
 *
 * Revision 1.19  2009/12/19 22:34:09  oboehm
 * trailing spaces removed
 *
 * Revision 1.18  2009/09/25 14:49:43  oboehm
 * javadocs completed with the help of JAutodoc
 *
 * Revision 1.17  2009/09/18 13:54:52  oboehm
 * javadoc warnings fixed
 *
 * Revision 1.16  2009/04/09 18:30:47  oboehm
 * no longer double entries (bug 2745302 fixed)
 *
 * Revision 1.15  2009/04/03 21:30:09  oboehm
 * with (Abstract)ProfileAspect it is now possible
 * to find methods/constructors which are never called
 *
 * Revision 1.14  2009/03/30 06:59:45  oboehm
 * 2719516 fixed
 *
 * $Source: /cvsroot/patterntesting/PatternTesting08/patterntesting-rt/src/main/java/patterntesting/runtime/monitor/SimpleProfileMonitor.java,v $
 */




© 2015 - 2025 Weber Informatics LLC | Privacy Policy