-1

How does one measure the performance of a method/period of time in an application in Java? Is there a class made specifically for this functionality?

George Newton
  • 2,893
  • 6
  • 25
  • 44
  • 2
    Well how do you define "performance"? Now measure this definition. If you're talking about time to execute: `System.nanoTime()` is usually recommended over `System.currentTimeMillis()`, but that's as specific an answer as you can get. – Jeroen Vannevel Apr 11 '14 at 21:34
  • Like in seconds, not asymptoptic analysis – George Newton Apr 11 '14 at 21:35

4 Answers4

2

There are two ways:

  • Testing at design time. This means, you will perform your tests when designing the application. You may be tempted to create some methods and naively measure them by using System.nanoTime() (or, in worst case, System.currentTimeMillis()), and this is a naive approach because you're missing lot of concepts for handling a micro benchmark. Still, if you want to manually benchmark your methods, at least follow proper rules for a micro benchmark. IMO you should not reinvent the wheel, instead use a benchmark framework like JUnitBenchmarks or Caliper where you can measure the time of your algorithms/methods using unit testing.

  • Testing at run time. This means, you will measure the performance when the application is running, usually in a dedicated environment similar to production environment. For this scenario, use a profiler. There are lot of profilers for Java applications, from free license like VisualVM and Java Mission Control (this comes from JDK 7 u40) that are shipped in the JDK distribution to commercial products like Yourkit.

Community
  • 1
  • 1
Luiggi Mendoza
  • 81,685
  • 14
  • 140
  • 306
1

In Java8, there is a new tool for this, have a look at http://openjdk.java.net/projects/code-tools/jmh/

JMH is a Java harness for building, running, and analysing nano/micro/milli/macro benchmarks written in Java and other languages targetting the JVM.

Zavior
  • 6,182
  • 2
  • 25
  • 36
0

You can use System.currentTimeMillis() or System.nanoTime() to get the time before and after the execution of the code you want to measure.

A more sofisticated approach would be to use JMeter or a similar app for generating statistics of many executions.

An even more sofisticated approach would be to use Perf4j.

Andres
  • 9,722
  • 4
  • 37
  • 57
  • 1
    [`System.nanoTime()` is advised over `System.currentTimeMillis()`](http://stackoverflow.com/questions/351565/system-currenttimemillis-vs-system-nanotime). – Jeroen Vannevel Apr 11 '14 at 21:39
  • *You can use System.currentTimeMillis() to get the time before and after the execution of the code* it would be better using [`System.nanoTime()`](http://docs.oracle.com/javase/7/docs/api/java/lang/System.html#nanoTime()) – Luiggi Mendoza Apr 11 '14 at 21:39
  • AFAIK JMeter works for web based applications, doesn't work for desktop or console apps. – Luiggi Mendoza Apr 11 '14 at 21:40
  • You can implement listeners that invoke many kinds of apps. – Andres Apr 11 '14 at 21:42
  • Also, this doesn't work well since it is a micro benchmark. Follow the [proposed rules for good microbenchmarking](http://stackoverflow.com/q/504103/1065197). – Luiggi Mendoza Apr 11 '14 at 21:46
  • 1
    Perf4j project seems abandoned since year 2011... – Luiggi Mendoza Apr 11 '14 at 21:51
0

I'm using this http://metrics.codahale.com/ and it rocks. It comes packaged in DropWizard which is nice. Also pick up a profiler -> yourKit or JMC (free Java Mission Control).

The key is to be lightweight when profiling. You don't want to add too much bias to the equation.

Jason McD
  • 439
  • 1
  • 3
  • 12