Skip to content

Commit aed3a58

Browse files
CLI Refactor
1. No more string parsing unnecessarily 2. Removed dead code bad bad logic
1 parent 7b9e4d6 commit aed3a58

File tree

6 files changed

+51
-196
lines changed

6 files changed

+51
-196
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
tasks.md
22
*.out
33
*.prof
4-
*prof
4+
*prof
5+
/Enviroment*/

cli/api.go

Lines changed: 24 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,19 @@ import (
66
"log/slog"
77

88
"github.com/AlexsanderHamir/prof/args"
9+
"github.com/AlexsanderHamir/prof/benchmark"
910
"github.com/AlexsanderHamir/prof/collector"
1011
"github.com/AlexsanderHamir/prof/config"
12+
"github.com/AlexsanderHamir/prof/shared"
1113
"github.com/AlexsanderHamir/prof/tracker"
1214
"github.com/AlexsanderHamir/prof/version"
1315
"github.com/spf13/cobra"
1416
)
1517

1618
var (
1719
// Root command flags.
18-
benchmarks string
19-
profiles string
20+
benchmarks []string
21+
profiles []string
2022
tag string
2123
count int
2224

@@ -48,16 +50,16 @@ func CreateRootCmd() *cobra.Command {
4850
func createManualCmd() *cobra.Command {
4951
manualCmd := &cobra.Command{
5052
Use: "manual",
51-
Short: "Receives profile files and performs data collection and organization.",
53+
Short: "Receives profile files and performs data collection and organization. (doesn't wrap go test)",
5254
Args: cobra.MinimumNArgs(1),
53-
Example: "prof manual cpu.prof memory.prof block.prof",
55+
Example: "prof manual --tag tagName cpu.prof memory.prof block.prof mutex.prof",
5456
RunE: func(_ *cobra.Command, args []string) error {
5557
return collector.RunCollector(args, tag)
5658
},
5759
}
5860

5961
tagFlag := "tag"
60-
manualCmd.Flags().StringVar(&tag, tagFlag, "", "Tag for organization")
62+
manualCmd.Flags().StringVar(&tag, tagFlag, "", "The tag is used to organize the results")
6163
_ = manualCmd.MarkFlagRequired(tagFlag)
6264

6365
return manualCmd
@@ -68,8 +70,7 @@ func createRunCmd() *cobra.Command {
6870
profileFlag := "profiles"
6971
tagFlag := "tag"
7072
countFlag := "count"
71-
example := fmt.Sprintf(`prof run --%s "[BenchmarkGenPool]" --%s "[cpu,memory]" --%s 10 --%s "tag1"`,
72-
benchFlag, profileFlag, countFlag, tagFlag)
73+
example := fmt.Sprintf(`prof run --%s BenchmarkGenPool --%s cpu,memory --%s 10 --%s "tag1"`, benchFlag, profileFlag, countFlag, tagFlag)
7374

7475
runCmd := &cobra.Command{
7576
Use: "run",
@@ -78,9 +79,9 @@ func createRunCmd() *cobra.Command {
7879
Example: example,
7980
}
8081

81-
runCmd.Flags().StringVar(&benchmarks, benchFlag, "", `Benchmarks to run (e.g., "[BenchmarkGenPool]")"`)
82-
runCmd.Flags().StringVar(&profiles, profileFlag, "", `Profiles to use (e.g., "[cpu,memory,mutex]")`)
83-
runCmd.Flags().StringVar(&tag, tagFlag, "", "Tag for the run")
82+
runCmd.Flags().StringSliceVar(&benchmarks, benchFlag, []string{}, `Benchmarks to run (e.g., "[BenchmarkGenPool]")"`)
83+
runCmd.Flags().StringSliceVar(&profiles, profileFlag, []string{}, `Profiles to use (e.g., "[cpu,memory,mutex]")`)
84+
runCmd.Flags().StringVar(&tag, tagFlag, "", "The tag is used to organize the results")
8485
runCmd.Flags().IntVar(&count, countFlag, 0, "Number of runs")
8586

8687
_ = runCmd.MarkFlagRequired(benchFlag)
@@ -153,34 +154,33 @@ func Execute() error {
153154
}
154155

155156
func runBenchmarks(_ *cobra.Command, _ []string) error {
156-
if benchmarks == "" || profiles == "" || tag == "" || count == 0 {
157-
return errors.New("missing required arguments. Use --help for usage information")
157+
if len(benchmarks) == 0 {
158+
return errors.New("benchmarks flag is empty")
158159
}
159160

160-
cfg, err := config.LoadFromFile("config_template.json")
161-
if err != nil {
162-
cfg = &config.Config{}
161+
if len(profiles) == 0 {
162+
return errors.New("profiles flag is empty")
163163
}
164164

165-
benchmarkList, profileList, err := parseAndValidateBenchmarkParams(benchmarks, profiles)
165+
cfg, err := config.LoadFromFile(shared.ConfigFilename)
166166
if err != nil {
167-
return fmt.Errorf("failed to parse benchmark config: %w", err)
167+
cfg = &config.Config{}
168168
}
169169

170-
if err = setupDirectories(tag, benchmarkList, profileList); err != nil {
170+
if err = benchmark.SetupDirectories(tag, benchmarks, profiles); err != nil {
171171
return fmt.Errorf("failed to setup directories: %w", err)
172172
}
173173

174174
benchArgs := &args.BenchArgs{
175-
Benchmarks: benchmarkList,
176-
Profiles: profileList,
175+
Benchmarks: benchmarks,
176+
Profiles: profiles,
177177
Count: count,
178178
Tag: tag,
179179
}
180180

181181
printConfiguration(benchArgs, cfg.FunctionFilter)
182182

183-
if err = runBencAndGetProfiles(benchArgs, cfg.FunctionFilter); err != nil {
183+
if err = runBenchAndGetProfiles(benchArgs, cfg.FunctionFilter); err != nil {
184184
return err
185185
}
186186

@@ -202,10 +202,6 @@ func runSetup(_ *cobra.Command, _ []string) error {
202202

203203
// runTrack handles the track command execution
204204
func runTrack(_ *cobra.Command, _ []string) error {
205-
if !validProfiles[profileType] {
206-
return fmt.Errorf("invalid profile type '%s'. Valid types: cpu, memory, mutex, block", profileType)
207-
}
208-
209205
validFormats := map[string]bool{
210206
"summary": true,
211207
"detailed": true,
@@ -215,20 +211,13 @@ func runTrack(_ *cobra.Command, _ []string) error {
215211
return fmt.Errorf("invalid output format '%s'. Valid formats: summary, detailed", outputFormat)
216212
}
217213

218-
// Call the tracker API
219-
report, err := tracker.CheckPerformanceDifferences(
220-
baselineTag,
221-
currentTag,
222-
benchmarkName,
223-
profileType,
224-
)
225-
214+
report, err := tracker.CheckPerformanceDifferences(baselineTag, currentTag, benchmarkName, profileType)
226215
if err != nil {
227216
return fmt.Errorf("failed to track performance differences: %w", err)
228217
}
229218

230-
// Display results based on output format
231-
if len(report.FunctionChanges) == 0 {
219+
noFunctionChanges := len(report.FunctionChanges) == 0
220+
if noFunctionChanges {
232221
slog.Info("No function changes detected between the two runs")
233222
return nil
234223
}

cli/globals.go

Lines changed: 0 additions & 11 deletions
This file was deleted.

cli/helpers.go

Lines changed: 5 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import (
55
"log/slog"
66
"math"
77
"sort"
8-
"strings"
98

109
"github.com/AlexsanderHamir/prof/args"
1110
"github.com/AlexsanderHamir/prof/benchmark"
@@ -14,85 +13,14 @@ import (
1413
"github.com/AlexsanderHamir/prof/tracker"
1514
)
1615

17-
var validProfiles = map[string]bool{
18-
"cpu": true,
19-
"memory": true,
20-
"mutex": true,
21-
"block": true,
22-
}
23-
24-
// validateListArguments checks if the benchmarks and profiles arguments are valid lists.
25-
func validateListArguments(benchmarks, profiles string) error {
26-
if strings.TrimSpace(benchmarks) == "[]" {
27-
return ErrEmptyBenchmarks
28-
}
29-
if strings.TrimSpace(profiles) == "[]" {
30-
return ErrEmptyProfiles
31-
}
32-
33-
benchmarks = strings.TrimSpace(benchmarks)
34-
profiles = strings.TrimSpace(profiles)
35-
36-
if !strings.HasPrefix(benchmarks, "[") || !strings.HasSuffix(benchmarks, "]") {
37-
return fmt.Errorf("benchmarks %w %s", ErrBracket, benchmarks)
38-
}
39-
if !strings.HasPrefix(profiles, "[") || !strings.HasSuffix(profiles, "]") {
40-
return fmt.Errorf("profiles %w %s", ErrBracket, profiles)
41-
}
42-
43-
return nil
44-
}
45-
46-
// parseListArgument parses a bracketed, comma-separated string into a slice of strings.
47-
func parseListArgument(arg string) []string {
48-
arg = strings.Trim(arg, "[]")
49-
if arg == "" {
50-
return []string{}
51-
}
52-
53-
parts := strings.Split(arg, ",")
54-
var result []string
55-
for _, part := range parts {
56-
result = append(result, strings.TrimSpace(part))
57-
}
58-
return result
59-
}
60-
61-
// Rest of the functions remain the same...
62-
func parseAndValidateBenchmarkParams(benchmarks, profiles string) ([]string, []string, error) {
63-
if err := validateListArguments(benchmarks, profiles); err != nil {
64-
return nil, nil, err
65-
}
66-
67-
benchmarkList := parseListArgument(benchmarks)
68-
profileList := parseListArgument(profiles)
69-
70-
if err := validateAcceptedProfiles(profileList); err != nil {
71-
return nil, nil, err
72-
}
73-
74-
return benchmarkList, profileList, nil
75-
}
76-
77-
func validateAcceptedProfiles(profiles []string) error {
78-
for _, profile := range profiles {
79-
if valid := validProfiles[profile]; !valid {
80-
return fmt.Errorf("received unvalid profile: %s", profile)
81-
}
82-
}
83-
return nil
84-
}
85-
86-
func setupDirectories(tag string, benchmarks, profiles []string) error {
87-
return benchmark.SetupDirectories(tag, benchmarks, profiles)
88-
}
89-
9016
func printConfiguration(benchArgs *args.BenchArgs, functionFilterPerBench map[string]config.FunctionFilter) {
91-
slog.Info("Parsed arguments",
17+
slog.Info(
18+
"Parsed arguments",
9219
"Benchmarks", benchArgs.Benchmarks,
9320
"Profiles", benchArgs.Profiles,
9421
"Tag", benchArgs.Tag,
95-
"Count", benchArgs.Count)
22+
"Count", benchArgs.Count,
23+
)
9624

9725
hasBenchFunctionFilters := len(functionFilterPerBench) > 0
9826
if hasBenchFunctionFilters {
@@ -105,7 +33,7 @@ func printConfiguration(benchArgs *args.BenchArgs, functionFilterPerBench map[st
10533
}
10634
}
10735

108-
func runBencAndGetProfiles(benchArgs *args.BenchArgs, benchmarkConfigs map[string]config.FunctionFilter) error {
36+
func runBenchAndGetProfiles(benchArgs *args.BenchArgs, benchmarkConfigs map[string]config.FunctionFilter) error {
10937
slog.Info("Starting benchmark pipeline...")
11038

11139
for _, benchmarkName := range benchArgs.Benchmarks {
@@ -120,7 +48,6 @@ func runBencAndGetProfiles(benchArgs *args.BenchArgs, benchmarkConfigs map[strin
12048
}
12149

12250
slog.Info("Analyzing profile functions", "Benchmark", benchmarkName)
123-
12451
args := &args.CollectionArgs{
12552
Tag: benchArgs.Tag,
12653
Profiles: benchArgs.Profiles,

0 commit comments

Comments
 (0)