Skip to content

Commit

Permalink
PPC: unit: test sortTopology for expected output
Browse files Browse the repository at this point in the history
The node topology contains several slices that later on are sorted and
used to determine whether two nodes are equal. Add a unit test to verify
this sorting is done as expected.

Signed-off-by: Shereen Haj <[email protected]>
  • Loading branch information
shajmakh committed Nov 11, 2024
1 parent ada0313 commit a9abb69
Show file tree
Hide file tree
Showing 2 changed files with 171 additions and 1 deletion.
6 changes: 5 additions & 1 deletion pkg/performanceprofile/profilecreator/profilecreator.go
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,11 @@ func (ghwHandler GHWHandler) SortedTopology() (*topology.Info, error) {
if err != nil {
return nil, fmt.Errorf("can't obtain topology info from GHW snapshot: %v", err)
}
sortTopology(topologyInfo)
return topologyInfo, nil
}

func sortTopology(topologyInfo *topology.Info) {
sort.Slice(topologyInfo.Nodes, func(x, y int) bool {
return topologyInfo.Nodes[x].ID < topologyInfo.Nodes[y].ID
})
Expand All @@ -283,7 +288,6 @@ func (ghwHandler GHWHandler) SortedTopology() (*topology.Info, error) {
return node.Cores[i].LogicalProcessors[0] < node.Cores[j].LogicalProcessors[0]
})
}
return topologyInfo, nil
}

// topologyHTDisabled returns topologyinfo in case Hyperthreading needs to be disabled.
Expand Down
166 changes: 166 additions & 0 deletions pkg/performanceprofile/profilecreator/profilecreatorfuncs_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
package profilecreator

import (
"reflect"
"testing"

"github.com/jaypipes/ghw/pkg/cpu"
"github.com/jaypipes/ghw/pkg/topology"
)

func TestSortTopology(t *testing.T) {
testcases := []struct {
name string
topoInfo topology.Info
expected topology.Info
}{
{
name: "unsorted",
topoInfo: topology.Info{
Nodes: []*topology.Node{
{
ID: 2,
Cores: []*cpu.ProcessorCore{
{
ID: 0,
LogicalProcessors: []int{2, 0, 1},
},
{
ID: 3,
LogicalProcessors: []int{11, 9, 10},
},
{
ID: 2,
LogicalProcessors: []int{8, 7, 6},
},
{
ID: 1,
LogicalProcessors: []int{3, 5, 4},
},
},
},
{
ID: 1,
Cores: []*cpu.ProcessorCore{

{
ID: 2,
LogicalProcessors: []int{8, 7, 6},
},
{
ID: 3,
LogicalProcessors: []int{11, 9, 10},
},
{
ID: 0,
LogicalProcessors: []int{2, 0, 1},
},
{
ID: 1,
LogicalProcessors: []int{3, 5, 4},
},
},
},
{
ID: 0,
Cores: []*cpu.ProcessorCore{
{
ID: 1,
LogicalProcessors: []int{3, 5, 4},
},
{
ID: 0,
LogicalProcessors: []int{2, 0, 1},
},
{
ID: 3,
LogicalProcessors: []int{11, 9, 10},
},
{
ID: 2,
LogicalProcessors: []int{8, 7, 6},
},
},
},
},
},
expected: topology.Info{
Nodes: []*topology.Node{
{
ID: 0,
Cores: []*cpu.ProcessorCore{
{
ID: 0,
LogicalProcessors: []int{0, 1, 2},
},
{
ID: 1,
LogicalProcessors: []int{3, 4, 5},
},
{
ID: 2,
LogicalProcessors: []int{6, 7, 8},
},
{
ID: 3,
LogicalProcessors: []int{9, 10, 11},
},
},
},
{
ID: 1,
Cores: []*cpu.ProcessorCore{
{
ID: 0,
LogicalProcessors: []int{0, 1, 2},
},
{
ID: 1,
LogicalProcessors: []int{3, 4, 5},
},
{
ID: 2,
LogicalProcessors: []int{6, 7, 8},
},
{
ID: 3,
LogicalProcessors: []int{9, 10, 11},
},
},
},
{
ID: 2,
Cores: []*cpu.ProcessorCore{
{
ID: 0,
LogicalProcessors: []int{0, 1, 2},
},
{
ID: 1,
LogicalProcessors: []int{3, 4, 5},
},
{
ID: 2,
LogicalProcessors: []int{6, 7, 8},
},
{
ID: 3,
LogicalProcessors: []int{9, 10, 11},
},
},
},
},
},
},
}

for _, tc := range testcases {
t.Run(tc.name, func(t *testing.T) {
sortTopology(&tc.topoInfo)
if !reflect.DeepEqual(tc.topoInfo, tc.expected) {
t.Errorf("expected %+v, got %+v", tc.expected, tc.topoInfo)
}

})
}
}

0 comments on commit a9abb69

Please sign in to comment.