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

com.alterioncorp.perfjdbc.StopWatch Maven / Gradle / Ivy

Go to download

Wrapper JDBC driver that monitors time spent inside JDBC code by thread. JDBC driver URL: jdbc:alterion:perf://class=<TARGET_DRIVER_CLASS>|url=<TARGET_JDBC_URL> To get the time in millis spent inside JDBC for thread: StopWatch.getTime()

The newest version!
package com.alterioncorp.perfjdbc;

public class StopWatch {
	
	private static final ThreadLocal threadLocal = new ThreadLocal();

	public static void start() {
		StopWatch stopWatch = threadLocal.get();
		if (stopWatch == null) {
			stopWatch = new StopWatch();
			threadLocal.set(stopWatch);
		}
		stopWatch.startTiming();
	}

	public static void stop() {
		StopWatch stopWatch = threadLocal.get();
		if (stopWatch == null) {
			throw new IllegalStateException("not started");
		}
		stopWatch.stopTiming();
	}
	
	public static void reset() {
		threadLocal.set(null);
	}
	
	public static long getTime() {
		long time = 0;
		StopWatch stopWatch = threadLocal.get();
		if (stopWatch != null) {
			time = stopWatch.totalTime;
		}
		return time;
	}
	
	public static boolean isStarted() {
		StopWatch stopWatch = threadLocal.get();
		return stopWatch == null ? false : stopWatch.started;
	}

	public static boolean hasBeenCalled() {
		return threadLocal.get() != null;
	}
	
	private boolean started;
	private long totalTime;
	private long lastMeasurementStartTime;

	private StopWatch() {
	}

	private void startTiming() {
		if (started) {
			throw new IllegalStateException("already started");
		}
		started = true;
		lastMeasurementStartTime = System.currentTimeMillis();
	}
	
	private void stopTiming() {
		if (! started) {
			throw new IllegalStateException("not started");
		}
		started = false;
		totalTime = totalTime + (System.currentTimeMillis() - lastMeasurementStartTime);
		lastMeasurementStartTime = 0;
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy