package net.bull.javamelody;

import net.bull.javamelody.internal.model.Counter;

/**
 * Stopwatch to record execution times of a piece of code in the services statistics.
 * (To monitor whole methods, see MonitoringSpringInterceptor, MonitoringInterceptor or MonitoringProxy.)
 * @author Emeric Vernat
 */
public class Stopwatch implements AutoCloseable {
	private static final Counter SERVICES_COUNTER = MonitoringProxy.getServicesCounter();

	private final String name;
	private final long startTime;
	private long duration;
	private boolean closed;

	/**
	 * Starts a stopwatch (must always be used in try-with-resource):
	 * <pre>
	 * try (Stopwatch stopwatch = new Stopwatch("nameyouwant")) {
	 *     // your code
	 * }
	 * </pre>
	 * @param stopwatchName Whatever name you want to display in the statistics
	 */
	public Stopwatch(String stopwatchName) {
		super();
		SERVICES_COUNTER.bindContextIncludingCpu(stopwatchName);
		this.startTime = System.currentTimeMillis();
		this.name = stopwatchName;
	}

	/**
	 * @return name of the stopwatch.
	 */
	public String getName() {
		return name;
	}

	/**
	 * @return duration until now or until stop of the stopwatch.
	 */
	public long getDuration() {
		if (closed) {
			return duration;
		}
		return System.currentTimeMillis() - startTime;
	}

	/**
	 * @return already stopped ?
	 */
	public boolean isClosed() {
		return closed;
	}

	/**
	 * Stops the stopwatch.
	 */
	@Override
	public void close() {
		if (closed) {
			throw new IllegalStateException("Stopwatch already closed");
		}
		SERVICES_COUNTER.addRequestForCurrentContext(false);
		duration = getDuration();
		closed = true;
	}
}

@@ -23,16 +23,71 @@
 elody;%0A%0A
+import net.bull.javamelody.internal.common.Parameters;%0A
 import n
@@ -129,16 +129,16 @@
 ounter;%0A
-
 %0A/**%0A * 
@@ -491,16 +491,123 @@
 unter();
+%0A%09private static final boolean COUNTER_HIDDEN = Parameters%0A%09%09%09.isCounterHidden(SERVICES_COUNTER.getName());
 %0A%0A%09priva
@@ -1016,16 +1016,16 @@
 Name) %7B%0A
-
 %09%09super(
@@ -1027,16 +1027,100 @@
 uper();%0A
+%09%09SERVICES_COUNTER.setDisplayed(!COUNTER_HIDDEN);%0A%09%09SERVICES_COUNTER.setUsed(true);%0A
 %09%09SERVIC

fix #876 StopWatch not displayed in statistics by default