Skip to content

Commit 96a1604

Browse files
committed
tests on the sampled/slot-filtered metrics
1 parent 286e8c3 commit 96a1604

File tree

3 files changed

+53
-3
lines changed

3 files changed

+53
-3
lines changed

src/StackExchange.Redis/HotKeys.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,11 @@ public sealed partial class HotKeysResult
143143

144144
private readonly SlotRange[]? _selectedSlots;
145145

146+
/// <summary>
147+
/// Gets whether slot filtering is in use.
148+
/// </summary>
149+
public bool IsSlotFiltered => TotalSelectedSlotsNetworkBytesRaw >= 0; // this key only present if slot-filtering active
150+
146151
/// <summary>
147152
/// The total CPU measured for all commands in all slots, without any sampling or filtering applied.
148153
/// </summary>

src/StackExchange.Redis/PublicAPI/PublicAPI.Unshipped.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
[SER003]StackExchange.Redis.HotKeysResult.CollectionStartTime.get -> System.DateTime
99
[SER003]StackExchange.Redis.HotKeysResult.CpuByKey.get -> System.ReadOnlySpan<StackExchange.Redis.HotKeysResult.MetricKeyCpu>
1010
[SER003]StackExchange.Redis.HotKeysResult.IsSampled.get -> bool
11+
[SER003]StackExchange.Redis.HotKeysResult.IsSlotFiltered.get -> bool
1112
[SER003]StackExchange.Redis.HotKeysResult.MetricKeyBytes
1213
[SER003]StackExchange.Redis.HotKeysResult.MetricKeyBytes.Bytes.get -> long
1314
[SER003]StackExchange.Redis.HotKeysResult.MetricKeyBytes.Key.get -> StackExchange.Redis.RedisKey

tests/StackExchange.Redis.Tests/HotKeysTests.cs

Lines changed: 47 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)