using ASM to calculate functions' time costs.
#How to Use
Just add dependiencies:
<dependencies>
<!-- https://mvnrepository.com/artifact/org.ow2.asm/asm -->
<dependency>
<groupId>org.ow2.asm</groupId>
<artifactId>asm</artifactId>
<version>5.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.ow2.asm/asm-commons -->
<dependency>
<groupId>org.ow2.asm</groupId>
<artifactId>asm-commons</artifactId>
<version>5.1</version>
</dependency>
<dependency>
<groupId>cn.edu.tsinghua</groupId>
<artifactId>aopTimeCost</artifactId>
<version>0.0.2</version>
</dependency>
</dependencies>
Then, compile cn.edu.tsinghua.aop
package and src/main/resources/META-INF/MANIFEST.MF
as a jar (or add the jar if I publish it in official Maven Repo). I suppose the jar name is aopTimeCost.jar
, and the jar is on /Users/yourname/Desktop/aopTimeCost.jar
.
Then, find the file path of the jar aopTimeCost-0.0.2.jar
(normally it is at your local maven repository: ~/.m2/repository/cn/edu/tsinghua/aopTimeCost/0.0.2/aopTimeCost-0.0.2.jar
). Remember this path, I will use to reference it.
Thirdly, if you are using Eclipse, then set the Run Configuration
for your Main class:
VM arguments:
-javaagent:<the aop jar file path>
. e.g., VM arguments:
-javaagent:/Users/jixuan/.m2/repository/cn/edu/tsinghua/aopTimeCost/0.0.2/aopTimeCost-0.0.2.jar
If you are using Idea, see you tomorrow. (The configuration should be similar, I think.)
Now you can enjoy the powerful of this tool:
For example, you have a class like this:
public class Test {
public void doanything() {
System.out.print("Hello World.");
}
}
And you want to know how many times that the function doanything
is called and the total time cost of the fucntion, just modify your code like this:
public class Test {
@Cost
public void doanything() {
System.out.print("Hello World.");
}
}
It is almost done! By default, I disable the statistics information. To enable the information, There are two options:
- using the code to enable the statistics:
CostResult.getInstance().startRecord();
and using the code to disable the staticstics again:
CostResult.getInstance().stopRecord();
- using JMX (e.g., jconsole), the MBean is
performance.CostResult
, you can clickstartRecord()
to enable it and clickstopRecord()
to disable the information.
Now it is time to see the result:
If you want to know the statistics information, just open jconsole and find performance.CostResult
MBean, these functions are useful:
-
getTimes(className, methodName)
, return the times that the method is called. Notice that, suppose your class iscn.edu.tsinghua.hxd.Test
, the the className iscn/edu/tsinghua/hxd/Test
. -
getTotalTimeCost(className, methodName)
, return the total time cost that the method spends. Notice that, suppose your class iscn.edu.tsinghua.hxd.Test
, the the className iscn/edu/tsinghua/hxd/Test
. -
sortTimes2String()
return the called times of all the functions that you are interested. The result is sorted. -
sortTimeCosts2String()
return the total time costs for each functions that you are interested. The result is sorted. -
sortTimes
, similar withsortTimes2String()
while the result isList<IntPair>
, you can only call this function by coding rather than jconsole. -
sortTimeCosts2String()
, similar withsortTimeCosts2String()
while the result isList<LongPair>
, you can only call this function by coding rather than jconsole. -
startRecord()
, enable the record. -
stopRecord()
, disable the record and clear the previous results.