Java VisualVM 中的总方法时间

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1892038/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 23:35:39  来源:igfitidea点击:

Total method time in Java VisualVM

javaprofilingvisualvm

提问by kpozin

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.)

在 Java VisualVM 中,有什么方法可以显示总方法时间,而不是“自己的时间”?(后者不是特别有用,因为它没有告诉您任何有关方法实际运行所需时间的信息。)

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

如果没有,是否有任何独立的免费 Java 分析器可以计算总方法时间?

采纳答案by Joseph Cottam

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.

可以从标准的“Profiler”或“Sampler”数据创建快照。但是,“Profiler”快照只能在应用程序关闭之前创建,而“Sampler”快照可以随时创建。

(The above information is based on VisualVM 1.3.1)

(以上信息基于VisualVM 1.3.1)

回答by Brendan Lesniak

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

回答by Mike Dunlavey

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.

有一种简单的方法可以将例程的总时间作为挂钟执行时间(而不是毫秒)的百分比。只需使用 ctrl-break 即可在等待时获得一堆堆栈快照。其中包含例程的部分是所需时间的百分比。准确度取决于您拍摄的次数。如果您只是在寻找问题所在,则不需要精确的时间测量。这是对其工作原理的简短说明。

回答by JB-

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

只需拍摄分析结果的快照。您将在那里获得挂钟时间和自拍时间。

回答by Senthil Balakrishnan

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.

JavaAssist 是一个类库,可以在不接触源代码的情况下操作 Java 字节码。让我们举一个例子来衡量执行一个方法所花费的时间。

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.

要测量执行所花费的时间subject.method2(),您可以Subject.methods()通过添加方法的开始和结束代码来增强,如图所示。

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

输出:开始:2010 年 5 月 26 日星期三 17:24:18 EDT 结束:2010 年 5 月 26 日星期三 17:24:20 EDT

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

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

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

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

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

原帖来自:http: //www.senthilb.com/2010/05/javaassist-byte-code-enhancement.html

回答by vsingh

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.

我想你想知道每个方法执行需要多少时间。您可能希望使用 JETM 来监控性能。这将为您提供每种方法的进入时间、退出时间和时差。你会发现哪种方法需要多少时间。

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

如果您使用的是 Spring,那么集成 JETM 就变得很容易 http://jetm.void.fm/howto/spring_2_x_integration.html

回答by dingjsh

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.

你可以使用jprofiler或者一些javaagent工具来为你监控方法的执行时间。github上有一些开源工具,比如simpleAPM。