Performance Zone is brought to you in partnership with:

A passionate Java Web Developer and Blogger.Continuously testing various technologies and publishing it as post to my blog. Sandeep is a DZone MVB and is not an employee of DZone and has posted 15 posts at DZone. You can read more from them at their website. View Full User Profile

Measuring Performance Using Perf4j Library

  • submit to reddit

  • "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,
    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) {
      StopWatch watch = new Log4JStopWatch();
    "start time : "+watch.getStartTime());
         try {
       Thread.sleep((long)(Math.random() * 1000L));
      } catch (InterruptedException e) {
       logger.error("InterruptedException : "+e);
    "Elapsed Time : "+ watch.getElapsedTime());
    "Stop Time : "+watch.stop());

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

  • 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 Sandeep Patel, author and DZone MVB. (source)

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)