|
| 1 | +// Copyright (c) 2017-2021 Uber Technologies Inc. |
| 2 | +// |
| 3 | +// Permission is hereby granted, free of charge, to any person obtaining a copy |
| 4 | +// of this software and associated documentation files (the "Software"), to deal |
| 5 | +// in the Software without restriction, including without limitation the rights |
| 6 | +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 7 | +// copies of the Software, and to permit persons to whom the Software is |
| 8 | +// furnished to do so, subject to the following conditions: |
| 9 | +// |
| 10 | +// The above copyright notice and this permission notice shall be included in |
| 11 | +// all copies or substantial portions of the Software. |
| 12 | +// |
| 13 | +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 14 | +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 15 | +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 16 | +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 17 | +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 18 | +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 19 | +// THE SOFTWARE. |
| 20 | + |
| 21 | +package autoscaler |
| 22 | + |
| 23 | +import "testing" |
| 24 | + |
| 25 | +func Test_linearRecommender_Recommend(t *testing.T) { |
| 26 | + type fields struct { |
| 27 | + lower ResourceUnit |
| 28 | + upper ResourceUnit |
| 29 | + targetUsages Usages |
| 30 | + } |
| 31 | + type args struct { |
| 32 | + currentResource ResourceUnit |
| 33 | + currentUsages Usages |
| 34 | + } |
| 35 | + |
| 36 | + defaultFields := fields{ |
| 37 | + lower: 5, |
| 38 | + upper: 15, |
| 39 | + targetUsages: map[UsageType]MilliUsage{ |
| 40 | + PollerUtilizationRate: 500, |
| 41 | + }, |
| 42 | + } |
| 43 | + |
| 44 | + tests := []struct { |
| 45 | + name string |
| 46 | + fields fields |
| 47 | + args args |
| 48 | + want ResourceUnit |
| 49 | + }{ |
| 50 | + { |
| 51 | + name: "on target usage", |
| 52 | + fields: defaultFields, |
| 53 | + args: args{ |
| 54 | + currentResource: 10, |
| 55 | + currentUsages: map[UsageType]MilliUsage{ |
| 56 | + PollerUtilizationRate: 500, |
| 57 | + }, |
| 58 | + }, |
| 59 | + want: ResourceUnit(10), |
| 60 | + }, |
| 61 | + { |
| 62 | + name: "under utilized, scale down", |
| 63 | + fields: defaultFields, |
| 64 | + args: args{ |
| 65 | + currentResource: 10, |
| 66 | + currentUsages: map[UsageType]MilliUsage{ |
| 67 | + PollerUtilizationRate: 400, |
| 68 | + }, |
| 69 | + }, |
| 70 | + want: ResourceUnit(8), |
| 71 | + }, |
| 72 | + { |
| 73 | + name: "under utilized, scale down but bounded", |
| 74 | + fields: defaultFields, |
| 75 | + args: args{ |
| 76 | + currentResource: 10, |
| 77 | + currentUsages: map[UsageType]MilliUsage{ |
| 78 | + PollerUtilizationRate: 200, |
| 79 | + }, |
| 80 | + }, |
| 81 | + want: ResourceUnit(5), |
| 82 | + }, |
| 83 | + { |
| 84 | + name: "zero utilization, scale down to min", |
| 85 | + fields: defaultFields, |
| 86 | + args: args{ |
| 87 | + currentResource: 10, |
| 88 | + currentUsages: map[UsageType]MilliUsage{ |
| 89 | + PollerUtilizationRate: 0, |
| 90 | + }, |
| 91 | + }, |
| 92 | + want: ResourceUnit(5), |
| 93 | + }, |
| 94 | + { |
| 95 | + name: "over utilized, scale up", |
| 96 | + fields: defaultFields, |
| 97 | + args: args{ |
| 98 | + currentResource: 10, |
| 99 | + currentUsages: map[UsageType]MilliUsage{ |
| 100 | + PollerUtilizationRate: 600, |
| 101 | + }, |
| 102 | + }, |
| 103 | + want: ResourceUnit(12), |
| 104 | + }, |
| 105 | + { |
| 106 | + name: "over utilized, scale up but bounded", |
| 107 | + fields: defaultFields, |
| 108 | + args: args{ |
| 109 | + currentResource: 10, |
| 110 | + currentUsages: map[UsageType]MilliUsage{ |
| 111 | + PollerUtilizationRate: 1000, |
| 112 | + }, |
| 113 | + }, |
| 114 | + want: ResourceUnit(15), |
| 115 | + }, |
| 116 | + } |
| 117 | + for _, tt := range tests { |
| 118 | + t.Run(tt.name, func(t *testing.T) { |
| 119 | + l := NewLinearRecommender(tt.fields.lower, tt.fields.upper, tt.fields.targetUsages) |
| 120 | + if got := l.Recommend(tt.args.currentResource, tt.args.currentUsages); got != tt.want { |
| 121 | + t.Errorf("Recommend() = %v, want %v", got, tt.want) |
| 122 | + } |
| 123 | + }) |
| 124 | + } |
| 125 | +} |
0 commit comments