在Java代码中进行基准测试 [英] Benchmarking inside Java code

查看:235
本文介绍了在Java代码中进行基准测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近一直在研究基准测试,我一直对记录程序数据等感兴趣。我很想知道我们是否可以实现自己的内存使用代码并在我们的程序中有效地实现我们自己的时间消耗代码。我知道如何检查代码运行所需的时间:

I have been looking into benchmarking lately, I have always been interested in logging program data etc. I was interested in knowing if we can implement our own memory usage code and implement our own time consumption code efficently inside our program. I know how to check long it takes for a code to run:

public static void main(String[]args){
        long start = System.currentTimeMillis();
        // code
        System.out.println(System.currentTimeMillis() - start);
    }

我也研究了强大的Java基准测试,第1部分:问题,本教程非常全面。显示 System.currentTimeMillis(); 的负面影响。然后教程建议我们使用 System.nanoTime(); (使其更准确?)。

I also looked into Robust Java benchmarking, Part 1: Issues, this tutorial is very comprehensive. Displays the negative effects of System.currentTimeMillis();. The tutorial then suggests that we use System.nanoTime(); (making it more accurate?).

I还查看了确定Java中的内存使用情况以了解内存使用情况。该网站显示了如何实施它。已提供的代码看起来无效,因为此人正在调用

I also looked at Determining Memory Usage in Java for memory usage. The website shows how you can implement it. The code that has been provided looks inefficent because the person is calling

long L = Runtime.getRuntime()。totalMemory() - Runtime.getRuntime ()。freeMemory();

之后他调用 System.gc(); (4 * 4)= 16次。然后再次重复该过程。
这不会占用内存吗?

After this he calls System.gc(); (4 * 4) = 16 times. Then repeating the process again. Doesn't this also take up memory?

所以在结论中,是否可以在java程序中实现一个高效的基准测试代码?

So in conlusion, is it possible to implement an efficent benchmarking code inside your java program?

推荐答案

是的,可以在java代码中有效地实现性能基准测试。重要的问题是,任何类型的性能基准测试都会增加自己的开销,以及你想要多少。 System.currentMill ..()是性能良好的基准,在大多数情况下,nanoTime()是一种矫枉过正。

Yes it is possible to effectively implement performance benchmarks in java code. The important question is that any kind of performance benchmark is going to add its own overhead and how much of it do you want. System.currentMill..() is good enough benchmark for performance and in most of the cases nanoTime() is an overkill.

对于内存System.gc会告诉你多种多样的不同运行的结果(因为gc run永远不会被保证。)我通常使用Visual VM进行内存分析(免费),然后使用TDA进行转储分析。

For memory System.gc will show you varied results for different runs (as gc run is never guranteed.) I generally use Visual VM for memory profiling (its free) and then use TDA for dumps analyzing.

一种较少侵入性的方法是使用面向方面的编程。您可以只创建一个在特定注释或方法集上运行的Aspect,并编写@Around建议来收集性能数据。

One way to do it less invasively is using Aspect oriented programing. You can create just one Aspect that runs on a particular Annotation or set of methods and write an @Around advice to collect performance data.

这是一个小片段:

public class TestAspect {

    @LogPerformance
    public void thisMethodNeedsToBeMonitored(){
        // Do Something
    }
    public void thisMethodNeedsToBeMonitoredToo(){
        // Do Something
    } 
}

@interface LogPerformance{}

@Aspect
class PerformanceAspect{
    @Around("the pointcut expression to pick up all " +
            "the @PerfMonitor annotated methods")
    public void logPerformance(){
        // log performance here
        // Log it to a file
    }
}

这篇关于在Java代码中进行基准测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆