Total method time in Java VisualVM

前端 未结 7 869
野趣味
野趣味 2020-12-04 21:09

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

相关标签:
7条回答
  • 2020-12-04 21:36

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

    0 讨论(0)
  • 2020-12-04 21:36

    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

    0 讨论(0)
  • 2020-12-04 21:38

    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.

    0 讨论(0)
  • 2020-12-04 21:40

    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)

    0 讨论(0)
  • 2020-12-04 21:45

    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 讨论(0)
  • 2020-12-04 21:50

    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

    0 讨论(0)
提交回复
热议问题