写代码过程中,我们经常需要记录接口调用的时间,以便进行调优。之前我的做法是利用java相应时间处理类计算出时间。这样下来代码写得十分臃肿,今天偶然得知apache common lang包中有一个十分方便的类可以完成该功能。下面贴出使用方法:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| private static void test01() throws InterruptedException { StopWatch watch = new StopWatch() watch.start() Thread.sleep(1000) watch.split() * This is the time between start and latest lit. * 调用start()方法到最后一次调用split()耗用的时间 */ System.out.println(watch.getSplitTime()) Thread.sleep(2000) watch.split() System.out.println(watch.getSplitTime()) Thread.sleep(500) watch.stop() * This is either the time between the start and the moment this method * is called, or the amount of time between start and stop * 调用start()方法到调用getTime()或stop()方法耗用的时间 */ System.out.println(watch.getTime()) }
|
start —记录起始时间
split —记录调用此方法的时间