Skip to content

Commit 54f0318

Browse files
Merge pull request #6 from AlexsanderHamir/refactors
Refactors
2 parents 19fc447 + 2a0a468 commit 54f0318

File tree

3 files changed

+77
-45
lines changed

3 files changed

+77
-45
lines changed

collector/api.go

Lines changed: 10 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,12 @@ package collector
22

33
import (
44
"fmt"
5-
"log/slog"
65
"os"
76
"os/exec"
87
"path"
98
"path/filepath"
10-
"strings"
119

1210
"github.com/AlexsanderHamir/prof/config"
13-
"github.com/AlexsanderHamir/prof/parser"
1411
"github.com/AlexsanderHamir/prof/shared"
1512
)
1613

@@ -34,18 +31,17 @@ func RunCollector(files []string, tag string) error {
3431
}
3532

3633
var functionFilter config.FunctionFilter
37-
3834
globalFilter, hasGlobalFilter := cfg.FunctionFilter[globalSign]
3935
if hasGlobalFilter {
4036
functionFilter = globalFilter
4137
}
4238

43-
for _, binaryFilePath := range files {
44-
binaryDirName := filepath.Base(binaryFilePath)
45-
fileName := strings.TrimSuffix(binaryDirName, filepath.Ext(binaryDirName))
46-
profileDirPath := path.Join(tagDir, fileName)
47-
if err = ensureDirExists(profileDirPath); err != nil {
48-
return err
39+
var profileDirPath string
40+
for _, fullBinaryPath := range files {
41+
fileName := getFileName(fullBinaryPath)
42+
profileDirPath, err = createProfileDirectory(tagDir, fileName)
43+
if err != nil {
44+
return fmt.Errorf("createProfileDirectory failed: %w", err)
4945
}
5046

5147
if !hasGlobalFilter {
@@ -56,23 +52,12 @@ func RunCollector(files []string, tag string) error {
5652
}
5753

5854
outputTextFilePath := path.Join(profileDirPath, fileName+"."+shared.TextExtension)
59-
if err = GenerateProfileTextOutput(binaryFilePath, outputTextFilePath); err != nil {
60-
return err
61-
}
62-
63-
var functions []string
64-
functions, err = parser.GetAllFunctionNames(outputTextFilePath, functionFilter)
65-
if err != nil {
66-
return fmt.Errorf("failed to extract function names: %w", err)
67-
}
68-
69-
functionDir := path.Join(profileDirPath, "functions")
70-
if err = ensureDirExists(functionDir); err != nil {
55+
if err = GenerateProfileTextOutput(fullBinaryPath, outputTextFilePath); err != nil {
7156
return err
7257
}
7358

74-
if err = SaveAllFunctionsPprofContents(functions, binaryFilePath, functionDir); err != nil {
75-
return fmt.Errorf("getAllFunctionsPprofContents failed: %w", err)
59+
if err = collectFunctions(outputTextFilePath, profileDirPath, fullBinaryPath, functionFilter); err != nil {
60+
return fmt.Errorf("collectFunctions failed: %w", err)
7661
}
7762
}
7863
return nil
@@ -106,31 +91,11 @@ func GeneratePNGVisualization(binaryFile, outputFile string) error {
10691
return os.WriteFile(outputFile, output, shared.PermFile)
10792
}
10893

109-
// GetFunctionPprofContent gets code line level mapping of specified function
110-
// and writes the data to a file named after the function.
111-
func GetFunctionPprofContent(function, binaryFile, outputFile string) error {
112-
cmd := []string{"go", "tool", "pprof", fmt.Sprintf("-list=%s", function), binaryFile}
113-
114-
// #nosec ProfileTextDir04 -- cmd is constructed internally by getFunctionPprofContent(), not from user input
115-
execCmd := exec.Command(cmd[0], cmd[1:]...)
116-
output, err := execCmd.Output()
117-
if err != nil {
118-
return fmt.Errorf("pprof list command failed: %w", err)
119-
}
120-
121-
if err = os.WriteFile(outputFile, output, shared.PermFile); err != nil {
122-
return fmt.Errorf("failed to write function content: %w", err)
123-
}
124-
125-
slog.Info("Collected function", "function", function)
126-
return nil
127-
}
128-
12994
// SaveAllFunctionsPprofContents calls [GetFunctionPprofContent] sequentially.
13095
func SaveAllFunctionsPprofContents(functions []string, binaryPath, basePath string) error {
13196
for _, functionName := range functions {
13297
outputFile := filepath.Join(basePath, functionName+"."+shared.TextExtension)
133-
if err := GetFunctionPprofContent(functionName, binaryPath, outputFile); err != nil {
98+
if err := getFunctionPprofContent(functionName, binaryPath, outputFile); err != nil {
13499
return fmt.Errorf("failed to extract function content for %s: %w", functionName, err)
135100
}
136101
}

collector/helpers.go

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,14 @@
11
package collector
22

33
import (
4+
"fmt"
5+
"log/slog"
46
"os"
7+
"os/exec"
8+
"path"
59

10+
"github.com/AlexsanderHamir/prof/config"
11+
"github.com/AlexsanderHamir/prof/parser"
612
"github.com/AlexsanderHamir/prof/shared"
713
)
814

@@ -17,3 +23,42 @@ func ensureDirExists(basePath string) error {
1723

1824
return nil
1925
}
26+
27+
// getFunctionPprofContent gets code line level mapping of specified function
28+
// and writes the data to a file named after the function.
29+
func getFunctionPprofContent(function, binaryFile, outputFile string) error {
30+
cmd := []string{"go", "tool", "pprof", fmt.Sprintf("-list=%s", function), binaryFile}
31+
32+
// #nosec ProfileTextDir04 -- cmd is constructed internally by getFunctionPprofContent(), not from user input
33+
execCmd := exec.Command(cmd[0], cmd[1:]...)
34+
output, err := execCmd.Output()
35+
if err != nil {
36+
return fmt.Errorf("pprof list command failed: %w", err)
37+
}
38+
39+
if err = os.WriteFile(outputFile, output, shared.PermFile); err != nil {
40+
return fmt.Errorf("failed to write function content: %w", err)
41+
}
42+
43+
slog.Info("Collected function", "function", function)
44+
return nil
45+
}
46+
47+
func collectFunctions(outputTextFilePath, profileDirPath, fullBinaryPath string, functionFilter config.FunctionFilter) error {
48+
var functions []string
49+
functions, err := parser.GetAllFunctionNames(outputTextFilePath, functionFilter)
50+
if err != nil {
51+
return fmt.Errorf("failed to extract function names: %w", err)
52+
}
53+
54+
functionDir := path.Join(profileDirPath, "functions")
55+
if err = ensureDirExists(functionDir); err != nil {
56+
return err
57+
}
58+
59+
if err = SaveAllFunctionsPprofContents(functions, fullBinaryPath, functionDir); err != nil {
60+
return fmt.Errorf("getAllFunctionsPprofContents failed: %w", err)
61+
}
62+
63+
return nil
64+
}

collector/utils.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
package collector
22

3+
import (
4+
"path"
5+
"path/filepath"
6+
"strings"
7+
)
8+
39
func getPprofTextParams() []string {
410
return []string{
511
"-cum",
@@ -8,3 +14,19 @@ func getPprofTextParams() []string {
814
"-top",
915
}
1016
}
17+
18+
func getFileName(fullPath string) string {
19+
file := filepath.Base(fullPath)
20+
fileName := strings.TrimSuffix(file, filepath.Ext(file))
21+
22+
return fileName
23+
}
24+
25+
func createProfileDirectory(tagDir, fileName string) (string, error) {
26+
profileDirPath := path.Join(tagDir, fileName)
27+
if err := ensureDirExists(profileDirPath); err != nil {
28+
return "", err
29+
}
30+
31+
return profileDirPath, nil
32+
}

0 commit comments

Comments
 (0)