patterntesting.runtime.monitor.SimpleProfileMonitor Maven / Gradle / Ivy
/*
* $Id: SimpleProfileMonitor.java,v 1.10 2014/04/28 17:36:37 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.aspectj.lang.Signature;
import org.slf4j.*;
import patterntesting.runtime.util.SignatureHelper;
/**
* The Class SimpleProfileMonitor.
*
* @author oliver
* @since 19.12.2008
* @version $Revision: 1.10 $
*/
public final class SimpleProfileMonitor extends AbstractProfileMonitor {
private static final Logger log = LoggerFactory.getLogger(SimpleProfileMonitor.class);
private SimpleProfileMonitor parent;
private Collection childs;
private final String 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("root");
}
/**
* Instantiates a new simple profile monitor.
*
* @param rootLabel the root label
*/
public SimpleProfileMonitor(final String rootLabel) {
this.reset();
this.label = rootLabel;
}
/**
* Reset.
*
* @see 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(final Signature label, final SimpleProfileMonitor parent) {
this(SignatureHelper.getAsString(label), parent);
}
/**
* Instantiates a new simple profile monitor.
*
* @param label the label
* @param parent the parent
*/
public SimpleProfileMonitor(final String label, final SimpleProfileMonitor parent) {
this.reset();
this.label = label;
this.parent = parent;
this.parent.addChild(this);
}
/**
* Adds the child.
*
* @param child the child
*/
protected void addChild(final SimpleProfileMonitor child) {
this.childs.add(child);
}
/**
* Adds the children.
*
* @param labels the labels
*/
public void addChildren(final List labels) {
for (String lbl : labels) {
addChild(lbl);
}
}
/**
* Adds the child.
*
* @param lbl the label
*/
public void addChild(final String lbl) {
try {
Signature sig = SignatureHelper.getAsSignature(lbl);
SimpleProfileMonitor child = new SimpleProfileMonitor(sig, this);
addChild(child);
} catch (Exception e) {
log.debug("can't add " + lbl, e);
}
}
/**
* Gets the monitors.
*
* @return the monitors
* @see ProfileMonitor#getMonitors()
*/
public ProfileMonitor[] getMonitors() {
return 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(final Signature sig) {
String lbl = SignatureHelper.getAsString(sig);
return getMonitor(lbl);
}
/**
* Gets the monitor.
*
* @param lbl the label as String
* @return the monitor
* @since 1.4.2
*/
public SimpleProfileMonitor getMonitor(final String lbl) {
for (SimpleProfileMonitor mon : childs) {
if (lbl.equals(mon.label)) {
return mon;
}
}
return null;
}
/**
* Start.
*
* @see ProfileMonitor#start()
*/
public void start() {
this.startTime = System.nanoTime();
}
/**
* Stop.
*
* @see ProfileMonitor#stop()
*/
public void stop() {
long time = System.nanoTime() - startTime;
this.add(time / 1000000.0);
}
/**
* Adds the.
*
* @param value the measured time
* @see ProfileMonitor#add(double)
*/
public void add(final 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;
}
}
/**
* Gets the total.
*
* @return the total
* @see ProfileMonitor#getTotal()
*/
public double getTotal() {
return this.total;
}
/**
* Gets the last value.
*
* @return the last value
* @see ProfileMonitor#getLastValue()
*/
public double getLastValue() {
return this.lastValue;
}
/**
* Gets the max.
*
* @return the max
* @see ProfileMonitor#getMax()
*/
public double getMax() {
return this.max;
}
/**
* Gets the min.
*
* @return the min
* @see ProfileMonitor#getMin()
*/
public double getMin() {
return this.min;
}
/**
* Gets the hits.
*
* @return the hits
* @see ProfileMonitor#getHits()
*/
public int getHits() {
return this.hits;
}
/**
* Gets the avg.
*
* @return the avg
* @see ProfileMonitor#getAvg()
*/
public double getAvg() {
return this.total / this.hits;
}
/**
* Gets the label.
*
* @return the label
* @see ProfileMonitor#getLabel()
*/
public String getLabel() {
return this.label;
}
/**
* To string.
*
* @return the string
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
return this.getLabel() + " - " + toShortString();
}
/**
* To short string.
*
* @return the string
* @see ProfileMonitor#toShortString()
*/
public String toShortString() {
return "total: " + this.total + " ms / avg: "
+ this.getAvg() + " ms / hits: " + this.hits;
}
/**
* To csv headline.
*
* @return the string
* @see ProfileMonitor#toCsvHeadline()
*/
public String toCsvHeadline() {
return "Label; Unit; Total; Avg; Hits; Max; Min";
}
/**
* To csv string.
*
* @return the string
* @see ProfileMonitor#toCsvString()
*/
public String toCsvString() {
return '"' + this.getLabel() + "\"; ms; " + this.total + "; "
+ this.getAvg() + "; " + this.hits + "; " + this.getMax() + "; "
+ this.getMin();
}
/**
* Gets the units.
*
* @return the units
* @see ProfileMonitor#getUnits()
*/
public String getUnits() {
return "ms";
}
/**
* Gets the active.
*
* @return the active
* @see ProfileMonitor#getActive()
*/
public double getActive() {
return 0.0;
}
/**
* Gets the avg active.
*
* @return the avg active
* @see ProfileMonitor#getAvgActive()
*/
public double getAvgActive() {
return 0.0;
}
/**
* Gets the first access.
*
* @return the first access
* @see ProfileMonitor#getFirstAccess()
*/
public Date getFirstAccess() {
return null;
}
/**
* Gets the last access.
*
* @return the last access
* @see ProfileMonitor#getLastAccess()
*/
public Date getLastAccess() {
return null;
}
/**
* Gets the max active.
*
* @return the max active
* @see ProfileMonitor#getMaxActive()
*/
public double getMaxActive() {
return 0.0;
}
}