Search code examples
javareflectionprofilingvisualvmjvisualvm

How to profiling Java methods invoked by reflect


I'm using JVisualVM to profile my Java application.

In my application, I use reflect to run some methods. For example:

public void a() {}

public void b() {}

public void runMethod() {
    Method m = this.getClass().getMethod("a");
    // or getMethod("b) in some cases
    m.invoke(this, [])
)

In JVisualVM profiling, I can only see the CPU time used by runMethod. I'd like to see how many CPU time is used by method a or b. How can I do that? Thanks.


Solution

  • you can do like this :

            public void a(){               // just for testing
              try {
                    Thread.sleep(1);          //OR some for loop to print something
                 } catch (InterruptedException e) {
    
                e.printStackTrace();
                 }
             }
    
               long startTime=System.currentTimeMillis();
                m.invoke(Bidlist[0], null);
                long endTime=System.currentTimeMillis();
                System.out.println("show takes "+(endTime-startTime)+" ms");
    

    since you are not doing any operation in a so it would give 0. To understand i put sleep(1)