Skip to content

Commit 2005be9

Browse files
Make subscription latency estimation in MTRDevice a bit less noisy. (project-chip#33717)
Averages out the new latency with (lower weighted) previous ones.
1 parent f91b256 commit 2005be9

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
@@ -308,6 +308,13 @@ - (BOOL)isEqual:(id)object
308308
// happen more often than once every 10 minutes.
309309
#define MTRDEVICE_MIN_RESUBSCRIBE_DUE_TO_READ_INTERVAL_SECONDS (10 * 60)
310310

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

0 commit comments

Comments
 (0)