JVM Instrumentation. Know how many times a method was called during program execution

2

I have a processing application of batch , currently it is multithreads , I need to know how many times we are executing the save method.

Would anyone have any ideas? I need to increase the number of threads during the night and I would like to have some parameter for my decision.

    
asked by anonymous 03.06.2015 / 16:31

1 answer

4

According to what you said, you have a save method, it must be static or not. Either way, you want to know how many times it runs. over a period of time.

Use AspecjJ

With AspectJ, you will be able to back up the number of runs of certain methods.

I suggest you decide between saving this information to a log file, or incrementing a variable that you can read later.

How?

I'm going to assume that you're using Maven to manage the lifecycle of your builds. First, have AspectJ dependencies, 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 methods you want:

//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, you can later read the value written to variable issuesTimes and make decisions about how to best manage your threads, and the number of objects.

What AspectJ will do will implement your classes, in case your method and allow you to enter byde-codes. Both and buildtime

And last but not least, use the AspectJ plugin for effective instrumentation to happen.

You should have this plugin in the POM.XML plugins 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 above example the instrumentation will occur in build-time And not in runtime , it's important to look at that detail.

    
03.06.2015 / 17:37