Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

添加记录运行时间以便效率分析的工具PerformanceUtils #135

Merged
merged 1 commit into from
Nov 9, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,220 @@
package com.vip.vjtools.vjkit.logging;

import java.util.HashMap;
import java.util.Map;

import org.slf4j.Logger;

/**
* 效率管理工具
* @author william.liang
*
*/
public class PerformanceUtils {
private PerformanceUtils() {
}

private static ThreadLocal<Timer> localTimer = new ThreadLocal<>();
private static ThreadLocal<Map<String, Timer>> localTimerMap = new ThreadLocal<>();

/**
* 记录开始时间
*/
public static void start() {
getTimer().start();
}

/**
* 记录结束时间
*/
public static void end() {
getTimer().end();
}

/**
* 计算耗时
* @return
*/
public static long duration() {
return getTimer().duration();
}

/**
* 清除ThreadLocal
*/
public static void remove() {
localTimer.remove();
localTimerMap.remove();
}

/**
* 记录开始时间
* @param key
*/
public static void start(String key) {
getTimer(key).start();
}

/**
* 记录结束时间
* @param key
*/
public static void end(String key) {
getTimer(key).end();
}

/**
* 计算耗时
* @param key
* @return
*/
public static long duration(String key) {
return getTimer(key).duration();
}

/**
* 清除ThreadLocal
* @param key
*/
public static void remove(String key) {
getTimerMap().remove(key);
}

/**
* 当处理时间超过预定的阈值时发出警告信息
* @param log
* @param threshold 阈值(单位:ms)
*/
public static void warn(Logger log, long threshold) {
if (duration() > threshold) {
log.warn("[Performance Warning] json= msg=任务处理时长超过设定的阈值,总时长为{}ms", duration());
}
}

/**
* 当处理时间超过预定的阈值时发出警告信息
* @param log
* @param key
* @param threshold 阈值(单位:ms)
*/
public static void warn(Logger log, String key, long threshold) {
if (duration(key) > threshold) {
log.warn("[Performance Warning] json= msg=任务【{}】处理时长超过设定的阈值,总时长为{}ms", key, duration(key));
}
}

/**
* 当处理时间超过预定的阈值时发出警告信息
* @param log
* @param threshold 阈值(单位:ms)
* @param json 需要记录的对象json
*/
public static void warn(Logger log, long threshold, String json) {
if (duration() > threshold) {
log.warn("[Performance Warning] json={} msg=任务处理时长超过设定的阈值,总时长为{}ms", json, duration());
}
}

/**
* 当处理时间超过预定的阈值时发出警告信息
* @param log
* @param key
* @param threshold 阈值(单位:ms)
* @param json 需要记录的对象json
*/
public static void warn(Logger log, String key, long threshold, String json) {
if (duration(key) > threshold) {
log.warn("[Performance Warning] json={} msg=任务【{}】处理时长超过设定的阈值,总时长为{}ms", json, key, duration(key));
}
}

/**
* 记录结束时间并当处理时间超过预定的阈值时发出警告信息,最后清除
* @param log
* @param threshold 阈值(单位:ms)
*/
public static void endWithWarnAndRemove(Logger log, long threshold) {
end();
warn(log, threshold);
remove();
}

/**
* 记录结束时间并当处理时间超过预定的阈值时发出警告信息,最后清除
* @param log
* @param key
* @param threshold 阈值(单位:ms)
*/
public static void endWithWarnAndRemove(Logger log, String key, long threshold) {
end(key);
warn(log, key, threshold);
remove(key);
}

/**
* 记录结束时间并当处理时间超过预定的阈值时发出警告信息,最后清除
* @param log
* @param threshold 阈值(单位:ms)
* @param json 需要记录的对象json
*/
public static void endWithWarnAndRemove(Logger log, long threshold, String json) {
end();
warn(log, threshold, json);
remove();
}

/**
* 记录结束时间并当处理时间超过预定的阈值时发出警告信息,最后清除
* @param log
* @param key
* @param threshold 阈值(单位:ms)
* @param json 需要记录的对象json
*/
public static void endWithWarnAndRemove(Logger log, String key, long threshold, String json) {
end(key);
warn(log, key, threshold, json);
remove(key);
}

private static Timer getTimer(String key) {
Timer timer = getTimerMap().getOrDefault(key, new Timer());
getTimerMap().putIfAbsent(key, timer);
return timer;
}

private static Timer getTimer() {
Timer timer = localTimer.get();
if (timer == null) {
timer = new Timer();
localTimer.set(timer);
}
return timer;
}

private static Map<String, Timer> getTimerMap() {
Map<String, Timer> timerMap = localTimerMap.get();
if (timerMap == null) {
timerMap = new HashMap<>(16);
localTimerMap.set(timerMap);
}
return timerMap;
}

static class Timer {
private long start;
private long end;

public void start() {
start = System.currentTimeMillis();
}

public void end() {
end = System.currentTimeMillis();
}

public long duration() {
return end - start;
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.vip.vjtools.vjkit.logging;

import org.junit.Test;
import org.slf4j.LoggerFactory;

import ch.qos.logback.classic.Logger;

public class PerformanceUtilsTest {

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

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

}