Monitor and collect Java application performance data through logs

Source: Internet
Author: User
Tags sleep system log thread web services cpu usage high cpu usage websphere application server

Introduction

System log is an important tool for application problem diagnosis and operation maintenance. Logback, log4j are the logging APIs that are commonly used in the Java platform. At present, most of the products are only the important parameters of the system, state changes and abnormal information through the log output. This article will introduce the PERF4J is a dedicated Java server-side code timing, logging and monitoring results of the Open Source Toolkit. PERF4J is an extension of the Common Log toolkit that can be used to count and publish raw performance data to customizable output sources such as consoles, log files, JMX, and so on. PERF4J provides a variety of ways to integrate with Java code, and developers and system-dimensional personnel have the flexibility to embed PERF4J APIs into applications of various architectures.

PERF4J currently relies on open source project collaboration platform Codehaus for document and code management, and the next step is to migrate the project to the Github platform so that more communities and developers can participate in development and maintenance. PERF4J welcomes users to present new functional requirements and encourages the contribution of custom or extended code to the PERF4J source. The perf4j version used by the sample code in this article is 0.9.16, which readers should be aware of when downloading a class pack or configuring Maven.

Before reading the article, you have some understanding of Java annotations, JMX, aspect-oriented programming. In particular, the use of jconsole and the way Spring AOP is configured are more familiar.

The article first explains in which application scenario should give priority to the use of perf4j. Then it's a concrete explanation of how pef4j and applications are integrated. Finally, you will find out how to generate the collected data into a visual chart that is easy to analyze.

Application Scenarios

When you encounter performance problems on the Java platform, such as high CPU usage and slow system response, the usual analysis method is to use the JVM profiling tool to crawl the CPU footprint for a period of time before the system bottleneck point, and then troubleshoot several methods with the highest CPU occupancy rate. The advantage of PERF4J is the ability to continuously track the execution efficiency of the functional code of the statistic, and the in-depth analysis of the different methods in the two versions, which can be identified as early as possible during the development cycle. PERF4J can also be used in a product environment, starting at an early stage of operations, to monitor its statistical data as a system's performance and health indicators over time.

Preferred PERF4J Application Scenario:

Java native code invocation (JNI)

Distributed systems, cluster deployments

Service-Oriented Architecture (SOA)

Remote method call (RMI)

Developers must isolate the performance issues of local methods, remote methods, and WEB services to prevent interference with the analysis of the Java application itself. Logging is the simplest way to use a distributed architecture or cluster deployment system is relatively complex, different network environment, basic hardware and operating system differences, virtual host resources and configuration differences, etc., it is difficult to use a unified tool to monitor code level performance indicators. Logging can be easily added to a variety of programs and is the least expensive way to spend resources and time. PERF4J provides a CSV-formatted conversion tool that enables developers to easily summarize statistical results with the help of Third-party tools.

Integrated into applications

Here are two ways to use the APIs provided by PER4J. In practical projects, flexible selection should be based on existing procedural frameworks and monitoring purposes. In addition, for the WebSphere application server's own logging system, additional measures must be taken to ensure that the perf4j is working properly.

Timing the Code snippet

Org.perf4j.StopWatch in PERF4J is the basic tool in the entire API. This is a well encapsulated timer. You can embed stopwatch anywhere in your code. This approach often causes complex methods to be decomposed, thus helping to pinpoint the root cause of the problem. The following is an introduction to the specific uses of Listing 1 and list.

Listing 1.STOPWACTH Basic usage

The public static void Basicstopwatch () throws interruptedexception{ 
   //creates the STOPWACTH when the timer starts, and can be used after Stopwatch.start () Reset timer start time point
   stopwatch stopwatch = new Stopwatch ("Transactiona"); 
   Executes the code that needs to be timed
   thread.sleep (2 * 1000L);    
   String result = Stopwatch.stop (); 
   System.out.print (result); 
}

Example of the final output in Listing 1: start[1340442785756] time[1995] Tag[transactiona]. Setting Tag[transactiona] In a constructor is used to differentiate between different business logic and can be viewed as a transaction (Transaction) in performance analysis.

If you need to separate multiple pieces of code, you can define multiple transactions using the lap () method of the Loggingstopwatch class.

Listing 2.LoggingStopWatch Usage

public static void Loggingstopwacth () throws interruptedexception{ 
   Loggingstopwatch stopwatch = new Loggingstopwatch (); 
   Thresholds are set, and results that are less than this threshold will not be recorded
   stopwatch.settimethreshold (1*1000l); 
   Thread.Sleep (2 * 1000L); 
   Stop the current timer and start a new starting point
   stopwatch.lap ("transactionb"); 
   Thread.Sleep (500L); 
   Stopwatch.stop ("Transactionc"); 
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.