You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

63 lines
1.9 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. package com.gaotao.modules.schedule;
  2. import java.util.ArrayList;
  3. import java.util.List;
  4. /**
  5. * Collects create-split step timings on the current request thread and prints one summary.
  6. */
  7. public final class CreateSplitRollTiming {
  8. private static final ThreadLocal<Session> SESSION = new ThreadLocal<>();
  9. private CreateSplitRollTiming() {
  10. }
  11. public static void begin() {
  12. SESSION.set(new Session());
  13. }
  14. public static void record(String step, long elapsedMs) {
  15. Session session = SESSION.get();
  16. if (session == null || step == null) {
  17. return;
  18. }
  19. long cost = Math.max(elapsedMs, 0);
  20. session.steps.add(step + "=" + cost + "毫秒");
  21. session.stepTotal += cost;
  22. }
  23. public static String summary(String api, long totalMs, String context, String failedMessage) {
  24. Session session = SESSION.get();
  25. long cost = Math.max(totalMs, 0);
  26. StringBuilder sb = new StringBuilder();
  27. sb.append("[创建分卷] 汇总 接口=").append(api)
  28. .append(" 总耗时=").append(cost).append("毫秒");
  29. if (context != null && !context.isEmpty()) {
  30. sb.append(" ").append(context);
  31. }
  32. if (session != null && !session.steps.isEmpty()) {
  33. long otherMs = cost - session.stepTotal;
  34. if (otherMs < 0) {
  35. otherMs = 0;
  36. }
  37. sb.append(" 提交及间隙=").append(otherMs).append("毫秒");
  38. for (String step : session.steps) {
  39. sb.append(" | ").append(step);
  40. }
  41. }
  42. if (failedMessage != null && !failedMessage.isEmpty()) {
  43. sb.append(" | 失败=").append(failedMessage);
  44. }
  45. return sb.toString();
  46. }
  47. public static void clear() {
  48. SESSION.remove();
  49. }
  50. private static final class Session {
  51. private final List<String> steps = new ArrayList<>();
  52. private long stepTotal;
  53. }
  54. }