-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapi.go
More file actions
288 lines (234 loc) · 8.61 KB
/
api.go
File metadata and controls
288 lines (234 loc) · 8.61 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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
package cli
import (
"errors"
"fmt"
"log/slog"
"github.com/AlexsanderHamir/prof/engine/benchmark"
"github.com/AlexsanderHamir/prof/engine/collector"
"github.com/AlexsanderHamir/prof/engine/tracker"
"github.com/AlexsanderHamir/prof/engine/version"
"github.com/AlexsanderHamir/prof/internal/args"
"github.com/AlexsanderHamir/prof/internal/config"
"github.com/AlexsanderHamir/prof/internal/shared"
"github.com/spf13/cobra"
)
var (
// Root command flags.
benchmarks []string
profiles []string
tag string
count int
// Track command flags.
baselineTag string
currentTag string
benchmarkName string
profileType string
outputFormat string
)
// CreateRootCmd creates and returns the root cobra command.
func CreateRootCmd() *cobra.Command {
rootCmd := &cobra.Command{
Use: "prof",
Short: "CLI tool for organizing pprof generated data, and analyzing performance differences at the profile level.",
RunE: runBenchmarks,
}
rootCmd.AddCommand(createProfManual())
rootCmd.AddCommand(createProfAuto())
rootCmd.AddCommand(createSetupCmd())
rootCmd.AddCommand(createTrackCmd())
rootCmd.AddCommand(createVersionCmd())
return rootCmd
}
func createProfManual() *cobra.Command {
manualCmd := &cobra.Command{
Use: shared.MANUALCMD,
Short: "Receives profile files and performs data collection and organization. (doesn't wrap go test)",
Args: cobra.MinimumNArgs(1),
Example: fmt.Sprintf("prof %s --tag tagName cpu.prof memory.prof block.prof mutex.prof", shared.MANUALCMD),
RunE: func(_ *cobra.Command, args []string) error {
return collector.RunCollector(args, tag)
},
}
tagFlag := "tag"
manualCmd.Flags().StringVar(&tag, tagFlag, "", "The tag is used to organize the results")
_ = manualCmd.MarkFlagRequired(tagFlag)
return manualCmd
}
func createProfAuto() *cobra.Command {
benchFlag := "benchmarks"
profileFlag := "profiles"
tagFlag := "tag"
countFlag := "count"
example := fmt.Sprintf(`prof %s --%s "BenchmarkGenPool" --%s "cpu,memory" --%s 10 --%s "tag1"`, shared.AUTOCMD, benchFlag, profileFlag, countFlag, tagFlag)
cmd := &cobra.Command{
Use: shared.AUTOCMD,
Short: "Wraps `go test` and `pprof` to benchmark code and gather profiling data for performance investigations.",
RunE: runBenchmarks,
Example: example,
}
cmd.Flags().StringSliceVar(&benchmarks, benchFlag, []string{}, `Benchmarks to run (e.g., "BenchmarkGenPool")"`)
cmd.Flags().StringSliceVar(&profiles, profileFlag, []string{}, `Profiles to use (e.g., "cpu,memory,mutex")`)
cmd.Flags().StringVar(&tag, tagFlag, "", "The tag is used to organize the results")
cmd.Flags().IntVar(&count, countFlag, 0, "Number of runs")
_ = cmd.MarkFlagRequired(benchFlag)
_ = cmd.MarkFlagRequired(profileFlag)
_ = cmd.MarkFlagRequired(tagFlag)
_ = cmd.MarkFlagRequired(countFlag)
return cmd
}
// createTrackCmd creates the track subcommand
func createTrackCmd() *cobra.Command {
shortExplanation := "Compare performance between two benchmark runs to detect regressions and improvements"
cmd := &cobra.Command{
Use: "track",
Short: shortExplanation,
}
cmd.AddCommand(createTrackAutoCmd())
cmd.AddCommand(createTrackManualCmd())
return cmd
}
func createTrackAutoCmd() *cobra.Command {
baseTagFlag := "base"
currentTagFlag := "current"
benchNameFlag := "bench-name"
profileTypeFlag := "profile-type"
outputFormatFlag := "output-format"
example := fmt.Sprintf(`prof track auto --%s "tag1" --%s "tag2" --%s "cpu" --%s "BenchmarkGenPool" --%s "summary"`, baseTagFlag, currentTagFlag, profileTypeFlag, benchNameFlag, outputFormatFlag)
longExplanation := fmt.Sprintf("This command only works if the %s command was used to collect and organize the benchmark and profile data, as it expects a specific directory structure generated by that process.", shared.AUTOCMD)
shortExplanation := "If prof auto was used to collect the data, track auto can be used to analyze it, you just have to pass the tag name."
cmd := &cobra.Command{
Use: shared.TrackAutoCMD,
Short: shortExplanation,
Long: longExplanation,
RunE: runTrackAuto,
Example: example,
}
cmd.Flags().StringVar(&baselineTag, baseTagFlag, "", "Name of the baseline tag")
cmd.Flags().StringVar(¤tTag, currentTagFlag, "", "Name of the current tag")
cmd.Flags().StringVar(&benchmarkName, benchNameFlag, "", "Name of the benchmark")
cmd.Flags().StringVar(&profileType, profileTypeFlag, "", "Profile type (cpu, memory, mutex, block)")
cmd.Flags().StringVar(&outputFormat, outputFormatFlag, "detailed", `Output format: "summary" or "detailed"`)
_ = cmd.MarkFlagRequired(baseTagFlag)
_ = cmd.MarkFlagRequired(currentTagFlag)
_ = cmd.MarkFlagRequired(benchNameFlag)
_ = cmd.MarkFlagRequired(profileTypeFlag)
return cmd
}
func createTrackManualCmd() *cobra.Command {
baseFlag := "base"
currentFlag := "current"
outputFormatFlag := "output-format"
example := fmt.Sprintf(`prof track %s --%s "path/to/profile_file.txt" --%s "path/to/profile_file.txt" --%s "summary"`, shared.TrackManualCMD, baseFlag, currentFlag, outputFormatFlag)
cmd := &cobra.Command{
Use: shared.TrackManualCMD,
Short: "Manually specify the paths to the profile text files you want to compare.",
RunE: runTrackManual,
Example: example,
}
cmd.Flags().StringVar(&baselineTag, baseFlag, "", "Name of the baseline tag")
cmd.Flags().StringVar(¤tTag, currentFlag, "", "Name of the current tag")
cmd.Flags().StringVar(&outputFormat, outputFormatFlag, "", "Output format choice choice")
_ = cmd.MarkFlagRequired(baseFlag)
_ = cmd.MarkFlagRequired(currentFlag)
_ = cmd.MarkFlagRequired(outputFormatFlag)
return cmd
}
func createVersionCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "version",
Short: "Shows the current version of prof and checks for updates.",
RunE: runVersion,
DisableFlagsInUseLine: true,
}
return cmd
}
// createSetupCmd creates the setup subcommand
func createSetupCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "setup",
Short: "Generates the template configuration file.",
RunE: runSetup,
DisableFlagsInUseLine: true,
}
return cmd
}
// Execute runs the CLI application
func Execute() error {
return CreateRootCmd().Execute()
}
func runBenchmarks(_ *cobra.Command, _ []string) error {
if len(benchmarks) == 0 {
return errors.New("benchmarks flag is empty")
}
if len(profiles) == 0 {
return errors.New("profiles flag is empty")
}
cfg, err := config.LoadFromFile(shared.ConfigFilename)
if err != nil {
cfg = &config.Config{}
}
if err = benchmark.SetupDirectories(tag, benchmarks, profiles); err != nil {
return fmt.Errorf("failed to setup directories: %w", err)
}
benchArgs := &args.BenchArgs{
Benchmarks: benchmarks,
Profiles: profiles,
Count: count,
Tag: tag,
}
printConfiguration(benchArgs, cfg.FunctionFilter)
if err = runBenchAndGetProfiles(benchArgs, cfg.FunctionFilter); err != nil {
return err
}
return nil
}
// runVersion handles the version command execution
func runVersion(_ *cobra.Command, _ []string) error {
current, latest := version.Check()
output := version.FormatOutput(current, latest)
fmt.Print(output)
return nil
}
// runSetup handles the setup command execution
func runSetup(_ *cobra.Command, _ []string) error {
return config.CreateTemplate()
}
var validFormats = map[string]bool{
"summary": true,
"detailed": true,
"summary-html": true,
"detailed-html": true,
}
// runTrack handles the track command execution
func runTrackAuto(_ *cobra.Command, _ []string) error {
if !validFormats[outputFormat] {
return fmt.Errorf("invalid output format '%s'. Valid formats: summary, detailed", outputFormat)
}
report, err := tracker.CheckPerformanceDifferences(baselineTag, currentTag, benchmarkName, profileType)
if err != nil {
return fmt.Errorf("failed to track performance differences: %w", err)
}
noFunctionChanges := len(report.FunctionChanges) == 0
if noFunctionChanges {
slog.Info("No function changes detected between the two runs")
return nil
}
chooseOutputFormat(report)
return nil
}
func runTrackManual(_ *cobra.Command, _ []string) error {
if !validFormats[outputFormat] {
return fmt.Errorf("invalid output format '%s'. Valid formats: summary, detailed", outputFormat)
}
report, err := tracker.CheckPerformanceDifferencesManual(baselineTag, currentTag)
if err != nil {
return fmt.Errorf("failed to track performance differences: %w", err)
}
noFunctionChanges := len(report.FunctionChanges) == 0
if noFunctionChanges {
slog.Info("No function changes detected between the two runs")
return nil
}
chooseOutputFormat(report)
return nil
}