@@ -8,15 +8,17 @@ public class HotKeysClusterTests(ITestOutputHelper output, SharedConnectionFixtu
88{
99 protected override string GetConfiguration ( ) => TestConfig . Current . ClusterServersAndPorts + ",connectTimeout=10000" ;
1010
11- [ Fact ]
12- public void CanUseClusterFilter ( )
11+ [ Theory ]
12+ [ InlineData ( true ) ]
13+ [ InlineData ( false ) ]
14+ public void CanUseClusterFilter ( bool sample )
1315 {
1416 var key = Me ( ) ;
1517 using var muxer = GetServer ( key , out var server ) ;
1618 Log ( $ "server: { Format . ToString ( server . EndPoint ) } , key: '{ key } '") ;
1719
1820 var slot = muxer . HashSlot ( key ) ;
19- server . HotKeysStart ( slots : [ ( short ) slot ] ) ;
21+ server . HotKeysStart ( slots : [ ( short ) slot ] , sampleRatio : sample ? 3 : 1 ) ;
2022
2123 var db = muxer . GetDatabase ( ) ;
2224 db . KeyDelete ( key , flags : CommandFlags . FireAndForget ) ;
@@ -28,6 +30,7 @@ public void CanUseClusterFilter()
2830 server . HotKeysStop ( ) ;
2931 var result = server . HotKeysGet ( ) ;
3032 Assert . NotNull ( result ) ;
33+ Assert . True ( result . IsSlotFiltered , nameof ( result . IsSlotFiltered ) ) ;
3134 var slots = result . SelectedSlots ;
3235 Assert . Equal ( 1 , slots . Length ) ;
3336 Assert . Equal ( slot , slots [ 0 ] . From ) ;
@@ -38,6 +41,23 @@ public void CanUseClusterFilter()
3841
3942 Assert . Equal ( 1 , result . NetworkBytesByKey . Length ) ;
4043 Assert . Equal ( key , result . NetworkBytesByKey [ 0 ] . Key ) ;
44+
45+ Assert . True ( result . TotalSelectedSlotsCpuTimeMicroseconds >= 0 , nameof ( result . TotalSelectedSlotsCpuTimeMicroseconds ) ) ;
46+ Assert . True ( result . TotalCpuTimeUserMicroseconds >= 0 , nameof ( result . TotalCpuTimeUserMicroseconds ) ) ;
47+
48+ Assert . Equal ( sample , result . IsSampled ) ;
49+ if ( sample )
50+ {
51+ Assert . Equal ( 3 , result . SampleRatio ) ;
52+ Assert . True ( result . TotalSampledSelectedSlotsCpuTimeMicroseconds >= 0 , nameof ( result . TotalSampledSelectedSlotsCpuTimeMicroseconds ) ) ;
53+ Assert . True ( result . TotalSampledSelectedSlotsNetworkBytesRaw >= 0 , nameof ( result . TotalSampledSelectedSlotsNetworkBytesRaw ) ) ;
54+ }
55+ else
56+ {
57+ Assert . Equal ( 1 , result . SampleRatio ) ;
58+ Assert . Equal ( - 1 , result . TotalSampledSelectedSlotsCpuTimeMicroseconds ) ;
59+ Assert . Equal ( - 1 , result . TotalSampledSelectedSlotsNetworkBytesRaw ) ;
60+ }
4161 }
4262}
4363
@@ -131,6 +151,8 @@ private void CheckSimpleWithKey(RedisKey key, HotKeysResult hotKeys, IServer ser
131151 Assert . True ( net . Bytes > 0 , nameof ( net . Bytes ) ) ;
132152
133153 Assert . Equal ( 1 , hotKeys . SampleRatio ) ;
154+ Assert . False ( hotKeys . IsSampled , nameof ( hotKeys . IsSampled ) ) ;
155+ Assert . False ( hotKeys . IsSlotFiltered , nameof ( hotKeys . IsSlotFiltered ) ) ;
134156
135157 if ( server . ServerType is ServerType . Cluster )
136158 {
@@ -234,4 +256,26 @@ public async Task MetricsChoiceAsync(HotKeysMetrics metrics)
234256 Assert . NotNull ( result ) ;
235257 Assert . Equal ( metrics , result . Metrics ) ;
236258 }
259+
260+ [ Fact ]
261+ public async Task SampleRatioUsageAsync ( )
262+ {
263+ RedisKey key = Me ( ) ;
264+ await using var muxer = GetServer ( key , out var server ) ;
265+ await server . HotKeysStartAsync ( sampleRatio : 3 ) ;
266+ var db = muxer . GetDatabase ( ) ;
267+ await db . KeyDeleteAsync ( key , flags : CommandFlags . FireAndForget ) ;
268+ for ( int i = 0 ; i < 20 ; i ++ )
269+ {
270+ await db . StringIncrementAsync ( key , flags : CommandFlags . FireAndForget ) ;
271+ }
272+
273+ await server . HotKeysStopAsync ( flags : CommandFlags . FireAndForget ) ;
274+ var result = await server . HotKeysGetAsync ( ) ;
275+ Assert . NotNull ( result ) ;
276+ Assert . True ( result . IsSampled , nameof ( result . IsSampled ) ) ;
277+ Assert . Equal ( 3 , result . SampleRatio ) ;
278+ Assert . NotEqual ( result . TotalProfiledNetworkBytes , result . TotalNetworkBytes ) ;
279+ Assert . NotEqual ( result . TotalProfiledCpuTime , result . TotalCpuTime ) ;
280+ }
237281}
0 commit comments