Over a million developers have joined DZone.

Measuring Performance Using Perf4j Library

DZone's Guide to

Measuring Performance Using Perf4j Library

· Performance Zone ·
Free Resource

Sensu is an open source monitoring event pipeline. Try it today.

  • "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) {
      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.layout.ConversionPattern=%d [%t] %-5p %c - %m%n
  # Print only messages of level WARN or above in the package com.foo.

  • 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


Sensu: workflow automation for monitoring. Learn more—download the whitepaper.


Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

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

{{ parent.tldr }}

{{ parent.urlSource.name }}