可能第一次你看到这个标题不懂在讲什么?其实很简单,就是介绍在 Spring 里 Controller 方法里进行调用A方法和在 Main 方法里进行调用同一个A方法的时间消耗对比!
Main 压测
/*** @author Lux Sun* @date 2020/11/4*/publicclassTest { publicstaticvoidmain(String[] args) { StopWatchsw=newStopWatch(); sw.start(); // 业务代码Asw.stop(); log.info("Main-Time: {}", sw.getTotalTimeMillis()); } }
Controller 压测
/*** @author Lux Sun* @date 2020/6/18*/publicclassExecController { "/test") (publicvoidtest() { StopWatchsw=newStopWatch(); sw.start(); // 业务代码Asw.stop(); log.info("Controller-Time: {}", sw.getTotalTimeMillis()); } }
结论
Controller-Time 比 Main-Time 要短,更接近真实环境压测时间;猜测因为 JVM 里面有代码的预加载或预编译的感觉,所以时间开销方面肯定是 Controller 更胜一筹,因为服务一直开着,而 Main 是每次手动从 0 开始启动的。