Total method time in Java VisualVM

JavaProfilingVisualvm

Java Problem Overview


In Java VisualVM, is there any way to display total method time, rather than "self time"? (The latter is not particularly useful, since it doesn't tell you anything about how much time methods actually take to run.)

If not, is there any standalone free Java profiler that does calculate total method time?

Java Solutions


Solution 1 - Java

Looking at the trace data in a "snapshot" view allows you to see the total as well as the self time.

Press the "snapshot" button that appears about the table of results. This will create a new tab that contains a "Call Tree" view which breaks down the self vs. total time. The "combined" view also provides this information, but splits the screen space with a "Hot Spots" view that is similar to the standard profiling view.

Snapshots can be created from either standard "Profiler" or "Sampler" data. However, "Profiler" snapshots can only be created before the application is closed, while "Sampler" ones can be created at any time.

(The above information is based on VisualVM 1.3.1)

Solution 2 - Java

Just take a snapshot of the profiling results. You will get the wall-clock time as well as self time there.

Solution 3 - Java

There's a simple way to get total time of a routine as a percent of wall-clock execution time (rather than milliseconds). Just use ctrl-break to get a bunch of stackshots while you're waiting for it. The fraction of them containing the routine is the % of time it takes. The accuracy depends on how many shots you take. If you're just looking for where the problems are, you don't need precision time measurement. Here's a short explanation of how it works.

Solution 4 - Java

I think you want to find out how much time does each method execution takes. You would want to use JETM to monitor the performance. This would give you entrance time, exit time and a time difference for each method. You would find out which method is taking how much time.

If you are using Spring then it becomes easy to integrate JETM http://jetm.void.fm/howto/spring_2_x_integration.html

Solution 5 - Java

you can use jprofiler or some javaagent tools to monitor the method execute time for you.there is some open source tools on the github,like simpleAPM.

Solution 6 - Java

JavaAssist is a class library to manipulate your Java Byte Code without touching the source. Let's take an example of measuring time taken to execute a method.

public class Subject {
	/**
	 * Timetaken for start & end of the method
	 * 
	 * @throws InterruptedException
	 */
	public void method2() throws InterruptedException {
		// Some business logic :)
		Thread.sleep(2000);
	}
}

To measure time taken for executing subject.method2(), you could enhance the Subject.methods() by adding code start and end of the method as shown.

public class JavaAssist {
	public static void main(String[] args) {
		timeTaken();
	}

	public static void timeTaken() {
		try {
			ClassPool p = ClassPool.getDefault();
			CtClass cc = p.get("Subject");
			CtMethod meth2 = cc.getDeclaredMethod("method2");
			meth2.insertBefore("System.out.println(\" Start : \"+new java.util.Date());");
			meth2.insertAfter("System.out.println(\" End : \"+new java.util.Date());");
			// cc.writeFile(".");
			Class c = cc.toClass();
			Subject s = (Subject) c.newInstance();
			s.method2();
			cc.detach();
		} catch (Exception e) {
			// suppressed
		}
	}
}

Output: Start : Wed May 26 17:24:18 EDT 2010 End : Wed May 26 17:24:20 EDT 2010

Reference http://www.csg.is.titech.ac.jp/~chiba/javassist/tutorial/tutorial.html#read

http://www.csg.is.titech.ac.jp/~chiba/javassist/html/

Origin Post from: http://www.senthilb.com/2010/05/javaassist-byte-code-enhancement.html

Solution 7 - Java

you could use a

 long startTime = System.currentTimeMillis();

at the beggining

and

 long endTime = System.currentTimeMillis();

and finally to get the result

 long result = endTime - startTime; //Note, part might be backwards, I don't
                                    //Remember

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionkpozinView Question on Stackoverflow
Solution 1 - JavaJoseph CottamView Answer on Stackoverflow
Solution 2 - JavaJB-View Answer on Stackoverflow
Solution 3 - JavaMike DunlaveyView Answer on Stackoverflow
Solution 4 - JavavsinghView Answer on Stackoverflow
Solution 5 - JavadingjshView Answer on Stackoverflow
Solution 6 - JavaSenthil BalakrishnanView Answer on Stackoverflow
Solution 7 - JavaBrendan LesniakView Answer on Stackoverflow