Skip to content

Commit a9abb69

Browse files
committed
PPC: unit: test sortTopology for expected output
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]>
1 parent ada0313 commit a9abb69

File tree

2 files changed

+171
-1
lines changed

2 files changed

+171
-1
lines changed

pkg/performanceprofile/profilecreator/profilecreator.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,11 @@ func (ghwHandler GHWHandler) SortedTopology() (*topology.Info, error) {
270270
if err != nil {
271271
return nil, fmt.Errorf("can't obtain topology info from GHW snapshot: %v", err)
272272
}
273+
sortTopology(topologyInfo)
274+
return topologyInfo, nil
275+
}
276+
277+
func sortTopology(topologyInfo *topology.Info) {
273278
sort.Slice(topologyInfo.Nodes, func(x, y int) bool {
274279
return topologyInfo.Nodes[x].ID < topologyInfo.Nodes[y].ID
275280
})
@@ -283,7 +288,6 @@ func (ghwHandler GHWHandler) SortedTopology() (*topology.Info, error) {
283288
return node.Cores[i].LogicalProcessors[0] < node.Cores[j].LogicalProcessors[0]
284289
})
285290
}
286-
return topologyInfo, nil
287291
}
288292

289293
// topologyHTDisabled returns topologyinfo in case Hyperthreading needs to be disabled.
Lines changed: 166 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,166 @@
1+
package profilecreator
2+
3+
import (
4+
"reflect"
5+
"testing"
6+
7+
"github.com/jaypipes/ghw/pkg/cpu"
8+
"github.com/jaypipes/ghw/pkg/topology"
9+
)
10+
11+
func TestSortTopology(t *testing.T) {
12+
testcases := []struct {
13+
name string
14+
topoInfo topology.Info
15+
expected topology.Info
16+
}{
17+
{
18+
name: "unsorted",
19+
topoInfo: topology.Info{
20+
Nodes: []*topology.Node{
21+
{
22+
ID: 2,
23+
Cores: []*cpu.ProcessorCore{
24+
{
25+
ID: 0,
26+
LogicalProcessors: []int{2, 0, 1},
27+
},
28+
{
29+
ID: 3,
30+
LogicalProcessors: []int{11, 9, 10},
31+
},
32+
{
33+
ID: 2,
34+
LogicalProcessors: []int{8, 7, 6},
35+
},
36+
{
37+
ID: 1,
38+
LogicalProcessors: []int{3, 5, 4},
39+
},
40+
},
41+
},
42+
{
43+
ID: 1,
44+
Cores: []*cpu.ProcessorCore{
45+
46+
{
47+
ID: 2,
48+
LogicalProcessors: []int{8, 7, 6},
49+
},
50+
{
51+
ID: 3,
52+
LogicalProcessors: []int{11, 9, 10},
53+
},
54+
{
55+
ID: 0,
56+
LogicalProcessors: []int{2, 0, 1},
57+
},
58+
{
59+
ID: 1,
60+
LogicalProcessors: []int{3, 5, 4},
61+
},
62+
},
63+
},
64+
{
65+
ID: 0,
66+
Cores: []*cpu.ProcessorCore{
67+
{
68+
ID: 1,
69+
LogicalProcessors: []int{3, 5, 4},
70+
},
71+
{
72+
ID: 0,
73+
LogicalProcessors: []int{2, 0, 1},
74+
},
75+
{
76+
ID: 3,
77+
LogicalProcessors: []int{11, 9, 10},
78+
},
79+
{
80+
ID: 2,
81+
LogicalProcessors: []int{8, 7, 6},
82+
},
83+
},
84+
},
85+
},
86+
},
87+
expected: topology.Info{
88+
Nodes: []*topology.Node{
89+
{
90+
ID: 0,
91+
Cores: []*cpu.ProcessorCore{
92+
{
93+
ID: 0,
94+
LogicalProcessors: []int{0, 1, 2},
95+
},
96+
{
97+
ID: 1,
98+
LogicalProcessors: []int{3, 4, 5},
99+
},
100+
{
101+
ID: 2,
102+
LogicalProcessors: []int{6, 7, 8},
103+
},
104+
{
105+
ID: 3,
106+
LogicalProcessors: []int{9, 10, 11},
107+
},
108+
},
109+
},
110+
{
111+
ID: 1,
112+
Cores: []*cpu.ProcessorCore{
113+
{
114+
ID: 0,
115+
LogicalProcessors: []int{0, 1, 2},
116+
},
117+
{
118+
ID: 1,
119+
LogicalProcessors: []int{3, 4, 5},
120+
},
121+
{
122+
ID: 2,
123+
LogicalProcessors: []int{6, 7, 8},
124+
},
125+
{
126+
ID: 3,
127+
LogicalProcessors: []int{9, 10, 11},
128+
},
129+
},
130+
},
131+
{
132+
ID: 2,
133+
Cores: []*cpu.ProcessorCore{
134+
{
135+
ID: 0,
136+
LogicalProcessors: []int{0, 1, 2},
137+
},
138+
{
139+
ID: 1,
140+
LogicalProcessors: []int{3, 4, 5},
141+
},
142+
{
143+
ID: 2,
144+
LogicalProcessors: []int{6, 7, 8},
145+
},
146+
{
147+
ID: 3,
148+
LogicalProcessors: []int{9, 10, 11},
149+
},
150+
},
151+
},
152+
},
153+
},
154+
},
155+
}
156+
157+
for _, tc := range testcases {
158+
t.Run(tc.name, func(t *testing.T) {
159+
sortTopology(&tc.topoInfo)
160+
if !reflect.DeepEqual(tc.topoInfo, tc.expected) {
161+
t.Errorf("expected %+v, got %+v", tc.expected, tc.topoInfo)
162+
}
163+
164+
})
165+
}
166+
}

0 commit comments

Comments
 (0)