文章目录
1.代码分析
public class Person {
private Integer pId;
private String pName;
private Random random = new Random();
// 计算器
private static Object calculator = new Object();
public Person() {
}
public Person(Integer pId, String pName) {
this.pId = pId;
this.pName = pName;
}
// 做某些事情
public void doSomething() {
int thingType = random.nextInt(3);
int sum = 0;
switch (thingType) {
case 0: // 轻
sum = this.sum0();
break;
case 1: // 中
sum = this.sum1();
break;
case 2: // 重
sum = this.sum2();
break;
default:
break;
}
// System.out.println("thingType=" + thingType + " sum=" + sum);
}
// 轻活
private int sum0() {
int a = 1, b = 1;
int sum = a + b;
return sum;
}
// 中活
private int sum1() {
int sum = 0;
for (int i = 0; i < 1000; i++) {
sum += i;
sum += i - sum;
}
return sum;
}
// 重活
private int sum2() {
int sum = 0;
for (int i = 0; i < 100000; i++) {
sum += (i % 10);
sum += (sum % 10);
sum += (sum % 10);
}
return sum;
}
public static void main(String[] args) {
new Thread() {
@Override
public void run() {
Thread.currentThread().setName("子线程AAA");
while (true){
int pid = 0;
// 不停的创建对象,并打印他的名字
Person person = new Person(pid,"person_" + pid );
// 做某些事情
person.doSomething();
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
};
}.start();
}
2.CPU
CPU消耗的分布及时间(cpu时间或者运行时间); 方法的执行图; 方法的执行统计(最大,最小,平均运行时间等)
2.1 Call Tree 调用树
动态的,不断变化
把时间调快一下,触发更快,更快速的发现问题
2.2 Hot Spots 热点
动态的,不断变化
可以修改Hot spot options,来查看不同的概况
2.3 Call Graph 调用图
静态的
2.4 Method Statistics 方法统计
必须使用发行模式才可以
通过平均时间,就能知道是哪里耗了
2.5 Complexity Analysis 复杂度分析
一般用于测试多重循环瓶颈(略)