0
点赞
收藏
分享

微信扫一扫

Java - Controller 压测 VS Main 压测



可能第一次你看到这个标题不懂在讲什么?其实很简单,就是介绍在 Spring 里 Controller 方法里进行调用A方法和在 Main 方法里进行调用同一个A方法的时间消耗对比!



Main 压测

/**
* @author Lux Sun
* @date 2020/11/4
*/
@Slf4j
public class Test {

public static void main(String[] args) {
StopWatch sw = new StopWatch();
sw.start();
// 业务代码A
sw.stop();
log.info("Main-Time: {}", sw.getTotalTimeMillis());
}
}


Controller 压测

/**
* @author Lux Sun
* @date 2020/6/18
*/
@Slf4j
@RestController
public class ExecController {

@GetMapping("/test")
public void test() {
StopWatch sw = new StopWatch();
sw.start();
// 业务代码A
sw.stop();
log.info("Controller-Time: {}", sw.getTotalTimeMillis());
}
}


结论

Controller-Time 比 Main-Time 要短,更接近真实环境压测时间;猜测因为 JVM 里面有代码的预加载或预编译的感觉,所以时间开销方面肯定是 Controller 更胜一筹,因为服务一直开着,而 Main 是每次手动从 0 开始启动的。


举报

相关推荐

0 条评论