Java Performance Util

I have not finished or tested the following two classes, I provide freely for helping with Performance Testing Java

package performance;

import java.util.logging.Logger;

public class PerformanceTests {
	private static Logger logger = Logger.getLogger(PerformanceTests.class.getName());
	private static PerformanceTests instance;
	
	public static PerformanceTests getInstance() {
		if (null == instance) {
			instance = new PerformanceTests();
		}
		
		return instance;
	}
	
	public static void addTwoOneDigitNumbers_OneBillionTimes() {
		logger.info("Performance Test, Adding Two One Digit Numbers 1 billion times");
		int sum;
		PerformanceUtil.getInstance().start();
		for (int i=0; i<200000000; i++) {
			for (int j=0; j<5; j++) {
				sum = j+j;
			}
			if (i==50000000) {
				logger.info("--- Processed 250,000,000 additions, current running time: "
						+PerformanceUtil.getInstance().getRunningTime());
			}
			if (i==100000000) {
				logger.info("--- Processed 500,000,000 additions, current running time: "
						+PerformanceUtil.getInstance().getRunningTime());
			}
			if (i==150000000) {
				logger.info("--- Processed 750,000,000 additions, current running time: "
						+PerformanceUtil.getInstance().getRunningTime());
			}
		}
		PerformanceUtil.getInstance().stop();
		logger.info("Test completed, recorded time in Nanoseconds: "
				+PerformanceUtil.getInstance().getRecordedTime());		
	}
	
	public static void addTwoOneDigitNumbers_OneBillionTimes_andCheckVerify_OneBillionTimes() {
		logger.info("Performance Test, Adding Two One Digit Numbers 1 billion times");
		int sum;
		int doubleTheValue;
		int doubleTheSum;
		int sumOfDoubled;
		PerformanceUtil.getInstance().start();
		for (int i=0; i<200000000; i++) {
			for (int j=0; j<5; j++) {
				sum = j+j;
				doubleTheValue = j*2;
				doubleTheSum = sum*2;
				sumOfDoubled = doubleTheValue+doubleTheValue;
				if (doubleTheSum!=sumOfDoubled) {
					PerformanceUtil.getInstance().stop();
					logger.severe("Numbers not verifying ending test at i="+i+",j="+j+" at recorded time: "
							+PerformanceUtil.getInstance().getRecordedTime());
					
				}
			}
			if (i==50000000) {
				logger.info("--- Processed 250,000,000 additions, current running time: "
						+PerformanceUtil.getInstance().getRunningTime());
			}
			if (i==100000000) {
				logger.info("--- Processed 500,000,000 additions, current running time: "
						+PerformanceUtil.getInstance().getRunningTime());
			}
			if (i==150000000) {
				logger.info("--- Processed 750,000,000 additions, current running time: "
						+PerformanceUtil.getInstance().getRunningTime());
			}
		}
		PerformanceUtil.getInstance().stop();
		logger.info("Test completed, recorded time in Nanoseconds: "
				+PerformanceUtil.getInstance().getRecordedTime());				
	}
}
package performance;

public class PerformanceUtil {
	private boolean stopWatchStarted = false;
	private long startTime;
	private long endTime;
	private long recordedTime=0;
	private static PerformanceUtil instance;
	
	public static PerformanceUtil getInstance() {
		if (null == instance) {
			instance = new PerformanceUtil();
		}
		
		return instance;
	}
	
	public boolean start() {
		if (stopWatchStarted) {
			return false;
		} else {
			startTime=System.nanoTime();
			stopWatchStarted=true;
			return true;
		}
	}
	
	public boolean stop() {
		if (!stopWatchStarted) {
			return false;
		} else {
			endTime=System.nanoTime();
			stopWatchStarted=false;
			return true;
		}
	}
	
	public long getRunningTime() {
		long runningTime = System.nanoTime()-startTime;
		return runningTime;
	}
	
	public long getRecordedTime() {
		recordedTime=endTime-startTime;
		return recordedTime;
	}
}

Able to Performance Test sometimes an after thought, Realization that verification factors into Performance and Throughput sometimes an after thought.

Published by techinfodebug

Flex and Java Developer, Christian, Art, Music, Video, and Vlogging

Leave a comment