|
| 1 | +/* |
| 2 | +Copyright 2026 The llm-d Authors. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package latencyobserver |
| 18 | + |
| 19 | +import ( |
| 20 | + "context" |
| 21 | + "testing" |
| 22 | + "time" |
| 23 | + |
| 24 | + "github.com/stretchr/testify/assert" |
| 25 | + "github.com/stretchr/testify/require" |
| 26 | +) |
| 27 | + |
| 28 | +// testEndpointID is the namespaced name of the endpoint the tests observe. |
| 29 | +const testEndpointID = "default/a" |
| 30 | + |
| 31 | +// flushConfig is tuned for tests: a short bucket so the floor path runs, and a |
| 32 | +// low minRequests so a handful of observations is enough to be trusted. |
| 33 | +func flushConfig() Config { |
| 34 | + cfg := DefaultConfig |
| 35 | + cfg.WindowSize, cfg.MaxRequests, cfg.MinRequests = 64, 16, 4 |
| 36 | + cfg.BucketDuration, cfg.BucketHistorySize = "1s", 4 |
| 37 | + return cfg |
| 38 | +} |
| 39 | + |
| 40 | +// flushAt recomputes the endpoint's snapshot at a controlled time. Dispatch |
| 41 | +// reads time.Now(), so tests drive publish directly to steer the bucket clock. |
| 42 | +func flushAt(p *Observer, now time.Time) { |
| 43 | + p.publish(context.Background(), testEndpointID, p.stateFor(testEndpointID), now) |
| 44 | +} |
| 45 | + |
| 46 | +func TestResolveConfig(t *testing.T) { |
| 47 | + resolved, err := DefaultConfig.resolve() |
| 48 | + require.NoError(t, err) |
| 49 | + assert.Equal(t, time.Second, resolved.interval) |
| 50 | + assert.Equal(t, 100, resolved.maxRequests) |
| 51 | + assert.InDelta(t, 0.25, resolved.lowPercentile, 1e-9, "percentiles resolve to fractions") |
| 52 | + |
| 53 | + invalid := map[string]func(*Config){ |
| 54 | + "zero window": func(c *Config) { c.WindowSize = 0 }, |
| 55 | + "maxRequests above window": func(c *Config) { c.MaxRequests = c.WindowSize + 1 }, |
| 56 | + "zero minRequests": func(c *Config) { c.MinRequests = 0 }, |
| 57 | + "bucket history below two": func(c *Config) { c.BucketHistorySize = 1 }, |
| 58 | + "percentiles inverted": func(c *Config) { c.LowPercentile, c.TypicalPercentile = 50, 25 }, |
| 59 | + "unparsable duration": func(c *Config) { c.IntervalDuration = "soon" }, |
| 60 | + "non-positive duration": func(c *Config) { c.BucketDuration = "-1m" }, |
| 61 | + } |
| 62 | + for name, mutate := range invalid { |
| 63 | + t.Run(name, func(t *testing.T) { |
| 64 | + cfg := DefaultConfig |
| 65 | + mutate(&cfg) |
| 66 | + _, err := cfg.resolve() |
| 67 | + require.Error(t, err) |
| 68 | + }) |
| 69 | + } |
| 70 | +} |
| 71 | + |
| 72 | +func TestFlush(t *testing.T) { |
| 73 | + now := time.Now() |
| 74 | + |
| 75 | + t.Run("publishes the load anchors from the short window", func(t *testing.T) { |
| 76 | + p := newObserverWithConfig(t, flushConfig()) |
| 77 | + // TTFT rises with the in-flight count it was dispatched at, which is the |
| 78 | + // relationship the scorer's curve captures. |
| 79 | + for i := range 10 { |
| 80 | + p.record(testEndpointID, 0.1+float64(i)*0.1, int64(i), now.Add(time.Duration(i)*time.Millisecond)) |
| 81 | + } |
| 82 | + |
| 83 | + flushAt(p, now.Add(time.Second)) |
| 84 | + |
| 85 | + s := p.stateFor(testEndpointID).published.Load() |
| 86 | + require.NotNil(t, s) |
| 87 | + assert.Equal(t, 10, s.RecentRequestCount) |
| 88 | + assert.Equal(t, 4, s.CalibrationThreshold) |
| 89 | + assert.Less(t, s.LowLoadTTFT, s.TypicalLoadTTFT, "P25 must sit below P50") |
| 90 | + assert.Less(t, s.InflightAtLowLoad, s.InflightAtTypicalLoad, "the faster band must be less loaded") |
| 91 | + }) |
| 92 | + |
| 93 | + t.Run("the floor arrives only once a bucket closes", func(t *testing.T) { |
| 94 | + p := newObserverWithConfig(t, flushConfig()) |
| 95 | + for i := range 10 { |
| 96 | + ttft := 1.0 |
| 97 | + if i < 2 { |
| 98 | + ttft = 0.1 // a fast tail the floor must follow |
| 99 | + } |
| 100 | + p.record(testEndpointID, ttft, 1, now.Add(time.Duration(i)*time.Millisecond)) |
| 101 | + } |
| 102 | + |
| 103 | + flushAt(p, now) // starts the bucket clock |
| 104 | + assert.Zero(t, p.stateFor(testEndpointID).published.Load().FloorTTFT) |
| 105 | + |
| 106 | + // One bucket later exactly: the bucket window looks back bucketDuration, |
| 107 | + // so flushing further out would close an empty bucket. |
| 108 | + flushAt(p, now.Add(time.Second)) |
| 109 | + s := p.stateFor(testEndpointID).published.Load() |
| 110 | + assert.Greater(t, s.FloorTTFT, 0.0) |
| 111 | + assert.Less(t, s.FloorTTFT, 1.0, "tracks the fast requests, not the bulk") |
| 112 | + }) |
| 113 | + |
| 114 | + t.Run("Floor withholds the value below minRequests", func(t *testing.T) { |
| 115 | + p := newObserverWithConfig(t, flushConfig()) // minRequests 4 |
| 116 | + for i := range 2 { |
| 117 | + p.record(testEndpointID, 0.1, 1, now.Add(time.Duration(i)*time.Millisecond)) |
| 118 | + } |
| 119 | + flushAt(p, now) |
| 120 | + flushAt(p, now.Add(time.Second)) |
| 121 | + |
| 122 | + s := p.stateFor(testEndpointID).published.Load() |
| 123 | + assert.Greater(t, s.FloorTTFT, 0.0, "the raw floor is computed") |
| 124 | + assert.Zero(t, s.Floor(), "but Floor() withholds it") |
| 125 | + }) |
| 126 | +} |
| 127 | + |
| 128 | +// The datalayer drives the recompute: one Dispatch per endpoint publishes that |
| 129 | +// endpoint's snapshot, and the plugin accepts no extractors. |
| 130 | +func TestDispatch(t *testing.T) { |
| 131 | + ctx := context.Background() |
| 132 | + p := newObserverWithConfig(t, flushConfig()) |
| 133 | + p.record(testEndpointID, 0.3, 1, time.Now()) |
| 134 | + |
| 135 | + require.NoError(t, p.Dispatch(ctx, newDataEndpoint())) |
| 136 | + assert.NotNil(t, p.stateFor(testEndpointID).published.Load()) |
| 137 | + assert.Equal(t, time.Second, p.Interval(), "the configured interval is the dispatch cadence") |
| 138 | + require.NoError(t, p.Dispatch(ctx, nil), "a nil endpoint is a no-op") |
| 139 | + assert.Error(t, p.AppendExtractor(p), "this dispatcher sources nothing") |
| 140 | +} |
0 commit comments