/// <summary> /// 方法帮助类 /// </summary> public class ActionHelper { /// <summary> /// 执行时间阀值 /// </summary> public static readonly uint ElapsedMillisecondsLimit = 100; /// <summary> /// 统一计算执行时间 /// </summary> /// <param name="action">执行方法</param> /// <returns></returns> public static uint ComputeActionCostMilliseconds(Action action) { var stopwatch = new System.Diagnostics.Stopwatch(); stopwatch.Start(); // 开始监视 action(); stopwatch.Stop(); // 停止监视 var timeSpan = stopwatch.Elapsed; // 获取总时间 return (uint)timeSpan.TotalMilliseconds; } /// <summary> /// 统一计算执行时间 /// </summary> /// <param name="action">执行方法</param> /// <returns>Result.Data为执行结果。Result.Message为所消费的毫秒数</returns> public static ActionElapseResult<T> ComputeFuctionCostMilliseconds<T>(Func<T> action) { var stopwatch = new System.Diagnostics.Stopwatch(); stopwatch.Start(); // 开始监视 var obj = action(); stopwatch.Stop(); // 停止监视 var timeSpan = stopwatch.Elapsed; // 获取总时间 return new ActionElapseResult<T>() { Data = obj, ElapsedMilliseconds = (uint)(timeSpan.TotalMilliseconds) }; } /// <summary> /// 统一计算执行时间并记录日志 /// </summary> /// <param name="action">执行方法</param> /// <param name="elapsedMillisecondsLimit">执行所消费的时间阀值(超过阀值则记录日志)</param> /// <param name="loggerAction">日志执行方法</param> /// <returns>Result.Data为执行结果。Result.Message为所消费的毫秒数</returns> public static T ComputeFuctionCostMilliseconds<T>(Func<T> action, uint elapsedMillisecondsLimit, Action<uint> loggerAction) { return ComputeFuctionCostMilliseconds(action, (ms) => (elapsedMillisecondsLimit == 0 || ms > elapsedMillisecondsLimit), loggerAction); } /// <summary> /// 统一计算执行时间并记录日志 /// </summary> /// <param name="action">执行方法</param> /// <param name="elapsedMillisecondsLimit">是否根据消费时间判断需要记录日志</param> /// <param name="loggerAction">日志执行方法</param> /// <returns>Result.Data为执行结果。Result.Message为所消费的毫秒数</returns> public static T ComputeFuctionCostMilliseconds<T>(Func<T> action, Func<uint,bool> elapsedMillisecondsLimit, Action<uint> loggerAction) { var result = ComputeFuctionCostMilliseconds(action); var ms = (uint)(result.ElapsedMilliseconds); if (loggerAction != null && elapsedMillisecondsLimit(ms)) { loggerAction(ms); } return result.Data; } }
java的写法
import rx.functions.Action0; import rx.functions.Action1; import rx.functions.Func0; import rx.functions.Func1; /** * RequestUtils帮忙类 * * @author zhangjy * @date 2017/10/19 */ public class ActionUtils { /** * .ctor */ private ActionUtils() { } /** * 获取当前执行方法消耗时间毫秒值 * * @param action 执行方法 * @return 毫秒值 */ public static long computeActionCostMilliseconds(Action0 action) { //记录开始时间 long startTime = System.currentTimeMillis(); //此处为你调用的方法 action.call(); //记录结束时间 long endTime = System.currentTimeMillis(); return endTime - startTime; } /** * 获取当前执行方法消耗时间毫秒值 * * @param action 执行方法 * @param elapsedMillisecondsLimit 消耗时间阀值(超过该值将执行loggerAction方法) * @param loggerAction 执行方法 * @return 执行方法的返回值 */ public static <T> T computeFuctionCostMilliseconds(Func0<T> action, int elapsedMillisecondsLimit, Action1<Long> loggerAction) { return computeFuctionCostMilliseconds(action, ms -> ms > elapsedMillisecondsLimit, loggerAction); } /** * 获取当前执行方法消耗时间毫秒值 * * @param action 执行方法 * @param elapsedMillisecondsLimitFunc 消耗时间阀值函数(该函数为True将执行loggerAction方法) * @param loggerAction 执行方法 * @return 执行方法的返回值 */ public static <T> T computeFuctionCostMilliseconds(Func0<T> action, Func1<Long, Boolean> elapsedMillisecondsLimitFunc, Action1<Long> loggerAction) { ActionElapseResult<T> result = computeFuctionCostMilliseconds(action); if (loggerAction != null && elapsedMillisecondsLimitFunc.call(result.getElapsedMilliseconds())) { loggerAction.call(result.getElapsedMilliseconds()); } return result.getData(); } /** * 获取当前执行方法消耗时间毫秒值 * * @param action 执行方法 * @return 消费毫秒值及返回结果 */ private static <T> ActionElapseResult<T> computeFuctionCostMilliseconds(Func0<T> action) { //记录开始时间 long startTime = System.currentTimeMillis(); //此处为你调用的方法 T data = action.call(); //记录结束时间 long endTime = System.currentTimeMillis(); return new ActionElapseResult<>(endTime - startTime, data); } private static class ActionElapseResult<T> { private long elapsedMilliseconds; private T data; private ActionElapseResult(long ms, T data) { this.data = data; this.elapsedMilliseconds = ms; } private long getElapsedMilliseconds() { return elapsedMilliseconds; } private T getData() { return data; } } }