Git Product home page Git Product logo

java-mini-profiler-core's Introduction

Java Mini Profiler

JMP is a mini-profiler for Java inspired by mvc-mini-profile (and Jeff Atwood's blog post).

ScreenShot

Quick start

1. Add the following repo/dependencies to your maven pom -

	<dependency>
		<groupId>au.com.funkworks.jmp</groupId>
		<artifactId>java-mini-profiler-web</artifactId>
		<version>0.7</version>
	</dependency>	

	<repository>
    	<id>alvins-releases</id>
    	<url>https://github.com/alvins82/mvn-repo/raw/master/releases</url>
	</repository>

2. Add to your web.xml the servlet and filter.

	<servlet>
		<servlet-name>miniprofiler</servlet-name>
		<servlet-class>au.com.funkworks.jmp.MiniProfilerServlet</servlet-class>		
	</servlet>
	<servlet-mapping>
		<servlet-name>miniprofiler</servlet-name>
		<url-pattern>/java_mini_profile/*</url-pattern>
	</servlet-mapping>
	
	<filter>
		<filter-name>miniprofiler-filter</filter-name>		 
		<filter-class>au.com.funkworks.jmp.MiniProfilerFilter</filter-class>				
	</filter>
	<filter-mapping>
		<filter-name>miniprofiler-filter</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>

3. Add somewhere in your jsp.

	${mini_profile_includes}

4. Add some code to profile using the syntax below.

	Step step = MiniProfiler.step("Some things happening");
	...	
	// do some work	
	...
	step.close();

5. Load your web-application and watch the profile in the top left corner as shown below.

Optional

SQL Profiling

This allows you to see how long your queries are taking and the actual query itself. This is implemented using a JDBC data-source proxy.

1. Include the following in your maven to include the sql dependency.

	<dependency>
		<groupId>au.com.funkworks.jmp</groupId>
		<artifactId>java-mini-profiler-sql</artifactId>
		<version>0.7</version>
	</dependency>	

2. Configure the original dataSource so it is wrapped by au.com.funkworks.jmp.SqlRecordDataSource. Example shown below.

	<jee:jndi-lookup id="dataSourceActual" jndi-name="java:comp/env/jdbc/dataSource" />

   	<bean id="dataSourceProxy" class="au.com.funkworks.jmp.SqlRecordDataSource">
    	<constructor-arg ref="dataSourceActual" />
  	</bean>
  	 
	<bean id="dataSource" class="org.springframework.jdbc.datasource.LazyConnectionDataSourceProxy">
        <property name="targetDataSource">
            <ref bean="dataSourceProxy" />
        </property>
    </bean>

JSP Profiling

It is often beneficial to profile around jsp statements. A good example is when you are using Spring's Open Session In View (OSIV) pattern.

1. Include the following taglib in your JSP.

	<%@ taglib prefix="profiler" uri="http://www.funkworks.com.au/tags/jmp" %>

2. Use the jmp tag to profile around bits of JSP code.

<profiler:jmp description="Profiling some jsp code">

jsp stuff here...

</profiler:jmp>

AOP Public Method Profiling

The real power of JMP comes alive with the use of aspect oriented programming. To enable AOP profiling ensure aop is enabled.

There are two options available.

1. The first options is to profile around all public methods of your project - this is the most powerful option and gives the most benefit.

To use it - create an aspect class in your project - an example is shown below.

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

import au.com.funkworks.jmp.MiniProfiler;
import au.com.funkworks.jmp.Step;

@Aspect
@Component
public class JMPFullAspect {

	@Pointcut(value="execution(public * *(..))")
	public void anyPublicMethod() {
	}
	
	@Pointcut("within(your.package..*)")
    private void inYourPackage() {}
 
	@Around("anyPublicMethod() && inYourPackage()")
	public Object profile(ProceedingJoinPoint pjp) throws Throwable {
		String desc = pjp.getSignature().toShortString();
		if (desc.endsWith("()")) {
			desc = desc.substring(0, desc.length()-2);
		} else if (desc.endsWith("(..)")) {
			desc = desc.substring(0, desc.length()-4);
		}
		
		String packageName = pjp.getSignature().toLongString().split(" ")[2];
		String tag = null;
		
		// optional tag for roll-up of timings e.g. around dao layer
		if (packageName.startsWith("your.package.dao")) {
			tag = "DAO Layer";
		}
		
		try (Step step = MiniProfiler.step(desc, tag);) {			
			return pjp.proceed();
		}
	}
}

You also need to enable AOP if not already enabled.

	<aop:aspectj-autoproxy proxy-target-class="true"/>

2. Another option is to use the @Profiler annotation - simply annotate your method as below.

	@Profiler
	public void yourMethod() {
		// do some things
	}

You also need to enable this aspect by including a spring context in your web.xml.

	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>
			...other contexts
			classpath:au/com/funkworks/jmp/applicationContext-jmp.xml							
		</param-value>
	</context-param>

Notes

  • If not using jsp/jstl - the 'mini_profile_includes' attribute is set on request object via setAttribute)
  • jQuery is required to be loaded before including '${mini_profile_includes}' in your JSP.

java-mini-profiler-core's People

Contributors

alvins82 avatar

Stargazers

 avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.