Total method time in Java VisualVM

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-17 15:32:21

问题


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?


回答1:


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)




回答2:


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




回答3:


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.




回答4:


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




回答5:


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.




回答6:


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




回答7:


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


来源:https://stackoverflow.com/questions/1892038/total-method-time-in-java-visualvm

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!