Per4j tutorial: Getting started with Per4j simple example | JavaInUse

Per4j tutorial: Getting started with Per4j simple example

During application development we test everything in Dev and QA environment and then if the performance is satisfactory the application is deployed in production. In production we try to find the performance of the application using the Logs. Time taken by various components is calculated by using the time stamps logged by the application System.currentTime() in our log files. But we need to further analyze the logs to calculate the performance using this information.

Perf4j is an open source performance logging framework that helps us out here. “Perf4j.jar” is a java library for measuring performance of a java file. Its can be used with log4J
  • Open source
  • Simple to implement
  • Supports Statistical Analysis
  • Integration possible with other logging framework like log4j
  • @Profile annotation for use with Aspects
Create the eclipse project as follows-
Create the as follows-
log4j.rootLogger=DEBUG, TEST
log4j.appender.TEST.layout.ConversionPattern=%d [%t] %-5p %c - %m%n
In the below example we create the for testing Per4j as follows-
package com.javainuse;

import org.apache.log4j.BasicConfigurator;
import org.apache.log4j.Logger;
import org.perf4j.StopWatch;
import org.perf4j.log4j.Log4JStopWatch;

public class Per4jTest {

    private static final Logger logger = Logger.getLogger(Per4jTest.class);

    private void funtion1() {
        StopWatch stopWatch = new Log4JStopWatch("funtion1");
        try {
            Thread.sleep((long) (Math.random() * 1000L));
        catch (InterruptedException e) {


    private void funtion2() {
        StopWatch stopWatch = new Log4JStopWatch("funtion2");
        try {
            Thread.sleep((long) (Math.random() * 1000L));
        catch (InterruptedException e) {


    public static void main(String args[]) {
        BasicConfigurator.configure();"Hello World Perf4j");
        Per4jTest demo = new Per4jTest();

Run the Per4jTest class as a java application. We get the output as follows-
Per4j also allows to further analyze the logs.
Our logs.log file will be as follows
0 [main] INFO com.javainuse.Per4jTest  - Hello World Perf4j
393 [main] INFO com.javainuse.Per4jTest  - funtion1
393 [main] INFO org.perf4j.TimingLogger  - start[1473783067503] time[390] tag[funtion1]
511 [main] INFO com.javainuse.Per4jTest  - funtion2
511 [main] INFO org.perf4j.TimingLogger  - start[1473783067894] time[118] tag[funtion2]
Keep the log file and the perf4j-0.9.16 jar in the same folder and run the following command using the command prompt-
java -jar perf4j-0.9.16.jar logs.log -t 900000

We get the statistical analysis of the logs. We can also get the graphical analysis of the logs by using the command-
java -jar perf4j-0.9.16.jar --graph per4jgraph.html logs.log

per4jgraph.html is created as follows-

Download Source Code

Download it - Per4j Example(Add the Per4j and log jar in lib to run this)

See Also

Using Java Reflections API to map Object Elements
Difference between Spy and Mock in Mockito
Difference between Mock thenCallRealMethod() and Spy in Mockito
Checking the specified class contains a field matching the specified name using Java Reflections
Getting Started with JMS Messaging- ActiveMQ Hello World Tutorial
Getting Name of Current Method inside a method in Java