0

I have an issue with my web application and I suspect that the session scoped beans are taking a long time to load.

Is there a way I can log how long the beans are taking to initialize?

Fritz Duchardt
  • 11,026
  • 4
  • 41
  • 60
DD.
  • 21,498
  • 52
  • 157
  • 246
  • *Is there a way I can log how long the beans are taking to initialize?* yes, in a `@PostConstruct` method. But since we don't have any code to inspect, we cannot say if the issue is there or somewhere else. – Luiggi Mendoza Mar 09 '15 at 16:44

3 Answers3

0

You could write a simple time logging mechanism along these lines setting the start time at the beginning of your constructor and the end time in a method annotated with @PostConstruct. This should give you a good idea how much time Spring takes to initialize your bean and wire up its dependencies.

Community
  • 1
  • 1
Fritz Duchardt
  • 11,026
  • 4
  • 41
  • 60
  • Does the constructor get called before or after the dependencies are injected? I need to include the dependency injection time as well in the initialized time that I am measuring. – DD. Mar 09 '15 at 17:01
  • Default constructor gets called before dependency injection – Fritz Duchardt Mar 10 '15 at 08:28
0

A simple log4j configuration should enable you to understand how long which bean(s) are taking. Set the logging level to DEBUG and use the date/time format in the log4j appender to print out the times.

zoostar
  • 132
  • 3
0

SLF4j (that can easily be used with Spring: the reason why I found that) has a special TimeInstrument implementation for such purposes: [StopWatch]

Maybe this helps somebody, even though this question is a little old.

EDIT: The nice thing about it: it actually uses nanoTime() which is - according to these posts[1, 2] - the most precise solution.

(Code snippet from org.slf4j.profiler.StopWatch.start(String) )

public void start(String name) {
    this.name = name;
    startTime = System.nanoTime();
    status = TimeInstrumentStatus.STARTED;
}
Community
  • 1
  • 1
Unknown Id
  • 460
  • 3
  • 12