Skip to content

Commit b39fa89

Browse files
Non-Wrapping Implemented
The core feature for the non wrapping way of collecting profile information is ready
1 parent 3daf60c commit b39fa89

File tree

6 files changed

+58
-31
lines changed

6 files changed

+58
-31
lines changed

benchmark/api.go

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -96,17 +96,12 @@ func CollectProfileFunctions(args *args.CollectionArgs) error {
9696
return fmt.Errorf("failed to create output directory: %w", err)
9797
}
9898

99-
filter := parser.ProfileFilter{
100-
FunctionPrefixes: args.BenchmarkConfig.IncludePrefixes,
101-
IgnoreFunctions: args.BenchmarkConfig.IgnoreFunctions,
102-
}
103-
104-
functions, err := parser.GetAllFunctionNames(paths.ProfileTextFile, filter)
99+
functions, err := parser.GetAllFunctionNames(paths.ProfileTextFile, args.BenchmarkConfig)
105100
if err != nil {
106101
return fmt.Errorf("failed to extract function names: %w", err)
107102
}
108103

109-
if err = saveAllFunctionsPprofContents(functions, paths); err != nil {
104+
if err = collector.SaveAllFunctionsPprofContents(functions, paths.ProfileBinaryFile, paths.FunctionDirectory); err != nil {
110105
return fmt.Errorf("getAllFunctionsPprofContents failed: %w", err)
111106
}
112107
}

benchmark/helpers.go

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import (
1010
"strings"
1111
"time"
1212

13-
"github.com/AlexsanderHamir/prof/collector"
1413
"github.com/AlexsanderHamir/prof/shared"
1514
)
1615

@@ -209,15 +208,3 @@ func getProfilePaths(tag, benchmarkName, profile string) ProfilePaths {
209208
FunctionDirectory: filepath.Join(tagDir, profile+shared.FunctionsDirSuffix, benchmarkName),
210209
}
211210
}
212-
213-
// saveAllFunctionsPprofContents calls [getFunctionPprofContent] sequentially.
214-
func saveAllFunctionsPprofContents(functions []string, paths ProfilePaths) error {
215-
for _, function := range functions {
216-
outputFile := filepath.Join(paths.FunctionDirectory, function+"."+shared.TextExtension)
217-
if err := collector.GetFunctionPprofContent(function, paths.ProfileBinaryFile, outputFile); err != nil {
218-
return fmt.Errorf("failed to extract function content for %s: %w", function, err)
219-
}
220-
}
221-
222-
return nil
223-
}

collector/api.go

Lines changed: 51 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,13 @@ import (
99
"path/filepath"
1010
"strings"
1111

12+
"github.com/AlexsanderHamir/prof/config"
13+
"github.com/AlexsanderHamir/prof/parser"
1214
"github.com/AlexsanderHamir/prof/shared"
1315
)
1416

17+
const globalSign = "*"
18+
1519
// RunCollector handles data organization without wrapping go test.
1620
func RunCollector(files []string, tag string) error {
1721
if err := ensureDirExists(shared.MainDirOutput); err != nil {
@@ -24,23 +28,51 @@ func RunCollector(files []string, tag string) error {
2428
return fmt.Errorf("CleanOrCreateDir failed: %w", err)
2529
}
2630

31+
cfg, err := config.LoadFromFile(shared.ConfigFilename)
32+
if err != nil {
33+
cfg = &config.Config{}
34+
}
35+
36+
var functionFilter config.FunctionFilter
37+
38+
globalFilter, hasGlobalFilter := cfg.FunctionFilter[globalSign]
39+
if hasGlobalFilter {
40+
functionFilter = globalFilter
41+
}
42+
2743
for _, binaryFilePath := range files {
28-
// 3. create a dir for each profile file
2944
fileName := strings.TrimSuffix(binaryFilePath, filepath.Ext(binaryFilePath))
3045
profilepath := path.Join(tagDir, fileName)
31-
if err := ensureDirExists(profilepath); err != nil {
46+
if err = ensureDirExists(profilepath); err != nil {
3247
return err
3348
}
3449

35-
// 4. Collect results.
36-
// 1. generate txt files
37-
outputFilePath := path.Join(profilepath, fileName+"."+shared.TextExtension)
38-
if err := GenerateProfileTextOutput(binaryFilePath, outputFilePath); err != nil {
50+
if !hasGlobalFilter {
51+
localFilter, hasLocalFilter := cfg.FunctionFilter[fileName]
52+
if hasLocalFilter {
53+
functionFilter = localFilter
54+
}
55+
}
56+
57+
outputTextFilePath := path.Join(profilepath, fileName+"."+shared.TextExtension)
58+
if err = GenerateProfileTextOutput(binaryFilePath, outputTextFilePath); err != nil {
3959
return err
4060
}
4161

42-
// 2. collect functions according to config
62+
var functions []string
63+
functions, err = parser.GetAllFunctionNames(outputTextFilePath, functionFilter)
64+
if err != nil {
65+
return fmt.Errorf("failed to extract function names: %w", err)
66+
}
67+
68+
functionDir := path.Join(profilepath, "functions")
69+
if err = ensureDirExists(functionDir); err != nil {
70+
return err
71+
}
4372

73+
if err = SaveAllFunctionsPprofContents(functions, binaryFilePath, functionDir); err != nil {
74+
return fmt.Errorf("getAllFunctionsPprofContents failed: %w", err)
75+
}
4476
}
4577
return nil
4678
}
@@ -92,3 +124,15 @@ func GetFunctionPprofContent(function, binaryFile, outputFile string) error {
92124
slog.Info("Collected function", "function", function)
93125
return nil
94126
}
127+
128+
// SaveAllFunctionsPprofContents calls [GetFunctionPprofContent] sequentially.
129+
func SaveAllFunctionsPprofContents(functions []string, binaryPath, basePath string) error {
130+
for _, functionName := range functions {
131+
outputFile := filepath.Join(basePath, functionName+"."+shared.TextExtension)
132+
if err := GetFunctionPprofContent(functionName, binaryPath, outputFile); err != nil {
133+
return fmt.Errorf("failed to extract function content for %s: %w", functionName, err)
134+
}
135+
}
136+
137+
return nil
138+
}

collector/helpers.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,3 @@ func ensureDirExists(basePath string) error {
1717

1818
return nil
1919
}
20-

parser/api.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"strings"
99

1010
"github.com/AlexsanderHamir/prof/args"
11+
"github.com/AlexsanderHamir/prof/config"
1112
"github.com/AlexsanderHamir/prof/shared"
1213
)
1314

@@ -74,7 +75,7 @@ func TurnLinesIntoObjects(profilePath, profileType string) ([]*LineObj, error) {
7475
}
7576

7677
// GetAllFunctionNames extracts all function names from a profile text file, applying the given filter.
77-
func GetAllFunctionNames(filePath string, filter ProfileFilter) (names []string, err error) {
78+
func GetAllFunctionNames(filePath string, filter config.FunctionFilter) (names []string, err error) {
7879
scanner, file, err := shared.GetScanner(filePath)
7980
if err != nil {
8081
return nil, fmt.Errorf("GetAllFunctionNames Failed: %w", err)
@@ -107,7 +108,7 @@ func GetAllFunctionNames(filePath string, filter ProfileFilter) (names []string,
107108
continue
108109
}
109110

110-
if funcName := extractFunctionName(line, filter.FunctionPrefixes, ignoreSet); funcName != "" {
111+
if funcName := extractFunctionName(line, filter.IncludePrefixes, ignoreSet); funcName != "" {
111112
names = append(names, funcName)
112113
}
113114
}

shared/utils.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ const (
2222
PermFile = 0o644
2323
FunctionsDirSuffix = "_functions"
2424
TextExtension = "txt"
25+
ConfigFilename = "config_template.json"
2526
)
2627

2728
func GetScanner(filePath string) (*bufio.Scanner, *os.File, error) {
@@ -40,7 +41,7 @@ func CleanOrCreateDir(dir string) error {
4041
info, err := os.Stat(dir)
4142
if err != nil {
4243
if os.IsNotExist(err) {
43-
if err := os.MkdirAll(dir, PermDir); err != nil {
44+
if err = os.MkdirAll(dir, PermDir); err != nil {
4445
return fmt.Errorf("failed to create %s directory: %w", dir, err)
4546
}
4647
return nil

0 commit comments

Comments
 (0)