perf(efficiency): 落实首批采样减负与发行包瘦身
This commit is contained in:
@@ -9,6 +9,7 @@ final class MetricsStore {
|
||||
private(set) var isPanelVisible = false
|
||||
let sampling: SamplingPreferences
|
||||
var effectiveMilliseconds: Int { isPanelVisible ? sampling.panelMilliseconds : sampling.backgroundMilliseconds }
|
||||
private var hasSamplingDemand: Bool { isPanelVisible || sampling.backgroundEnabled }
|
||||
@ObservationIgnored private var generation = 0
|
||||
@ObservationIgnored private var visibilityGeneration = 0
|
||||
@ObservationIgnored private var task: Task<Void, Never>?
|
||||
@@ -21,7 +22,8 @@ final class MetricsStore {
|
||||
self.sampler = sampler
|
||||
self.sampling = sampling ?? SamplingPreferences()
|
||||
self.sampling.onChange = { [weak self] in
|
||||
guard let self, !self.isPaused, self.effectiveMilliseconds != self.scheduledInterval else { return }
|
||||
guard let self, !self.isPaused,
|
||||
self.effectiveMilliseconds != self.scheduledInterval || self.hasSamplingDemand != (self.task != nil) else { return }
|
||||
self.reschedule(reset: false, sampleImmediately: false)
|
||||
}
|
||||
start()
|
||||
@@ -36,8 +38,10 @@ final class MetricsStore {
|
||||
private func reschedule(reset: Bool, sampleImmediately: Bool = true) {
|
||||
generation += 1
|
||||
task?.cancel()
|
||||
task = nil
|
||||
needsHistoryBoundary = needsHistoryBoundary || reset
|
||||
scheduledInterval = effectiveMilliseconds
|
||||
guard hasSamplingDemand else { needsHistoryBoundary = true; return }
|
||||
var delay = Duration.zero
|
||||
if !sampleImmediately, let lastPublishedAt {
|
||||
delay = max(.zero, .milliseconds(effectiveMilliseconds) - lastPublishedAt.duration(to: .now))
|
||||
@@ -51,8 +55,10 @@ final class MetricsStore {
|
||||
}
|
||||
if reset { await sampler.reset() }
|
||||
while !Task.isCancelled {
|
||||
let sampledAt = ContinuousClock.now
|
||||
let visibleGeneration = self?.visibilityGeneration
|
||||
var next = await sampler.sample(includeProcesses: self?.isPanelVisible == true, processUpdate: { [weak self] processes in
|
||||
var next = await sampler.sample(includeProcesses: self?.isPanelVisible == true,
|
||||
includeSlowMetrics: self?.isPanelVisible == true, processUpdate: { [weak self] processes in
|
||||
Task { @MainActor [weak self] in
|
||||
guard let self, self.visibilityGeneration == visibleGeneration,
|
||||
self.isPanelVisible, !self.isPaused else { return }
|
||||
@@ -71,7 +77,9 @@ final class MetricsStore {
|
||||
self?.needsHistoryBoundary = false
|
||||
SamplingDiagnostics.record(next)
|
||||
let interval = self?.effectiveMilliseconds ?? 5_000
|
||||
do { try await Task.sleep(for: .milliseconds(interval)) }
|
||||
// Start-to-start cadence; missed deadlines do not trigger catch-up bursts.
|
||||
let deadline = sampledAt.advanced(by: .milliseconds(interval))
|
||||
do { try await Task.sleep(until: max(deadline, .now), clock: .continuous) }
|
||||
catch { return }
|
||||
}
|
||||
}
|
||||
@@ -97,7 +105,7 @@ final class MetricsStore {
|
||||
if !visible { snapshot.processes = nil }
|
||||
let active = visible && !isPaused
|
||||
Task { await sampler.setProcessVisible(active) }
|
||||
if !isPaused { reschedule(reset: false) }
|
||||
if !isPaused { reschedule(reset: !sampling.backgroundEnabled) }
|
||||
}
|
||||
|
||||
deinit { task?.cancel() }
|
||||
|
||||
Reference in New Issue
Block a user