Platinum Partner
java,high-perf,performance

Measuring Performance Using Perf4j Library

  • "Perf4j.jar" is a java library for measuring performance of a java file.
  • The Download link:-
  • In this Demo, "We are using LOG4J library to log in the console.Logger Info will print the numbers in the console. A timer is used to randomly select some Milli seconds to delay".
  • The Project Structure:-

  • The java code for testing Perf4j is Perf4jDemo.java,
    package com.sandeep.perf4j.test;
     
    import org.apache.log4j.BasicConfigurator;
    import org.apache.log4j.Logger;
    import org.perf4j.StopWatch;
    import org.perf4j.log4j.Log4JStopWatch;
     
    public class Perf4jDemo {
     
     private static final Logger logger = Logger.getLogger(Perf4jDemo.class);
      
     public static void main(String[] args) {
       
      BasicConfigurator.configure();
     
      StopWatch watch = new Log4JStopWatch();
       
      logger.info("start time : "+watch.getStartTime());
       
       
         try {
           
       Thread.sleep((long)(Math.random() * 1000L));
        
      } catch (InterruptedException e) {
        
       logger.error("InterruptedException : "+e);
      }
         
       
      logger.info("Elapsed Time : "+ watch.getElapsedTime());
       
      logger.info("Stop Time : "+watch.stop());
     }
    }

  • The log4j.properties file,
          log4j.rootLogger=DEBUG, A1
  log4j.appender.A1=org.apache.log4j.ConsoleAppender
  log4j.appender.A1.layout=org.apache.log4j.PatternLayout
  log4j.appender.A1.layout.ConversionPattern=%d [%t] %-5p %c - %m%n
  # Print only messages of level WARN or above in the package com.foo.
  log4j.logger.com.foo=INFO 

  • The Output in console(text),
  INFO com.sandeep.perf4j.test.Perf4jDemo  - start time : 1363364781687
  INFO com.sandeep.perf4j.test.Perf4jDemo  - Elapsed Time : 155
  INFO org.perf4j.TimingLogger  - start[1363364781687] time[155] tag[]
        INFO com.sandeep.perf4j.test.Perf4jDemo  - Stop Time : start[1363364781687]  time[155]  

  • The output of the java file Console is(screenshot),
  • The Demo Code :-
         Download Link

 

Published at DZone with permission of {{ articles[0].authors[0].realName }}, DZone MVB. (source)

Opinions expressed by DZone contributors are their own.

{{ tag }}, {{tag}},

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}
{{ parent.authors[0].realName || parent.author}}

{{ parent.authors[0].tagline || parent.tagline }}

{{ parent.views }} ViewsClicks
Tweet

{{parent.nComments}}