简体   繁体   English

测量Java执行时间

[英]Measure Java Execution Time

I'm searching for a 'something' that can measure the execution time if my java code. 我正在寻找一种可以测量我的Java代码执行时间的“东西”。 I know I can do that with intensive use of System.nanoTime() and things like that, but I need something more advanced. 我知道我可以通过大量使用System.nanoTime()和类似的东西来做到这一点,但是我需要更高级的东西。

Here what I want to do 在这里我想做什么

gate.start("request");
gate.start("dbstuff");
doDbStuff();
gate.stop("dbstuff");
gate.start("businesslogic");
doSomeSimpleStuff();
for (int i = 0; i < 100; i++) {
  gate.start("complexeAlgorithm"); // sum the 100 cycles
  doComplexeAlgorithm();
  gate.stop("complexeAlgorithm");
}
gate.stop("businesslogic");
gate.stop("request");

The output should be something like this 输出应该是这样的

request 3.000 ms
+ dbstuff 700 ms
+ businesslogic 2.100 ms
  + complexeAlgorithm 300 ms
  + rest 1.800 ms
+ rest 200 ms

That 'something' may be a framework or a JVM tool or anything, that helps me to find my performance killers. 这个“东西”可能是框架,JVM工具或其他任何东西,可以帮助我找到性能杀手。 Any suggestions? 有什么建议么?

look at https://github.com/codahale/metrics 看看https://github.com/codahale/metrics

Metrics is a Java library which gives you unparalleled insight into what your code does in production. Metrics是一个Java库,可让您无比洞察代码在生产中的功能。 Metrics provides a powerful toolkit of ways to measure the behavior of critical components in your production environment. 度量标准提供了一种功能强大的工具包,该工具包可用来衡量生产环境中关键组件的行为。

With modules for common libraries like Jetty, Logback, Log4j, Apache HttpClient, Ehcache, JDBI, Jersey and reporting backends like Ganglia and Graphite, Metrics provides you with full-stack visibility. 通过适用于常见库(如Jetty,Logback,Log4j,Apache HttpClient,Ehcache,JDBI,Jersey)的模块以及报告后端(如Ganglia和Graphite),Metrics可为您提供全栈可见性。

http://metrics.codahale.com/ http://metrics.codahale.com/

You're asking for a profiler, the simplest one is free: have a look at jvisualvm , it comes with the java SDK. 您需要一个探查器,最简单的探查器是免费的:看看jvisualvm ,它是Java SDK附带的。 Another options are Yourkit and jprofiler . 另一个选项是Yourkitjprofiler

Profiling is not an easy topic, so it would be better if you search "java profiler" in the web. 分析不是一个容易的话题,因此,如果您在网络中搜索“ java profiler”,那就更好了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM