Repository navigation
Expand file tree
/
Copy pathbench_published_test.go
More file actions
147 lines (116 loc) · 3.82 KB
/
Copy pathbench_published_test.go
File metadata and controls
147 lines (116 loc) · 3.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
package boxpacker3_test
import (
"fmt"
"testing"
"github.com/bavix/boxpacker3/v2"
)
func qualityStrategies() []struct {
name string
strategy boxpacker3.RuleSettings
} {
return []struct {
name string
strategy boxpacker3.RuleSettings
}{
{nameFirstFitDecreasing, boxpacker3.RuleSettings{Order: boxpacker3.OrderDecreasing, Selection: boxpacker3.SelectFirstFit}},
{nameFirstFitIncreasing, boxpacker3.RuleSettings{Order: boxpacker3.OrderIncreasing, Selection: boxpacker3.SelectFirstFit}},
{nameBestFitIncreasing, boxpacker3.RuleSettings{Order: boxpacker3.OrderIncreasing, Selection: boxpacker3.SelectBestFit}},
{nameBestFitDecreasing, boxpacker3.RuleSettings{Order: boxpacker3.OrderDecreasing, Selection: boxpacker3.SelectBestFit}},
{nameNextFitIncreasing, boxpacker3.RuleSettings{Order: boxpacker3.OrderIncreasing, Selection: boxpacker3.SelectNextFit}},
{nameWorstFitIncreasing, boxpacker3.RuleSettings{Order: boxpacker3.OrderIncreasing, Selection: boxpacker3.SelectWorstFit}},
{nameAlmostWorstFitIncreasing, boxpacker3.RuleSettings{Order: boxpacker3.OrderIncreasing, Selection: boxpacker3.SelectAlmostWorstFit}},
}
}
const benchmarkProblems = 20
func BenchmarkPublished_Quality(b *testing.B) {
problems := loadPublishedInstances(b, fileBR1)
if len(problems) > benchmarkProblems {
problems = problems[:benchmarkProblems]
}
for _, entry := range qualityStrategies() {
b.Run(entry.name, func(b *testing.B) {
packer := rulePacker(entry.strategy.Order, entry.strategy.Selection)
var (
utilisation float64
leftBehind int
)
b.ReportAllocs()
for b.Loop() {
utilisation, leftBehind = 0, 0
for _, problem := range problems {
result, err := packer.Pack(b.Context(),
[]*boxpacker3.Box{problem.Box}, problem.Items)
if err != nil {
b.Fatal(err)
}
utilisation += volumeUtilisation(result.Boxes[0])
leftBehind += len(result.Unpacked)
}
}
b.StopTimer()
count := float64(len(problems))
b.ReportMetric(utilisation/count*100, "fill%")
b.ReportMetric(float64(leftBehind)/count, "left-behind/problem")
})
}
}
func BenchmarkPublished_Scaling(b *testing.B) {
for _, name := range []string{fileBR1, fileBR4, fileBR7} {
problems := loadPublishedInstances(b, name)
if len(problems) > benchmarkProblems {
problems = problems[:benchmarkProblems]
}
b.Run(name, func(b *testing.B) {
packer := boxpacker3.NewPacker()
var utilisation float64
b.ReportAllocs()
for b.Loop() {
utilisation = 0
for _, problem := range problems {
result, err := packer.Pack(b.Context(),
[]*boxpacker3.Box{problem.Box}, problem.Items)
if err != nil {
b.Fatal(err)
}
utilisation += volumeUtilisation(result.Boxes[0])
}
}
b.StopTimer()
b.ReportMetric(utilisation/float64(len(problems))*100, "fill%")
})
}
}
func BenchmarkSearchBudgets(b *testing.B) {
problems := loadPublishedInstances(b, fileBR1)
if len(problems) > benchmarkProblems {
problems = problems[:benchmarkProblems]
}
settings := []struct {
width int
branching int
}{{1, 1}, {2, 2}, {4, 3}, {8, 4}, {16, 6}}
for _, setting := range settings {
b.Run(fmt.Sprintf("w%d-k%d", setting.width, setting.branching), func(b *testing.B) {
search, err := boxpacker3.NewSearch(setting.width*32, setting.branching)
if err != nil {
b.Fatal(err)
}
packer := boxpacker3.NewPacker(boxpacker3.WithAlgorithm(search))
var utilisation float64
b.ReportAllocs()
for b.Loop() {
utilisation = 0
for _, problem := range problems {
result, packErr := packer.Pack(b.Context(),
[]*boxpacker3.Box{problem.Box}, problem.Items)
if packErr != nil {
b.Fatal(packErr)
}
utilisation += volumeUtilisation(result.Boxes[0])
}
}
b.StopTimer()
b.ReportMetric(utilisation/float64(len(problems))*100, "fill%")
})
}
}