This library provides (some of) the functionality of the StackExchange MiniProfiler for JVM-based applications.
(Screenshot)
The JVM MiniProfiler is deployed to Maven Central and can be added to your project as a dependency using the following coordinates:
groupId: io.jdev.miniprofiler
artifactId: miniprofiler-core
version: 0.12.0
Or just download the jar from http://search.maven.org/ if your build system is a bit less connected.
Once installed, start using the profiler thusly:
Profiler profiler = MiniProfiler.start("/url/my web request");
try {
// do some stuff
} finally {
profiler.stop();
}
To profile individual parts of your program, create and stop individual timings:
Timing t = MiniProfiler.current().step("My complicated step");
try {
// stuff
} finally {
timing.stop();
}
If you are using Java 7, you can use Java 7 auto-closable resource blocks for even cleaner code:
try (Timing t = MiniProfiler.current().step("My complicated step")) {
// stuff
}
Steps are nestable, and will appear nested in profiler output:
try (Timing t = MiniProfiler.current().step("My complicated step")) {
// stuff
try (Timing t1 = MiniProfiler.current().step("A sub-part of the complicated step")) {
// sub-stuff 1
}
try (Timing t2 = MiniProfiler.current().step("A second, sibling sub-part of the complicated step")) {
// sub-stuff 2
}
}
The default code above uses a static reference to a global ProfilerProvider object. If you are using a dependency injection framework such as Spring or Guice, or if you just like more testable code, then create a ProfilerProvider and inject it into your code. The included DefaultProfilerProvider should be enough for most purposes. Then use it in your code like this:
// at start of request
Profiler profiler = profilerProvider.start("/url/my web request");
try {
// do some stuff
} finally {
profiler.stop();
}
// further in where stuff is happening
Timing t = profilerProvider.current().step("My complicated step");
try {
// stuff
} finally {
timing.stop();
}
Add the miniprofiler-jdbc module to your project:
groupId: io.jdev.miniprofiler
artifactId: miniprofiler-jdbc
Then wrap your JDBC DataSource in a ProfilingDataSource and call getConnection() on it as normal.
To see the output of the profiled request on your web page, add a script tag to the bottom of your HTML page, just inside the body tag. The library comes with a ScriptTagWriter class to help with this:
<%= new ScriptTagWriter().printScriptTag(MiniProfiler.current(), request.getContextPath() + "/miniprofiler")%>
This will output a javascript script tag which will load the necessary javascript, css and data from under /miniprofiler in your web app.
The core library includes a ProfilingFilter which does several jobs:
- Start and stop profiling for all web requests, excluding anything that looks like a file to be served up directly
- Serve up front-end javascript, CSS and templates used for displaying the profiling info in a web page
- Serve up profiling data as JSON
The filter can be included in your web.xml like this:
<filter>
<filter-name>miniprofiler</filter-name>
<filter-class>io.jdev.miniprofiler.javax.servlet.ProfilingFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>miniprofiler</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
By default, the filter will expect requests for the static resources and data under /miniprofiler in your web app.
You can change this by passing it an init-param of path. Be sure to pass the same value to the ScriptTagWriter if you are using that.
The filter can also be configured to accept cross-origin requests by passing an allowed-origin init-param with the value that you would like the Access-Control-Allow-Origin header to be.
There are a couple of extra pieces of support for Java EE and Jakarta EE applications. Include the appropriate module for your platform:
For Jakarta EE (Jakarta CDI / EJB):
groupId: io.jdev.miniprofiler
artifactId: miniprofiler-jakarta-ee
For Javax EE (legacy javax CDI / EJB):
groupId: io.jdev.miniprofiler
artifactId: miniprofiler-javax-ee
Each module contains a DefaultCdiProfilerProvider which is a ProfilerProvider instance ready to be injected into your CDI-managed beans.
They also contain an interceptor for profiling EJB calls. Add the following to your beans.xml (using the appropriate package):
<interceptors>
<class>io.jdev.miniprofiler.jakarta.ee.ProfilingEJBInterceptor</class>
</interceptors>
Then add the @Profiled annotation to any EJB that you want profiled, and EJB method calls will appear in your profiler output.
Please raise issues in the GitHub issue tracker