/** * * @author yanglk <br> * Create Time:2017-11-13 16:48:37<br> * <a href="mailto:yanglk2010@sina.com">yanglk2010@sina.com</a> * @since 1.0 * @version 1.0 */ public class TimingTools { private final static Logger logger = LoggerFactory.getLogger(TimingTools.class); private final static ThreadLocal<Map<String, Long>> timeMap = ThreadLocal.withInitial(()->new HashMap<>()); /** * 模擬js中console.time("log"),console.timeEnd("log"); * 結果顯示log:%dms * * @param name 輸入名稱 */ public static void time(String name) { if(validate(name)) return; getMap().put(name, System.currentTimeMillis()); } public static void timeEnd(String name) { try { if(validate(name)) return; logger.debug("{}:{}ms", name, (System.currentTimeMillis()-getMap().get(name))); }finally { getMap().remove(name); } } private static Map<String, Long> getMap(){ return timeMap.get(); } private static boolean validate(String name) { if(name==null||name.trim().length()<=0||getMap().get(name)==null) { logger.info("TimingTools.timeEnd 輸入參數[name]不能爲空"); return true; } return false; } }