-
Notifications
You must be signed in to change notification settings - Fork 0
/
pprof2csv.go
94 lines (79 loc) · 2.26 KB
/
pprof2csv.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
package main
import (
"bufio"
"encoding/csv"
"flag"
"fmt"
"os"
"regexp"
"strings"
)
// parsePprofOutput parses the input pprof text (-text or -top output) into a 2D slice for CSV output.
func parsePprofOutput(pprofOutput string) ([][]string, error) {
var data [][]string
header := []string{"flat", "flat%", "sum%", "cum", "cum%", "function"}
data = append(data, header)
// Regular expression to match pprof output lines with fields.
re := regexp.MustCompile(`^\s*([0-9\.]+(?:ms|MB|KB|B)?)\s+([0-9\.]+%)\s+([0-9\.]+%)\s+([0-9\.]+(?:ms|MB|KB|B)?)\s+([0-9\.]+%)\s+(.+)$`)
scanner := bufio.NewScanner(strings.NewReader(pprofOutput))
for scanner.Scan() {
line := scanner.Text()
// Skip lines that do not match the profile data format
if !re.MatchString(line) {
continue
}
matches := re.FindStringSubmatch(line)
if len(matches) == 7 {
// Add the matched groups (excluding the full match itself) to the CSV data.
data = append(data, matches[1:])
}
}
if err := scanner.Err(); err != nil {
return nil, err
}
return data, nil
}
// writeCSV writes the parsed data into a CSV file.
func writeCSV(data [][]string, outputPath string) error {
file, err := os.Create(outputPath)
if err != nil {
return err
}
defer file.Close()
writer := csv.NewWriter(file)
defer writer.Flush()
for _, record := range data {
if err := writer.Write(record); err != nil {
return err
}
}
return nil
}
func main() {
// Define and parse command-line flags
inputFile := flag.String("input", "", "Path to the input pprof -text or -top output file")
outputFile := flag.String("output", "output.csv", "Path to the output CSV file")
flag.Parse()
if *inputFile == "" {
fmt.Println("Usage: pprof2csv -input <pprof_text_output.txt> -output <output.csv>")
os.Exit(1)
}
// Read the input file
pprofData, err := os.ReadFile(*inputFile)
if err != nil {
fmt.Printf("Error reading input file: %v\n", err)
os.Exit(1)
}
// Parse the pprof output into a CSV-compatible format
data, err := parsePprofOutput(string(pprofData))
if err != nil {
fmt.Printf("Error parsing pprof output: %v\n", err)
os.Exit(1)
}
// Write the parsed data into a CSV file
err = writeCSV(data, *outputFile)
if err != nil {
fmt.Printf("Error writing CSV: %v\n", err)
os.Exit(1)
}
}