<dependency>
<groupId>com.avides.spring</groupId>
<artifactId>spring-profiler</artifactId>
<version>0.1.0.RELEASE</version>
</dependency>
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()
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
@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)