Measuring Performance Using Perf4j Library
Join the DZone community and get the full member experience.
Join For Free
- "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.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 - 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 :-
Library
Published at DZone with permission of Sandeep Patel, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.
Trending
-
Deploying Smart Contract on Ethereum Blockchain
-
How to Use an Anti-Corruption Layer Pattern for Improved Microservices Communication
-
Java Concurrency: Condition
-
From CPU to Memory: Techniques for Tracking Resource Consumption Over Time
Comments