Archive

Archive for the ‘Performance Tuning’ Category

Java Memory Monitoring

March 1, 2011 Leave a comment

Check the following code. See the graph below.


public class MemTest {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		System.out.println("Memory Test");

		long longTest = 1;
		String sb = null;
		for (long lCount = 1; lCount < Long.MAX_VALUE; lCount++) {

			if (longTest <= 0) 	
			longTest = 1; 			
                        
                       longTest = longTest * lCount; 			
                       sb = "\n" + sb + "MemoryTest loop==>" + lCount + " long==>"
					+ longTest;
			System.out.println("lCount==>" + lCount);
		}

	}

}

System will hang in next couple of minutes…if the memory consumption continuous like this.

Now the code is improved….


public class MemTest {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		System.out.println("Memory Test");

		long longTest = 1;
		String sb = null;
		for (long lCount = 1; lCount < Long.MAX_VALUE; lCount++) {

			if (longTest <= 0) 				
                         longTest = 1; 			
                         
                          //Using this number to show difference in graph in beginning and later
                          if (lCount > 10000)
				sb = null;

			longTest = longTest * lCount;
			sb = "\n" + sb + "MemoryTest loop==>" + lCount + " long==>"
					+ longTest;
			System.out.println("lCount==>" + lCount);
		}

	}

}

Monitor the application CPU / Memory while running for couple of hours (Min 24 hours to 48 hours) before going to release (QA/Production).
If the line is going up …it is going to blow up……

Thank you,
Bhavani P Polimetla

Java – how to measure time in Java efficiently

February 9, 2011 Leave a comment

import java.util.Date;
import java.util.concurrent.TimeUnit;

/**
 * This program demonstrates how to measure time in Java efficiently.
 *
 * @author Bhavani P Polimetla
 * @since Feb-09-2011
 *
 */
public class TimeTest {

	/**
	 * @param args
	 */
	public static void main(String[] args) {

		// Test 1: Measure Time. (Can't measure methods which take very small
		// time in nano seconds.)
		long start = System.currentTimeMillis();
		workingHard();
		long end = System.currentTimeMillis();
		System.out.println("Test 1 Time==>" + (end - start) + " milli seconds");

		// Test 2: Measure Time. Date constructor uses
		// System.currentTimeMillis()
		// This is not good way
		start = (new Date()).getTime();
		workingHard();
		end = (new Date()).getTime();
		System.out.println("Test 2 Time==>" + (end - start) + " milli seconds");

		// Test 3: Measure Time in Nano seconds
		start = System.nanoTime();
		workingHard();
		end = System.nanoTime();
		System.out.println("Test 3 Time==>" + (end - start) + " nano seconds");

		// Conversion
		long duration = TimeUnit.NANOSECONDS.toMicros((end - start));
		System.out.println("Test 4 Time==>" + duration + " micro seconds");

	}

	public static void workingHard() {
		long k = 0;
		for (int i = 0; i < 1000; i++) { 			k = k + i; 		} 		System.out.println("I did big addition. : ) Final Total==>" + k);
	}

}

Result:
Test 1 Time==>0 milli seconds
I did big addition. : ) Final Total==>499500
Test 2 Time==>0 milli seconds
I did big addition. : ) Final Total==>499500
Test 3 Time==>462671 nano seconds
Test 4 Time==>462 micro seconds

JAMON API Setup – Notes

February 9, 2011 Leave a comment

http://jamonapi.sourceforge.net/

Download latest copy and install the jar file to maven repository
mvn install:install-file -Dfile=x:\download\jamon\jamonapi_2.7\jamon-2.7.jar -DgroupId=com.jamonapi -DartifactId=jamon -Dversion=2.7 -Dpackaging=jar -DgeneratePom=true

Add this dependency to pom file

<dependency>
	<groupId>com.jamonapi</groupId>
	<artifactId>jamon</artifactId>
	<version>2.7</version>
	<scope>provided</scope>
</dependency>

Add this to .java file where you want to measure the performance

import com.jamonapi.Monitor;
import com.jamonapi.MonitorFactory;

In method begenning add

Monitor mon = MonitorFactory.start("XYZ:Method Name");

In method ending add

mon.stop();

Identify place to get all above times and print them at the end of program
Print in .html file to see nice report.

String fileData = MonitorFactory.getReport();
Log.info("Performance Report==>"+fileData);

-end-

Java application XYZ failed UAT because of slow performance

August 11, 2010 Leave a comment

Problem Statement: Java application XYZ failed UAT because of slow performance.

Many times team neglects application performance until test team / client reports back.
Performance is one of the most important quality attributes.

Requirements Phase: Get the client expectations on application performance.
Design Phase: Design the application architecture for better performance.
Coding Phase: Use java profiling tools to measure and tune the code.
I used JAMON API and it helped me a lot. http://jamonapi.sourceforge.net/
At run time we can enable / disable the logging with the help of configuration.
Testing Phase: Success assured.

It is difficult to do the reverse steps. Just see the wrong path…
Testing Phase: Application failed load/performance test
Code: Go back and enable logs and start understanding who is taking more time
Design: Some thing wrong in design and need to be modified and code need to be realigned according to it.
Requirements: Some times users won’t understand the complexity of screens.
Example: One screen needs more than 5000 calls to external systems, databases, …etc. We need to educate the user to understand the complexity behind it. Also we need to move this type of work / reports to batch process. Or do the calculations before hand and serve the user when required.

It is lengthy subject and lots of books are available for further study.
Java Performance Tuning (2nd Edition) [Paperback]
Jack Shirazi (Author)
http://oreilly.com/catalog/9780596003777/

I purchased this book in year 2003 and still referring.

UAT: User Acceptance Test

-o-

Follow

Get every new post delivered to your Inbox.