开发者

Total method time in Java VisualVM

In Java VisualVM, is there any way to display total method time, rather than "self time"? (The latter is not particularly useful, sinc开发者_如何转开发e 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?


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)


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


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.


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


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.


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


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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜