Junjie
2026-04-13 2cb43c8bcc1f5b7e6807dfe16bc9e08109bf7833
#算法耗时优化
1个文件已修改
87 ■■■■■ 已修改文件
src/main/java/com/zy/common/utils/NavigateUtils.java 87 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/zy/common/utils/NavigateUtils.java
@@ -1651,15 +1651,25 @@
    }
    private StationPathRuntimeSnapshot loadStationPathRuntimeSnapshot(Integer currentTaskNo) {
        BaseRuntimeSnapshot baseRuntimeSnapshot = loadBaseRuntimeSnapshot();
        long totalStartNs = System.nanoTime();
        Map<String, Long> stepCostMap = new LinkedHashMap<>();
        long stepStartNs = System.nanoTime();
        BaseRuntimeSnapshot baseRuntimeSnapshot = loadBaseRuntimeSnapshot(stepCostMap);
        stepCostMap.put("loadBaseSnapshot", elapsedMillis(stepStartNs));
        if (baseRuntimeSnapshot == null) {
            logRuntimeSnapshotSlow(currentTaskNo, false, stepCostMap, totalStartNs);
            return StationPathRuntimeSnapshot.empty();
        }
        stepStartNs = System.nanoTime();
        StationTrafficSnapshot trafficSnapshot = buildStationTrafficSnapshot(
                baseRuntimeSnapshot.statusMap,
                currentTaskNo,
                baseRuntimeSnapshot.activeTraceList
        );
        stepCostMap.put("buildTrafficSnapshot", elapsedMillis(stepStartNs));
        logRuntimeSnapshotSlow(currentTaskNo, baseRuntimeSnapshot.cacheHit, stepCostMap, totalStartNs);
        return new StationPathRuntimeSnapshot(
                baseRuntimeSnapshot.statusMap,
                baseRuntimeSnapshot.stationLoopLoadMap,
@@ -1669,30 +1679,64 @@
        );
    }
    private BaseRuntimeSnapshot loadBaseRuntimeSnapshot() {
    private BaseRuntimeSnapshot loadBaseRuntimeSnapshot(Map<String, Long> stepCostMap) {
        long now = System.currentTimeMillis();
        CachedStationPathRuntimeSnapshot cachedSnapshot = cachedRuntimeSnapshot;
        if (cachedSnapshot != null && now - cachedSnapshot.cacheAtMs <= STATION_PATH_RUNTIME_SNAPSHOT_TTL_MS) {
            return cachedSnapshot.baseRuntimeSnapshot;
            if (stepCostMap != null) {
                stepCostMap.put("baseSnapshotCacheHit", 0L);
            }
            return cachedSnapshot.baseRuntimeSnapshot.toCacheHitSnapshot();
        }
        synchronized (runtimeSnapshotLock) {
            cachedSnapshot = cachedRuntimeSnapshot;
            if (cachedSnapshot != null && now - cachedSnapshot.cacheAtMs <= STATION_PATH_RUNTIME_SNAPSHOT_TTL_MS) {
                return cachedSnapshot.baseRuntimeSnapshot;
                if (stepCostMap != null) {
                    stepCostMap.put("baseSnapshotCacheHit", 0L);
                }
                return cachedSnapshot.baseRuntimeSnapshot.toCacheHitSnapshot();
            }
            BaseRuntimeSnapshot baseRuntimeSnapshot = buildBaseRuntimeSnapshot();
            long stepStartNs = System.nanoTime();
            BaseRuntimeSnapshot baseRuntimeSnapshot = buildBaseRuntimeSnapshot(stepCostMap);
            if (stepCostMap != null) {
                stepCostMap.put("buildBaseSnapshot", elapsedMillis(stepStartNs));
            }
            cachedRuntimeSnapshot = new CachedStationPathRuntimeSnapshot(now, baseRuntimeSnapshot);
            return baseRuntimeSnapshot;
        }
    }
    private BaseRuntimeSnapshot buildBaseRuntimeSnapshot() {
    private BaseRuntimeSnapshot buildBaseRuntimeSnapshot(Map<String, Long> stepCostMap) {
        long stepStartNs = System.nanoTime();
        Map<Integer, StationProtocol> statusMap = Collections.unmodifiableMap(new LinkedHashMap<>(loadStationStatusMap()));
        if (stepCostMap != null) {
            stepCostMap.put("loadStatusMap", elapsedMillis(stepStartNs));
        }
        stepStartNs = System.nanoTime();
        Map<Integer, Double> stationLoopLoadMap = Collections.unmodifiableMap(new LinkedHashMap<>(loadStationLoopLoadMap()));
        if (stepCostMap != null) {
            stepCostMap.put("loadLoopLoadMap", elapsedMillis(stepStartNs));
        }
        stepStartNs = System.nanoTime();
        LoopMergeGuardContext loopMergeGuardContext = loadLoopMergeGuardContext();
        if (stepCostMap != null) {
            stepCostMap.put("loadLoopMergeGuard", elapsedMillis(stepStartNs));
        }
        stepStartNs = System.nanoTime();
        Set<Integer> outStationIdSet = Collections.unmodifiableSet(new LinkedHashSet<>(loadAllOutStationIdSet()));
        if (stepCostMap != null) {
            stepCostMap.put("loadOutStationSet", elapsedMillis(stepStartNs));
        }
        stepStartNs = System.nanoTime();
        List<StationTaskTraceVo> activeTraceList = Collections.unmodifiableList(new ArrayList<>(loadPlanningActiveTraceList(statusMap)));
        return new BaseRuntimeSnapshot(statusMap, stationLoopLoadMap, loopMergeGuardContext, outStationIdSet, activeTraceList);
        if (stepCostMap != null) {
            stepCostMap.put("loadActiveTraceList", elapsedMillis(stepStartNs));
        }
        return new BaseRuntimeSnapshot(false, statusMap, stationLoopLoadMap, loopMergeGuardContext, outStationIdSet, activeTraceList);
    }
    private Map<Integer, Double> loadStationLoopLoadMap() {
@@ -2719,6 +2763,21 @@
                totalCostMs);
    }
    private void logRuntimeSnapshotSlow(Integer currentTaskNo,
                                        boolean cacheHit,
                                        Map<String, Long> stepCostMap,
                                        long totalStartNs) {
        long totalCostMs = elapsedMillis(totalStartNs);
        if (totalCostMs < STATION_PATH_SLOW_LOG_THRESHOLD_MS) {
            return;
        }
        News.warn("站点路径运行时快照加载耗时较长,taskNo={},cacheHit={},stepCosts={},totalCost={}ms",
                currentTaskNo,
                cacheHit,
                JSON.toJSONString(stepCostMap),
                totalCostMs);
    }
    private long elapsedMillis(long startNs) {
        long elapsedNs = System.nanoTime() - startNs;
        return elapsedNs <= 0L ? 0L : elapsedNs / 1_000_000L;
@@ -2921,23 +2980,35 @@
    }
    private static class BaseRuntimeSnapshot {
        private final boolean cacheHit;
        private final Map<Integer, StationProtocol> statusMap;
        private final Map<Integer, Double> stationLoopLoadMap;
        private final LoopMergeGuardContext loopMergeGuardContext;
        private final Set<Integer> outStationIdSet;
        private final List<StationTaskTraceVo> activeTraceList;
        private BaseRuntimeSnapshot(Map<Integer, StationProtocol> statusMap,
        private BaseRuntimeSnapshot(boolean cacheHit,
                                    Map<Integer, StationProtocol> statusMap,
                                    Map<Integer, Double> stationLoopLoadMap,
                                    LoopMergeGuardContext loopMergeGuardContext,
                                    Set<Integer> outStationIdSet,
                                    List<StationTaskTraceVo> activeTraceList) {
            this.cacheHit = cacheHit;
            this.statusMap = statusMap == null ? Collections.emptyMap() : statusMap;
            this.stationLoopLoadMap = stationLoopLoadMap == null ? Collections.emptyMap() : stationLoopLoadMap;
            this.loopMergeGuardContext = loopMergeGuardContext == null ? new LoopMergeGuardContext() : loopMergeGuardContext;
            this.outStationIdSet = outStationIdSet == null ? Collections.emptySet() : outStationIdSet;
            this.activeTraceList = activeTraceList == null ? Collections.emptyList() : activeTraceList;
        }
        private BaseRuntimeSnapshot toCacheHitSnapshot() {
            return new BaseRuntimeSnapshot(true,
                    statusMap,
                    stationLoopLoadMap,
                    loopMergeGuardContext,
                    outStationIdSet,
                    activeTraceList);
        }
    }
    private static class CachedStationPathRuntimeSnapshot {