Skip to content
This repository has been archived by the owner on Oct 10, 2024. It is now read-only.

Latest commit

 

History

History
64 lines (57 loc) · 2.8 KB

README.md

File metadata and controls

64 lines (57 loc) · 2.8 KB

spring-profiler

Maven Central Codacy Badge Coverage Status Build Status

Maven

<dependency>
    <groupId>com.avides.spring</groupId>
    <artifactId>spring-profiler</artifactId>
    <version>0.1.0.RELEASE</version>
</dependency>

Simple example

Profiler profiler = Profiler.startProfiling("anyIdentifier");
Thread.sleep(100);
// or any other code you want to profile
profiler.stop();

This will give you a log-message (debug-level) that looks similar to this:

execution time for anyIdentifier: 102ms

After stopping the profiler, you have access to some execution details, for example the exact execution-time by calling profiler.getDurationMillis()

Example with explicit given ProfilingHandler and allowedMillis

Profiler profiler = Profiler.startProfiling("anyIdentifier", new SysoutProfilingHandler());
Thread.sleep(100);
// or any other code you want to profile
profiler.stop(50);

This will give you an output to the System.out that looks similar to this:

WARNING: execution time for anyIdentifier: 102ms (allowed: 50ms)

If you do not specify a ProfilingHandler, a LoggingProfilingHandler is used as default (see example above). If you do so and the allowed time is exceeded, the logging-level switches to warning.

Of course, you can give your own ProfilingHandler-implementation (and even several at once) that meets your needs

Profiling of methods simply by annotation (only supported in a spring-application)

@Component
public class anyClass
{
    @Profiling
    public void anyMethod()
    {
        Thread.sleep(100);
        // or any other code you want to profile
    }
}

To get this working, simply add an @EnableProfiling-annotation at any of your configuration-classes (or the Application-class). After executing the method "anyMethod", it will give you a log-message (debug-level) that looks similar to this:

execution time for method anyClass.anyMethod(): 102ms

Explicit ProfilingHandlers and allowedMillis (explained in example above) can be set via the @Profiling-annotation-attributes (which also are well documented in javadoc)