DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
Securing Your Software Supply Chain with JFrog and Azure
Register Today

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

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

Measuring Performance Using Perf4j Library

Sandeep Patel user avatar by
Sandeep Patel
·
May. 01, 13 · Interview
Like (0)
Save
Tweet
Share
9.60K Views

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:-
           http://perf4j.codehaus.org/downloads.html
  • 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

 

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

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com

Let's be friends: