前言
功能优化的重点是需要缩短业务中比较耗时的模块的处理时间,首先我们需要分析出各个模块的耗时时间,接下来才能有针对的去做优化。
Spring 框架提供了一个 StopWatch 类,它用于在开发过程中测量代码执行时间。通过 StopWatch,你可以方便地记录多个任务(或代码块)的执行时间,并可以对这些时间进行统计和分析。
一、java计时
1. 计时代码
package org.example.timer;
/**
* Create by zjg on 2024/4/21
*/
public class Timer {
public static void main(String[] args) {
system();
}
public static void system() {
long task1Start = System.currentTimeMillis();
// 开始一个任务
System.out.println("task1执行");
// 模拟任务执行
try {
Thread.sleep(500); // 休眠0.5秒
} catch (InterruptedException e) {
e.printStackTrace();
}
// 结束任务,并自动记录执行时间
System.out.println("task1结束");
long task1End = System.currentTimeMillis();
// 开始另一个任务
System.out.println("task2执行");
long task2Start = System.currentTimeMillis();
// 模拟任务执行
try {
Thread.sleep(1000); // 休眠1秒
} catch (InterruptedException e) {
e.printStackTrace();
}
// 结束任务,并自动记录执行时间
System.out.println("task2结束");
long task2End = System.currentTimeMillis();
// 打印所有任务的执行时间
long timer1 = task1End - task1Start;
long timer2 = task2End - task2Start;
System.out.println(String.format("任务1耗时:[%s]秒",timer1/1000));
System.out.println(String.format("任务1耗时:[%s]毫秒",timer1));
System.out.println(String.format("任务1耗时:[%s]秒",timer2/1000));
System.out.println(String.format("任务2耗时:[%s]毫秒",timer2));
}
}
2. 执行结果
task1执行
task1结束
task2执行
task2结束
任务1耗时:[0]秒
任务1耗时:[506]毫秒
任务1耗时:[1]秒
任务2耗时:[1007]毫秒
可以看出,计时时间单位是毫米,秒是通过运算得来的,不足1秒的值就是0秒,不够精确。
二、spring计时
1.计时代码
package org.example.timer;
import org.springframework.util.StopWatch;
import java.util.Arrays;
/**
* Create by zjg on 2024/4/21
*/
public class Timer {
public static void main(String[] args) {
stopWatch();
}
public static void stopWatch() {
StopWatch stopWatch = new StopWatch();
// 开始一个任务
System.out.println("task1执行");
stopWatch.start("task1");
// 模拟任务执行
try {
Thread.sleep(500); // 休眠0.5秒
} catch (InterruptedException e) {
e.printStackTrace();
}
// 结束任务,并自动记录执行时间
System.out.println("task1结束");
stopWatch.stop();
// 开始另一个任务
System.out.println("task2执行");
stopWatch.start("task2");
// 模拟任务执行
try {
Thread.sleep(1000); // 休眠1秒
} catch (InterruptedException e) {
e.printStackTrace();
}
// 结束任务,并自动记录执行时间
System.out.println("task2结束");
stopWatch.stop();
StopWatch.TaskInfo[] taskInfo = stopWatch.getTaskInfo();
Arrays.stream(taskInfo).toList().forEach((taskInfo1 -> {
System.out.println(String.format("任务[%s],耗时[%s]秒",taskInfo1.getTaskName(),taskInfo1.getTimeSeconds()));
System.out.println(String.format("任务[%s],耗时[%s]毫秒",taskInfo1.getTaskName(),taskInfo1.getTimeMillis()));
System.out.println(String.format("任务[%s],耗时[%s]纳秒",taskInfo1.getTaskName(),taskInfo1.getTimeNanos()));
}));
// 打印所有任务的执行时间
System.out.println(stopWatch.prettyPrint());
}
}
2.执行结果
task1执行
task1结束
task2执行
task2结束
任务[task1],耗时[0.5026385]秒
任务[task1],耗时[502]毫秒
任务[task1],耗时[502638500]纳秒
任务[task2],耗时[1.0009278]秒
任务[task2],耗时[1000]毫秒
任务[task2],耗时[1000927800]纳秒
StopWatch '': running time = 1503566300 ns
---------------------------------------------
ns % Task name
---------------------------------------------
502638500 033% task1
1000927800 067% task2
spring计时器为我们提供了秒、毫秒、纳秒三种时间单位,并帮我们列出了多个任务的耗时百分比情况,相对较友好。
总结
计时任务其实是个切面问题,我们需要分析、跟踪系统中大量接口的交易时间、平均时间、超时时间等等。