springStopWatch

在写代码的时候我们经常会测试一段代码的执行时间,一般格式如下

  @RequestMapping(value = "/")
  public String hello() throws InterruptedException {
    long start = System.currentTimeMillis();
    Thread.sleep(100);
    long end = System.currentTimeMillis();
    System.out.println("执行耗时:" + (end - start));
    return "hello word";
  }

当我们有了SpringBoot以后我们完全可以这么做

  @RequestMapping(value = "/")
  public String hello() throws InterruptedException {
    StopWatch stopWatch = new StopWatch();
    stopWatch.start();
    Thread.sleep(100);
    stopWatch.stop();
    System.out.println(stopWatch.prettyPrint());
    System.out.println(stopWatch.getTotalTimeMillis());
    return "hello word";
  }