Skip to content

Commit

Permalink
vipshop#134 fix case
Browse files Browse the repository at this point in the history
  • Loading branch information
calvin1978 committed Nov 12, 2018
1 parent 35b75d4 commit a38814a
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,18 @@ public static void start() {
localTimer.get().start();
}

/**
* 返回开始到现在的时间
*/
public static long duration() {
return localTimer.get().duration();
}

/**
* 记录结束时间
*/
public static long end() {
long duration = localTimer.get().end();
long duration = localTimer.get().duration();
localTimer.remove();
return duration;
}
Expand All @@ -51,11 +58,18 @@ public static void start(String key) {
getTimer(key).start();
}

/**
* 记录特定Timer的开始时间
*/
public static long duration(String key) {
return getTimer(key).duration();
}

/**
* 记录特定Timer结束时间,返回耗时
*/
public static long end(String key) {
long duration = getTimer(key).end();
long duration = getTimer(key).duration();
localTimerMap.get().remove(key);
return duration;
}
Expand Down Expand Up @@ -178,7 +192,7 @@ public void start() {
start = System.currentTimeMillis();
}

public long end() {
public long duration() {
return System.currentTimeMillis() - start;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,17 @@
import ch.qos.logback.classic.Logger;

public class PerformanceUtilsTest {

Logger logger = (Logger) LoggerFactory.getLogger(PerformanceUtilsTest.class);

@Test
public void test() throws InterruptedException {
PerformanceUtil.start();
PerformanceUtil.start("test");
Thread.sleep(1000L);// NOSONAR
System.out.println(Thread.currentThread().getName() + " time cost: " + PerformanceUtil.duration() + "ms");
PerformanceUtil.end();
PerformanceUtil.warn(logger, 0L);
PerformanceUtil.end("test");
System.out.println(Thread.currentThread().getName() + " time cost: " + PerformanceUtil.duration() + "ms");
PerformanceUtil.slowLog(logger, "test", 0L);

PerformanceUtil.endWithSlowLog(logger, 100L);
PerformanceUtil.endWithSlowLog(logger, "test", 100L);
}

}

0 comments on commit a38814a

Please sign in to comment.