|
| 1 | +// Copyright 2025 The Cockroach Authors. |
| 2 | +// |
| 3 | +// Use of this software is governed by the CockroachDB Software License |
| 4 | +// included in the /LICENSE file. |
| 5 | + |
| 6 | +package mmaprototype |
| 7 | + |
| 8 | +import ( |
| 9 | + "testing" |
| 10 | + |
| 11 | + "github.com/stretchr/testify/require" |
| 12 | +) |
| 13 | + |
| 14 | +func TestComputeMaxFractionPendingIncDec(t *testing.T) { |
| 15 | + tests := []struct { |
| 16 | + name string |
| 17 | + reported LoadVector |
| 18 | + adjusted LoadVector |
| 19 | + expectedMaxInc float64 |
| 20 | + expectedMaxDec float64 |
| 21 | + }{ |
| 22 | + { |
| 23 | + name: "both_zero", |
| 24 | + reported: LoadVector{}, |
| 25 | + adjusted: LoadVector{}, |
| 26 | + expectedMaxInc: 0, |
| 27 | + expectedMaxDec: 0, |
| 28 | + }, |
| 29 | + { |
| 30 | + name: "reported_zero_adjusted_nonzero", |
| 31 | + reported: LoadVector{}, |
| 32 | + adjusted: LoadVector{CPURate: 100, WriteBandwidth: 200, ByteSize: 300}, |
| 33 | + expectedMaxInc: 1000, |
| 34 | + expectedMaxDec: 1000, |
| 35 | + }, |
| 36 | + { |
| 37 | + name: "reported_nonzero_adjusted_zero", |
| 38 | + reported: LoadVector{CPURate: 100, WriteBandwidth: 200, ByteSize: 300}, |
| 39 | + adjusted: LoadVector{CPURate: 0, WriteBandwidth: 0, ByteSize: 0}, |
| 40 | + expectedMaxInc: 0, |
| 41 | + expectedMaxDec: 1.0, // abs((0-100)/100) = 1.0, same for all dimensions |
| 42 | + }, |
| 43 | + { |
| 44 | + name: "simple_increase", |
| 45 | + reported: LoadVector{CPURate: 100}, |
| 46 | + adjusted: LoadVector{CPURate: 150}, |
| 47 | + expectedMaxInc: 0.5, |
| 48 | + expectedMaxDec: 0, |
| 49 | + }, |
| 50 | + { |
| 51 | + name: "simple_decrease", |
| 52 | + reported: LoadVector{CPURate: 100}, |
| 53 | + adjusted: LoadVector{CPURate: 50}, |
| 54 | + expectedMaxInc: 0, |
| 55 | + expectedMaxDec: 0.5, |
| 56 | + }, |
| 57 | + { |
| 58 | + name: "multiple_dimensions_increase", |
| 59 | + reported: LoadVector{CPURate: 100, WriteBandwidth: 200, ByteSize: 300}, |
| 60 | + adjusted: LoadVector{CPURate: 150, WriteBandwidth: 250, ByteSize: 400}, |
| 61 | + expectedMaxInc: 0.5, // max(0.5, 0.25, 0.333...) |
| 62 | + expectedMaxDec: 0, |
| 63 | + }, |
| 64 | + { |
| 65 | + name: "multiple_dimensions_decrease", |
| 66 | + reported: LoadVector{CPURate: 100, WriteBandwidth: 200, ByteSize: 300}, |
| 67 | + adjusted: LoadVector{CPURate: 50, WriteBandwidth: 100, ByteSize: 150}, |
| 68 | + expectedMaxInc: 0, |
| 69 | + expectedMaxDec: 0.5, // max(0.5, 0.5, 0.5) |
| 70 | + }, |
| 71 | + { |
| 72 | + name: "mixed_increase_and_decrease", |
| 73 | + reported: LoadVector{CPURate: 100, WriteBandwidth: 200, ByteSize: 300}, |
| 74 | + adjusted: LoadVector{CPURate: 150, WriteBandwidth: 100, ByteSize: 300}, |
| 75 | + expectedMaxInc: 0.5, // from CPURate |
| 76 | + expectedMaxDec: 0.5, // from WriteBandwidth |
| 77 | + }, |
| 78 | + { |
| 79 | + name: "negative_adjusted_load", |
| 80 | + reported: LoadVector{CPURate: 100}, |
| 81 | + adjusted: LoadVector{CPURate: -50}, |
| 82 | + expectedMaxInc: 0, |
| 83 | + expectedMaxDec: 1.5, // abs(-50 - 100) / 100 = 1.5 |
| 84 | + }, |
| 85 | + { |
| 86 | + name: "fractional_change", |
| 87 | + reported: LoadVector{CPURate: 1000}, |
| 88 | + adjusted: LoadVector{CPURate: 1100}, |
| 89 | + expectedMaxInc: 0.1, |
| 90 | + expectedMaxDec: 0, |
| 91 | + }, |
| 92 | + { |
| 93 | + name: "max_across_dimensions", |
| 94 | + reported: LoadVector{CPURate: 100, WriteBandwidth: 200, ByteSize: 300}, |
| 95 | + adjusted: LoadVector{CPURate: 120, WriteBandwidth: 300, ByteSize: 450}, |
| 96 | + expectedMaxInc: 0.5, // max(0.2, 0.5, 0.5) |
| 97 | + expectedMaxDec: 0, |
| 98 | + }, |
| 99 | + { |
| 100 | + name: "one_dimension_zero_others_change", |
| 101 | + reported: LoadVector{CPURate: 0, WriteBandwidth: 100, ByteSize: 200}, |
| 102 | + adjusted: LoadVector{CPURate: 50, WriteBandwidth: 150, ByteSize: 100}, |
| 103 | + expectedMaxInc: 1000, // from CPURate (reported=0) |
| 104 | + expectedMaxDec: 1000, // from CPURate (reported=0) |
| 105 | + }, |
| 106 | + } |
| 107 | + |
| 108 | + for _, tt := range tests { |
| 109 | + t.Run(tt.name, func(t *testing.T) { |
| 110 | + maxInc, maxDec := computeMaxFractionPendingIncDec(tt.reported, tt.adjusted) |
| 111 | + require.InDelta(t, tt.expectedMaxInc, maxInc, 1e-9, "max fraction increase") |
| 112 | + require.InDelta(t, tt.expectedMaxDec, maxDec, 1e-9, "max fraction decrease") |
| 113 | + }) |
| 114 | + } |
| 115 | +} |
0 commit comments