浅谈StopWatch 分手后的思念是犯贱 2022-04-01 09:52 189阅读 0赞 spring提供的StopWatch很强大,J2EE我们通常用System.currentTimeMillis()进行测试代码执行性能 public void decrementCart(Cart cart) { long start = System.currentTimeMillis(); hashOps = this.redisTemplate.boundHashOps(redisKey); //判断 hashOps Object o = hashOps.get(loginUser.getId().toString()); if (o != null) { //转化为 cart 对象 Cart storeCart = JsonUtils.parse(o.toString(), Cart.class); storeCart.setNum(storeCart.getNum()-1); // hashOps.put(storeCart.getSkuId(), JsonUtils.serialize(storeCart)); } long end = System.currentTimeMillis(); System.out.println(end-start); } 当大量性能调优时候,显得尤为繁琐,spring提供了更优雅的方式 public void decrementCart(Cart cart) { StopWatch stopWatch = new StopWatch(); stopWatch.start(); hashOps = this.redisTemplate.boundHashOps(redisKey); //判断 hashOps Object o = hashOps.get(loginUser.getId().toString()); if (o != null) { //转化为 cart 对象 Cart storeCart = JsonUtils.parse(o.toString(), Cart.class); storeCart.setNum(storeCart.getNum()-1); // hashOps.put(storeCart.getSkuId(), JsonUtils.serialize(storeCart)); } stopWatch.stop(); } 多个代码段测试时候,使用start 、stop即可。
还没有评论,来说两句吧...