Skip to content

Commit 9e8d8d4

Browse files
Make subscription latency estimation in MTRDevice a bit less noisy.
Averages out the new latency with (lower weighted) previous ones.
1 parent f49e684 commit 9e8d8d4

File tree

1 file changed

+13
-1
lines changed

1 file changed

+13
-1
lines changed

src/darwin/Framework/CHIP/MTRDevice.mm

+13-1
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,13 @@ - (BOOL)isEqual:(id)object
306306
// happen more often than once every 10 minutes.
307307
#define MTRDEVICE_MIN_RESUBSCRIBE_DUE_TO_READ_INTERVAL_SECONDS (10 * 60)
308308

309+
// Weight of new data in determining subscription latencies. To avoid random
310+
// outliers causing too much noise in the value, treat an existing value (if
311+
// any) as having 2/3 weight and the new value as having 1/3 weight. These
312+
// weights are subject to change, if it's determined that different ones give
313+
// better behavior.
314+
#define MTRDEVICE_SUBSCRIPTION_LATENCY_NEW_VALUE_WEIGHT (1.0 / 3.0)
315+
309316
@interface MTRDevice ()
310317
@property (nonatomic, readonly) os_unfair_lock lock; // protects the caches and device state
311318
// protects against concurrent time updates by guarding timeUpdateScheduled flag which manages time updates scheduling,
@@ -1012,7 +1019,12 @@ - (void)_handleSubscriptionEstablished
10121019
// We want time interval from initialSubscribeStart to now, not the other
10131020
// way around.
10141021
NSTimeInterval subscriptionLatency = -[initialSubscribeStart timeIntervalSinceNow];
1015-
_estimatedSubscriptionLatency = @(subscriptionLatency);
1022+
if (_estimatedSubscriptionLatency == nil) {
1023+
_estimatedSubscriptionLatency = @(subscriptionLatency);
1024+
} else {
1025+
NSTimeInterval newSubscriptionLatencyEstimate = MTRDEVICE_SUBSCRIPTION_LATENCY_NEW_VALUE_WEIGHT * subscriptionLatency + (1 - MTRDEVICE_SUBSCRIPTION_LATENCY_NEW_VALUE_WEIGHT) * _estimatedSubscriptionLatency.doubleValue;
1026+
_estimatedSubscriptionLatency = @(newSubscriptionLatencyEstimate);
1027+
}
10161028
[self _storePersistedDeviceData];
10171029
}
10181030

0 commit comments

Comments
 (0)