-
Notifications
You must be signed in to change notification settings - Fork 23
/
benchmark.go
163 lines (127 loc) · 3.93 KB
/
benchmark.go
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
package suites
import (
"fmt"
"os"
"time"
"github.com/google/shlex"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/stackrox/collector/integration-tests/pkg/common"
"github.com/stackrox/collector/integration-tests/pkg/config"
)
type BenchmarkBaselineTestSuite struct {
BenchmarkTestSuiteBase
}
type BenchmarkCollectorTestSuite struct {
BenchmarkTestSuiteBase
}
type BenchmarkTestSuiteBase struct {
IntegrationTestSuiteBase
perfContainers []string
}
func (b *BenchmarkTestSuiteBase) StartPerfTools() {
benchmark_options := config.BenchmarksInfo()
perf := benchmark_options.PerfCommand
bpftrace := benchmark_options.BpftraceCommand
bcc := benchmark_options.BccCommand
skipInit := benchmark_options.SkipInit
if skipInit && (perf == "" && bpftrace == "" && bcc == "") {
fmt.Fprintf(os.Stderr, "COLLECTOR_SKIP_HEADERS_INIT set, but no performance tool requested - ignoring.")
return
}
if !skipInit && (perf != "" || bpftrace != "" || bcc != "") {
b.RunInitContainer()
}
image_store := config.Images()
if perf != "" {
perf_image := image_store.QaImageByKey("performance-perf")
b.StartPerfContainer("perf", perf_image, perf)
}
if bpftrace != "" {
bpftrace_image := image_store.QaImageByKey("performance-bpftrace")
b.StartPerfContainer("bpftrace", bpftrace_image, bpftrace)
}
if bcc != "" {
bcc_image := image_store.QaImageByKey("performance-bcc")
b.StartPerfContainer("bcc", bcc_image, bcc)
}
}
func (b *BenchmarkTestSuiteBase) StartPerfContainer(name string, image string, args string) {
argsList, err := shlex.Split(args)
require.NoError(b.T(), err)
b.startContainer(name, image, common.QuoteArgs(argsList)...)
}
func (b *BenchmarkTestSuiteBase) RunInitContainer() {
init_image := config.Images().QaImageByKey("performance-init")
cmd := []string{
"-v", "/lib/modules:/lib/modules",
"-v", "/etc/os-release:/etc/os-release",
"-v", "/etc/lsb-release:/etc/lsb-release",
"-v", "/usr/src:/usr/src",
"-v", "/boot:/boot",
init_image,
}
containerID, err := b.launchContainer("host-init", cmd...)
require.NoError(b.T(), err)
if finished, _ := b.waitForContainerToExit("host-init", containerID, 5*time.Second, 0); !finished {
logs, err := b.containerLogs("host-init")
if err == nil {
fmt.Println(logs)
}
assert.FailNow(b.T(), "Failed to initialize host for performance testing")
}
b.cleanupContainers(containerID)
}
func (b *BenchmarkTestSuiteBase) startContainer(name string, image string, args ...string) {
cmd := []string{
"--privileged",
"-v", "/sys:/sys",
"-v", "/usr/src:/usr/src",
"-v", "/lib/modules:/lib/modules",
// by mounting /tmp we can allow tools to write stats/logs to a local path
// for later processing
"-v", "/tmp:/tmp",
image,
}
cmd = append(cmd, args...)
containerID, err := b.launchContainer(name, cmd...)
require.NoError(b.T(), err)
b.perfContainers = append(b.perfContainers, containerID)
}
func (b *BenchmarkTestSuiteBase) StopPerfTools() {
b.stopContainers(b.perfContainers...)
for _, container := range b.perfContainers {
log, err := b.containerLogs(container)
require.NoError(b.T(), err)
fmt.Println(log)
}
b.removeContainers(b.perfContainers...)
b.perfContainers = nil
}
func (s *BenchmarkCollectorTestSuite) SetupSuite() {
s.RegisterCleanup("perf", "bcc", "bpftrace", "init")
s.StartContainerStats()
s.StartPerfTools()
s.StartCollector(false, nil)
}
func (s *BenchmarkCollectorTestSuite) TestBenchmarkCollector() {
s.RunCollectorBenchmark()
}
func (s *BenchmarkCollectorTestSuite) TearDownSuite() {
s.StopPerfTools()
s.StopCollector()
s.cleanupContainers("benchmark")
s.WritePerfResults()
}
func (s *BenchmarkBaselineTestSuite) SetupSuite() {
s.StartContainerStats()
s.StartPerfTools()
}
func (s *BenchmarkBaselineTestSuite) TestBenchmarkBaseline() {
s.RunCollectorBenchmark()
}
func (s *BenchmarkBaselineTestSuite) TearDownSuite() {
s.StopPerfTools()
s.cleanupContainers("benchmark")
s.WritePerfResults()
}