github.com/kubewharf/katalyst-core@v0.5.3/pkg/util/machine/topology_test.go (about)

     1  /*
     2  Copyright 2022 The Katalyst 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 machine
    18  
    19  import (
    20  	"reflect"
    21  	"testing"
    22  
    23  	"github.com/stretchr/testify/assert"
    24  	"k8s.io/apimachinery/pkg/util/sets"
    25  
    26  	"github.com/kubewharf/katalyst-core/pkg/config/agent/global"
    27  )
    28  
    29  func TestMemoryDetailsEqual(t *testing.T) {
    30  	t.Parallel()
    31  
    32  	tests := []struct {
    33  		name   string
    34  		detail MemoryDetails
    35  		want   MemoryDetails
    36  		equal  bool
    37  	}{
    38  		{
    39  			name:   "Equal Maps",
    40  			detail: MemoryDetails{1: 100, 2: 200},
    41  			want:   MemoryDetails{1: 100, 2: 200},
    42  			equal:  true,
    43  		},
    44  		{
    45  			name:   "Different Lengths",
    46  			detail: MemoryDetails{1: 100},
    47  			want:   MemoryDetails{1: 100, 2: 200},
    48  			equal:  false,
    49  		},
    50  		{
    51  			name:   "Different Values",
    52  			detail: MemoryDetails{1: 100, 2: 200},
    53  			want:   MemoryDetails{1: 100, 2: 300},
    54  			equal:  false,
    55  		},
    56  		{
    57  			name:   "Different Keys",
    58  			detail: MemoryDetails{1: 100, 3: 300},
    59  			want:   MemoryDetails{1: 100, 2: 300},
    60  			equal:  false,
    61  		},
    62  	}
    63  
    64  	for _, tt := range tests {
    65  		tt := tt
    66  		t.Run(tt.name, func(t *testing.T) {
    67  			t.Parallel()
    68  
    69  			if got := tt.detail.Equal(tt.want); got != tt.equal {
    70  				t.Errorf("MemoryDetails.Equal() = %v, want %v", got, tt.equal)
    71  			}
    72  		})
    73  	}
    74  }
    75  
    76  func TestMemoryDetailsClone(t *testing.T) {
    77  	t.Parallel()
    78  
    79  	tests := []struct {
    80  		name   string
    81  		detail MemoryDetails
    82  	}{
    83  		{
    84  			name:   "Empty Map",
    85  			detail: MemoryDetails{},
    86  		},
    87  		{
    88  			name:   "Single Element",
    89  			detail: MemoryDetails{1: 100},
    90  		},
    91  		{
    92  			name:   "Multiple Elements",
    93  			detail: MemoryDetails{1: 100, 2: 200, 3: 300},
    94  		},
    95  	}
    96  
    97  	for _, tt := range tests {
    98  		tt := tt
    99  		t.Run(tt.name, func(t *testing.T) {
   100  			t.Parallel()
   101  
   102  			cloned := tt.detail.Clone()
   103  
   104  			if !reflect.DeepEqual(cloned, tt.detail) {
   105  				t.Errorf("Clone() = %v, want %v", cloned, tt.detail)
   106  			}
   107  
   108  			// Ensure that the clone is a different instance
   109  			if &cloned == &tt.detail {
   110  				t.Errorf("Clone() returned the same instance, want different instances")
   111  			}
   112  		})
   113  	}
   114  }
   115  
   116  func TestMemoryDetailsFillNUMANodesWithZero(t *testing.T) {
   117  	t.Parallel()
   118  
   119  	// Define test cases for FillNUMANodesWithZero method
   120  	tests := []struct {
   121  		name     string
   122  		detail   MemoryDetails
   123  		allNUMAs CPUSet
   124  		expected MemoryDetails
   125  	}{
   126  		{
   127  			name:   "Existing NUMA Nodes",
   128  			detail: MemoryDetails{0: 100, 1: 200},
   129  			allNUMAs: CPUSet{
   130  				Initialed: true,
   131  				elems:     map[int]struct{}{0: {}, 1: {}, 2: {}},
   132  			},
   133  			expected: MemoryDetails{0: 100, 1: 200, 2: 0},
   134  		},
   135  		{
   136  			name:   "Empty NUMA Nodes",
   137  			detail: MemoryDetails{},
   138  			allNUMAs: CPUSet{
   139  				Initialed: true,
   140  				elems:     map[int]struct{}{0: {}},
   141  			},
   142  			expected: MemoryDetails{0: 0},
   143  		},
   144  		{
   145  			name:   "No Additional NUMA Nodes",
   146  			detail: MemoryDetails{0: 100, 1: 200},
   147  			allNUMAs: CPUSet{
   148  				Initialed: true,
   149  				elems:     map[int]struct{}{0: {}, 1: {}},
   150  			},
   151  			expected: MemoryDetails{0: 100, 1: 200},
   152  		},
   153  	}
   154  
   155  	// Run the test cases
   156  	for _, tt := range tests {
   157  		tt := tt
   158  		t.Run(tt.name, func(t *testing.T) {
   159  			t.Parallel()
   160  
   161  			// Call the FillNUMANodesWithZero method and compare the result with expected outcome
   162  			if got := tt.detail.FillNUMANodesWithZero(tt.allNUMAs); !reflect.DeepEqual(got, tt.expected) {
   163  				t.Errorf("MemoryDetails.FillNUMANodesWithZero() = %v, want %v", got, tt.expected)
   164  			}
   165  		})
   166  	}
   167  }
   168  
   169  func TestGetSiblingNumaInfo(t *testing.T) {
   170  	t.Parallel()
   171  
   172  	type args struct {
   173  		conf            *global.MachineInfoConfiguration
   174  		numaDistanceMap map[int][]NumaDistanceInfo
   175  	}
   176  	tests := []struct {
   177  		name string
   178  		args args
   179  		want *SiblingNumaInfo
   180  	}{
   181  		{
   182  			name: "test for without sibling",
   183  			args: args{
   184  				conf: &global.MachineInfoConfiguration{
   185  					SiblingNumaMemoryBandwidthAllocatableRate: 0.5,
   186  					SiblingNumaMemoryBandwidthCapacity:        10,
   187  				},
   188  				numaDistanceMap: map[int][]NumaDistanceInfo{
   189  					0: {
   190  						{
   191  							NumaID:   0,
   192  							Distance: 10,
   193  						},
   194  						{
   195  							NumaID:   1,
   196  							Distance: 32,
   197  						},
   198  					},
   199  					1: {
   200  						{
   201  							NumaID:   0,
   202  							Distance: 32,
   203  						},
   204  						{
   205  							NumaID:   1,
   206  							Distance: 10,
   207  						},
   208  					},
   209  				},
   210  			},
   211  			want: &SiblingNumaInfo{
   212  				SiblingNumaMap: map[int]sets.Int{
   213  					0: sets.NewInt(),
   214  					1: sets.NewInt(),
   215  				},
   216  				SiblingNumaAvgMBWCapacityMap: map[int]int64{
   217  					0: 10,
   218  					1: 10,
   219  				},
   220  				SiblingNumaAvgMBWAllocatableMap: map[int]int64{
   221  					0: 5,
   222  					1: 5,
   223  				},
   224  			},
   225  		},
   226  		{
   227  			name: "test for with sibling",
   228  			args: args{
   229  				conf: &global.MachineInfoConfiguration{
   230  					SiblingNumaMemoryBandwidthAllocatableRate: 0.8,
   231  					SiblingNumaMemoryBandwidthCapacity:        10,
   232  				},
   233  				numaDistanceMap: map[int][]NumaDistanceInfo{
   234  					0: {
   235  						{
   236  							NumaID:   0,
   237  							Distance: 10,
   238  						},
   239  						{
   240  							NumaID:   1,
   241  							Distance: 10,
   242  						},
   243  						{
   244  							NumaID:   2,
   245  							Distance: 32,
   246  						},
   247  						{
   248  							NumaID:   3,
   249  							Distance: 32,
   250  						},
   251  					},
   252  					1: {
   253  						{
   254  							NumaID:   0,
   255  							Distance: 10,
   256  						},
   257  						{
   258  							NumaID:   1,
   259  							Distance: 10,
   260  						},
   261  						{
   262  							NumaID:   2,
   263  							Distance: 32,
   264  						},
   265  						{
   266  							NumaID:   3,
   267  							Distance: 32,
   268  						},
   269  					},
   270  					2: {
   271  						{
   272  							NumaID:   0,
   273  							Distance: 32,
   274  						},
   275  						{
   276  							NumaID:   1,
   277  							Distance: 32,
   278  						},
   279  						{
   280  							NumaID:   2,
   281  							Distance: 10,
   282  						},
   283  						{
   284  							NumaID:   3,
   285  							Distance: 10,
   286  						},
   287  					},
   288  					3: {
   289  						{
   290  							NumaID:   0,
   291  							Distance: 32,
   292  						},
   293  						{
   294  							NumaID:   1,
   295  							Distance: 32,
   296  						},
   297  						{
   298  							NumaID:   2,
   299  							Distance: 10,
   300  						},
   301  						{
   302  							NumaID:   3,
   303  							Distance: 10,
   304  						},
   305  					},
   306  				},
   307  			},
   308  			want: &SiblingNumaInfo{
   309  				SiblingNumaMap: map[int]sets.Int{
   310  					0: sets.NewInt(1),
   311  					1: sets.NewInt(0),
   312  					2: sets.NewInt(3),
   313  					3: sets.NewInt(2),
   314  				},
   315  				SiblingNumaAvgMBWCapacityMap: map[int]int64{
   316  					0: 5,
   317  					1: 5,
   318  					2: 5,
   319  					3: 5,
   320  				},
   321  				SiblingNumaAvgMBWAllocatableMap: map[int]int64{
   322  					0: 4,
   323  					1: 4,
   324  					2: 4,
   325  					3: 4,
   326  				},
   327  			},
   328  		},
   329  	}
   330  	for _, tt := range tests {
   331  		tt := tt
   332  		t.Run(tt.name, func(t *testing.T) {
   333  			t.Parallel()
   334  			assert.Equalf(t, tt.want, GetSiblingNumaInfo(tt.args.conf, tt.args.numaDistanceMap), "GetSiblingNumaInfo(%v, %v)", tt.args.conf, tt.args.numaDistanceMap)
   335  		})
   336  	}
   337  }