-
Notifications
You must be signed in to change notification settings - Fork 155
/
Copy pathmain.go
148 lines (129 loc) · 4.56 KB
/
main.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
package main
import (
"fmt"
"os"
"path"
"path/filepath"
"strings"
"time"
obs "github.com/openshift/cluster-logging-operator/api/observability/v1"
"github.com/openshift/cluster-logging-operator/internal/constants"
"github.com/openshift/cluster-logging-operator/test/helpers/oc"
"github.com/openshift/cluster-logging-operator/internal/cmd/functional-benchmarker/config"
"github.com/openshift/cluster-logging-operator/internal/cmd/functional-benchmarker/reports"
"github.com/openshift/cluster-logging-operator/internal/cmd/functional-benchmarker/runners"
"github.com/openshift/cluster-logging-operator/internal/cmd/functional-benchmarker/stats"
"github.com/openshift/cluster-logging-operator/internal/utils"
)
var (
log = utils.InitLogger("functional-benchmarker")
)
// HACK - This command is for development use only
func main() {
utils.InitLogger("functional-benchmarker")
defer func() {
log.Info("Test complete")
}()
options := config.InitOptions()
options.CollectorConfig = config.ReadConfig(options.CollectorConfigPath, options.BaseLine)
log.V(1).Info(options.CollectorConfig)
artifactDir := createArtifactDir(options.ArtifactDir)
if options.ArtifactDir != artifactDir {
options.ArtifactDir = artifactDir
}
metrics, statistics, config, err := RunBenchmark(artifactDir, options)
if err != nil {
log.Error(err, "Error in run")
os.Exit(1)
}
options.CollectorConfig = config
reporter := reports.NewReporter(options, artifactDir, metrics, statistics)
log.Info("Generating reports...")
reporter.Generate()
}
func RunBenchmark(artifactDir string, options config.Options) (*stats.ResourceMetrics, *stats.Statistics, string, error) {
runDuration := config.MustParseDuration(options.RunDuration, "run-duration")
sampleDuration := config.MustParseDuration(options.SampleDuration, "resource-sample-duration")
runner := runners.NewRunner(options)
log.Info("Deploying collector, loaders, and receivers")
runner.Deploy()
if options.DoCleanup {
log.V(2).Info("Deferring cleanup", "DoCleanup", options.DoCleanup)
defer runner.Cleanup()
}
done := make(chan bool)
startTime := time.Now()
sampler := time.NewTicker(sampleDuration)
metrics := stats.NewResourceMetrics()
log.V(0).Info("Starting to sample metrics for the collector...")
go func() {
for {
select {
case <-done:
return
default:
{
log.V(3).Info("Collecting Sample")
metrics.AddSample(runner.SampleCollector())
}
}
}
}()
time.Sleep(runDuration)
endTime := time.Now()
done <- true
sampler.Stop()
log.Info("Stopped sampling metrics")
log.Info("Fetching log data from receiver...")
if err := runner.FetchApplicationLogs(); err != nil {
return nil, nil, "", err
}
statistics := gatherStatistics(runner, options.Sample, options.MsgSize, startTime, endTime)
fetchContainerLogs(runner, artifactDir)
return metrics, statistics, runner.Config(), nil
}
func fetchContainerLogs(runner runners.Runner, artifactDir string) {
var err error
var out string
for _, container := range []string{constants.CollectorName, string(obs.OutputTypeHTTP)} {
if out, err = oc.Logs().WithNamespace(runner.Namespace()).WithPod(runner.Pod()).WithContainer(strings.ToLower(container)).Run(); err == nil {
/* #nosec G306*/
err = os.WriteFile(path.Join(artifactDir, container+".logs"), []byte(out), 0755)
if err != nil {
log.Error(err, "Error writing collector logs to artifactDir")
}
} else {
log.Error(err, "Error retrieving collector logs from container", "container")
}
}
}
func gatherStatistics(runner runners.Runner, sample bool, msgSize int, startTime, endTime time.Time) *stats.Statistics {
log.Info("Evaluating log data to calculate statistics")
logs, err := runner.ReadApplicationLogs()
if err != nil {
log.Error(err, "Error reading logs")
return &stats.Statistics{}
}
log.V(4).Info("Parsed logs", "parsed", logs)
return stats.NewStatisics(logs, msgSize, endTime.Sub(startTime))
}
func createArtifactDir(artifactDir string) string {
if strings.TrimSpace(artifactDir) == "" {
artifactDir = fmt.Sprintf("./benchmark-%s", time.Now().Format(time.RFC3339Nano))
artifactDir = strings.ReplaceAll(artifactDir, ":", "_")
}
var err error
if err = os.Mkdir(artifactDir, 0755); err != nil {
log.Error(err, "Error creating artifact directory")
os.Exit(1)
}
if err := os.Chmod(artifactDir, 0755); err != nil {
log.Error(err, "Error modifying artifact directory permissions")
os.Exit(1)
}
if artifactDir, err = filepath.Abs(artifactDir); err != nil {
log.Error(err, "Unable to determine the absolute file path of the artifact directory")
os.Exit(1)
}
return artifactDir
}