According to what you said, you have a save method, it should be static or not. Anyway, you want to know how many times it runs. for a period of time.
Use Aspecjj
With Aspectj, you will be able to lastrear the number of executions of certain methods.
I suggest you decide to save this information to a log file, or increment a variable that you can later read.
Like?
I will assume that you are using Maven to manage the lifecycle of your builds. First, have the dependencies of Aspectj, you will need the following artifacts:
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>1.6.11</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.6.11</version>
</dependency>
Now create a Joinpoint for the desired methods:
//A seguinte classe deve servir de template para você:
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
@Aspect
public class TrackingMethodsAspect {
public long volatile executedTimes = 0;
@Before("execution(* SuaClasse.seuMetodo(..))")
public void track(JoinPoint joinPoint) {
executedTimes++;
}
}
In this strategy, later you can read the value written for variable executedTimes and make decisions on how to better manage your threads, and the amount of objects.
What Aspectj will do to instrumentate your classes, in case your method and allow you to enter byde-codes. Both in Runtime how much in buildtime
And last and not least use the Aspectj plugin to effectively make the instrumentation happen.
You must have this plugin in the POM.XML plugin session
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>aspectj-maven-plugin</artifactId>
<version>1.2</version>
<executions>
<execution>
<goals>
<goal>compile</goal><!-- to weave all your main classes -->
<goal>test-compile</goal><!-- to weave all your test classes -->
</goals>
</execution>
</executions>
<configuration>
<weaveDependencies>
<weaveDependency>
<groupId>com.seuprojeto</groupId>
<artifactId>ProjectB</artifactId>
</weaveDependency>
</weaveDependencies>
<showWeaveInfo>true</showWeaveInfo>
<source>${maven.compiler.source}</source>
<target>${maven.compiler.target}</target>
</configuration>
</plugin>
In the example above the instrumentation will take place in build-time
And not in Runtime, it is important to pay attention to this detail.
It is a good way, yes, I found the approach interesting. I actually use Gradle to build applications.
– Flávio Granato
@Flávio Granato You apply aspects in Runtime. And it is also possible to use aspectj without Maven. And it also has the flexibility to use with Spring(Aspectj + Spring.) Check out this tutorial, you can instrumentate very quickly with these techniques: http://www.mkyong.com/spring3/spring-aop-aspectj-annotation-example/
– Filipe Miranda