Performance monitoring precision

Monitoring application performance is a primary use case of commons-monitoring. Java provides 2 timers implementation, as System.currentTimeMillis and System.nanoTime().

Those timers methods performances and real precision depends on the hardware, OS and JRE implementation.

MS Windows

Based on SUN technical documentation, both implementation precision depend on underlying hardware.

Both SUN JRE and Bea Jrockit implementation shows bad performances of nanoTime on Windows, but the currentTimeMillis precision is too limited for use as fine-grained performance counters.

Sun Solaris

On this platform, a quick & dirty bench [1] demonstrates both methods have comparable performances. No technical info on real precision, but nanoTime would give the better one available.

Other systems

?? any info available ??

Java 1.3 / Java 1.4

backport-util-concurrent (used to backport commons-monitoring on pre-java5) provides an implementation of System.nanoTime that delegates to currentTimeMillis with unit conversion. On Sun JRE 1.4 it also uses an internal SUN performance package to get a better precision.

conclusion

Based on those informations, currentTimeMillis - even with nice performances on Windows - cannot be used for fine-grained monitoring. In all cases nanoTime provides the better time-counter available on the system, in terms of both precision and fiability

[2]

   1 public class CurrentTimeMillisVsNanoTime
   2 {
   3     static int loops = 1000000;
   4     public static void main( String[] args )
   5     {
   6         long time = System.nanoTime();
   7         for ( int i = 0; i < loops; i++ )
   8         {
   9             System.nanoTime();
  10         }
  11         System.out.println( "System.nanoTime took     : " + (System.nanoTime() - time) + "ns" );
  12 
  13         time = System.nanoTime();
  14         for ( int i = 0; i < loops; i++ )
  15         {
  16             System.currentTimeMillis();
  17         }
  18         System.out.println( "System.currentTimeMillis : " + (System.nanoTime() - time) + "ns" );
  19     }
  20 }

Monitoring/Precision (last edited 2009-09-20 23:48:53 by localhost)