|
| 1 | +package cmd |
| 2 | + |
| 3 | +import ( |
| 4 | + "bufio" |
| 5 | + "bytes" |
| 6 | + "encoding/json" |
| 7 | + "fmt" |
| 8 | + "github.com/google/pprof/driver" |
| 9 | + "github.com/spf13/cobra" |
| 10 | + "io" |
| 11 | + "io/ioutil" |
| 12 | + "log" |
| 13 | + "net/http" |
| 14 | + "os" |
| 15 | + "strings" |
| 16 | +) |
| 17 | + |
| 18 | +// TextItem holds a single text report entry. |
| 19 | +type TextItem struct { |
| 20 | + Symbol string `json:"symbol"` |
| 21 | + Flat string `json:"flat%"` |
| 22 | + Cum string `json:"cum%"` |
| 23 | +} |
| 24 | + |
| 25 | +// TextReport holds a list of text items from the report and a list |
| 26 | +// of labels that describe the report. |
| 27 | +type TextReport struct { |
| 28 | + Items []TextItem `json:"data"` |
| 29 | + TotalRows int `json:"totalRows"` |
| 30 | + TotalPages int `json:"totalPages"` |
| 31 | + Labels []string `json:"labels"` |
| 32 | +} |
| 33 | + |
| 34 | +func exportLogic() func(cmd *cobra.Command, args []string) { |
| 35 | + return func(cmd *cobra.Command, args []string) { |
| 36 | + if len(args) != 1 { |
| 37 | + log.Fatalf("Exactly one profile file is required") |
| 38 | + } |
| 39 | + for _, granularity := range []string{"lines", "functions"} { |
| 40 | + err, report := generateTextReports(granularity, args[0]) |
| 41 | + if err == nil { |
| 42 | + var w io.Writer |
| 43 | + // open output file |
| 44 | + if local { |
| 45 | + fo, err := os.Create(localFilename) |
| 46 | + if err != nil { |
| 47 | + panic(err) |
| 48 | + } |
| 49 | + // close fo on exit and check for its returned error |
| 50 | + defer func() { |
| 51 | + if err := fo.Close(); err != nil { |
| 52 | + panic(err) |
| 53 | + } |
| 54 | + }() |
| 55 | + // make a write buffer |
| 56 | + w = bufio.NewWriter(fo) |
| 57 | + enc := json.NewEncoder(w) |
| 58 | + err = enc.Encode(report) |
| 59 | + if err != nil { |
| 60 | + log.Fatalf("Unable to export the profile to local json. Error: %v", err) |
| 61 | + } else { |
| 62 | + log.Printf("Succesfully exported profile to local file %s", localFilename) |
| 63 | + } |
| 64 | + } else { |
| 65 | + postBody, err := json.Marshal(report) |
| 66 | + if err != nil { |
| 67 | + log.Fatalf("An Error Occured %v", err) |
| 68 | + } |
| 69 | + responseBody := bytes.NewBuffer(postBody) |
| 70 | + endPoint := fmt.Sprintf("%s/v1/gh/%s/%s/commit/%s/bench/%s/cpu/%s", codeperfUrl, gitOrg, gitRepo, gitCommit, bench, granularity) |
| 71 | + resp, err := http.Post(endPoint, "application/json", responseBody) |
| 72 | + //Handle Error |
| 73 | + if err != nil { |
| 74 | + log.Fatalf("An Error Occured %v", err) |
| 75 | + } |
| 76 | + defer resp.Body.Close() |
| 77 | + //Read the response body |
| 78 | + body, err := ioutil.ReadAll(resp.Body) |
| 79 | + if err != nil { |
| 80 | + log.Fatalln(err) |
| 81 | + } |
| 82 | + sb := string(body) |
| 83 | + log.Printf(sb) |
| 84 | + } |
| 85 | + } else { |
| 86 | + log.Fatal(err) |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + } |
| 91 | +} |
| 92 | + |
| 93 | +func generateTextReports(granularity string, input string) (err error, report TextReport) { |
| 94 | + f := baseFlags() |
| 95 | + |
| 96 | + // Read the profile from the encoded protobuf |
| 97 | + outputTempFile, err := ioutil.TempFile("", "profile_output") |
| 98 | + if err != nil { |
| 99 | + log.Fatalf("cannot create tempfile: %v", err) |
| 100 | + } |
| 101 | + defer os.Remove(outputTempFile.Name()) |
| 102 | + defer outputTempFile.Close() |
| 103 | + f.strings["output"] = outputTempFile.Name() |
| 104 | + f.bools["text"] = true |
| 105 | + f.bools[granularity] = true |
| 106 | + f.args = []string{ |
| 107 | + input, |
| 108 | + } |
| 109 | + reader := bufio.NewReader(os.Stdin) |
| 110 | + options := &driver.Options{ |
| 111 | + Flagset: f, |
| 112 | + UI: &UI{r: reader}, |
| 113 | + } |
| 114 | + |
| 115 | + if err = driver.PProf(options); err != nil { |
| 116 | + log.Fatalf("cannot read pprof profile from %s. Error: %v", input, err) |
| 117 | + return |
| 118 | + } |
| 119 | + |
| 120 | + file, err := os.Open(outputTempFile.Name()) |
| 121 | + if err != nil { |
| 122 | + log.Fatal(err) |
| 123 | + } |
| 124 | + defer file.Close() |
| 125 | + r := bufio.NewReader(file) |
| 126 | + benchTypeStr, _, _ := r.ReadLine() |
| 127 | + benchDurationStr, _, _ := r.ReadLine() |
| 128 | + benchNodesDetails, _, _ := r.ReadLine() |
| 129 | + benchNodesDropDetails, _, e := r.ReadLine() |
| 130 | + report.Labels = append(report.Labels, string(benchTypeStr)) |
| 131 | + report.Labels = append(report.Labels, string(benchDurationStr)) |
| 132 | + report.Labels = append(report.Labels, string(benchNodesDetails)) |
| 133 | + report.Labels = append(report.Labels, string(benchNodesDropDetails)) |
| 134 | + // ignore dropped |
| 135 | + r.ReadLine() |
| 136 | + // ignore header |
| 137 | + r.ReadLine() |
| 138 | + var b []byte |
| 139 | + for e == nil { |
| 140 | + b, _, e = r.ReadLine() |
| 141 | + s := strings.TrimSpace(string(b)) |
| 142 | + // ignore flat |
| 143 | + ns := strings.SplitN(s, " ", 2) |
| 144 | + if len(ns) < 2 { |
| 145 | + continue |
| 146 | + } |
| 147 | + s = strings.TrimSpace(ns[1]) |
| 148 | + // read flat% |
| 149 | + ns = strings.SplitN(s, " ", 2) |
| 150 | + flatPercent := ns[0] |
| 151 | + s = strings.TrimSpace(ns[1]) |
| 152 | + // ignore sum |
| 153 | + ns = strings.SplitN(s, " ", 2) |
| 154 | + s = strings.TrimSpace(ns[1]) |
| 155 | + // ignore cum |
| 156 | + ns = strings.SplitN(s, " ", 2) |
| 157 | + s = strings.TrimSpace(ns[1]) |
| 158 | + // read cum% |
| 159 | + ns = strings.SplitN(s, " ", 2) |
| 160 | + cumPercent := ns[0] |
| 161 | + symbol := strings.TrimSpace(ns[1]) |
| 162 | + report.Items = append(report.Items, TextItem{ |
| 163 | + Symbol: symbol, |
| 164 | + Flat: flatPercent, |
| 165 | + Cum: cumPercent, |
| 166 | + }) |
| 167 | + } |
| 168 | + report.TotalPages = 1 |
| 169 | + report.TotalRows = len(report.Items) |
| 170 | + return |
| 171 | +} |
0 commit comments