|
| 1 | +// Copyright 2022 The Go Authors. All rights reserved. |
| 2 | +// Use of this source code is governed by a BSD-style |
| 3 | +// license that can be found in the LICENSE file. |
| 4 | + |
| 5 | +package app |
| 6 | + |
| 7 | +import ( |
| 8 | + "context" |
| 9 | + "embed" |
| 10 | + "encoding/json" |
| 11 | + "fmt" |
| 12 | + "log" |
| 13 | + "net/http" |
| 14 | + "sort" |
| 15 | + "time" |
| 16 | + |
| 17 | + "github.com/influxdata/influxdb-client-go/v2/api" |
| 18 | + "golang.org/x/build/internal/influx" |
| 19 | + "golang.org/x/build/third_party/bandchart" |
| 20 | +) |
| 21 | + |
| 22 | +// /dashboard/ displays a dashboard of benchmark results over time for |
| 23 | +// performance monitoring. |
| 24 | + |
| 25 | +//go:embed dashboard/* |
| 26 | +var dashboardFS embed.FS |
| 27 | + |
| 28 | +// dashboardRegisterOnMux registers the dashboard URLs on mux. |
| 29 | +func (a *App) dashboardRegisterOnMux(mux *http.ServeMux) { |
| 30 | + mux.Handle("/dashboard/", http.FileServer(http.FS(dashboardFS))) |
| 31 | + mux.Handle("/dashboard/third_party/bandchart/", http.StripPrefix("/dashboard/third_party/bandchart/", http.FileServer(http.FS(bandchart.FS)))) |
| 32 | + mux.HandleFunc("/dashboard/data.json", a.dashboardData) |
| 33 | +} |
| 34 | + |
| 35 | +// BenchmarkJSON contains the timeseries values for a single benchmark name + |
| 36 | +// unit. |
| 37 | +// |
| 38 | +// We could try to shoehorn this into benchfmt.Result, but that isn't really |
| 39 | +// the best fit for a graph. |
| 40 | +type BenchmarkJSON struct { |
| 41 | + Name string |
| 42 | + Unit string |
| 43 | + |
| 44 | + // These will be sorted by CommitDate. |
| 45 | + Values []ValueJSON |
| 46 | +} |
| 47 | + |
| 48 | +type ValueJSON struct { |
| 49 | + CommitHash string |
| 50 | + CommitDate time.Time |
| 51 | + |
| 52 | + // These are pre-formatted as percent change. |
| 53 | + Low float64 |
| 54 | + Center float64 |
| 55 | + High float64 |
| 56 | +} |
| 57 | + |
| 58 | +// fetch queries Influx to fill Values. Name and Unit must be set. |
| 59 | +// |
| 60 | +// WARNING: Name and Unit are not sanitized. DO NOT pass user input. |
| 61 | +func (b *BenchmarkJSON) fetch(ctx context.Context, qc api.QueryAPI) error { |
| 62 | + if b.Name == "" { |
| 63 | + return fmt.Errorf("Name must be set") |
| 64 | + } |
| 65 | + if b.Unit == "" { |
| 66 | + return fmt.Errorf("Unit must be set") |
| 67 | + } |
| 68 | + |
| 69 | + // TODO(prattmic): Adjust UI to comfortably display more than 7d of |
| 70 | + // data. |
| 71 | + query := fmt.Sprintf(` |
| 72 | +from(bucket: "perf") |
| 73 | + |> range(start: -7d) |
| 74 | + |> filter(fn: (r) => r["_measurement"] == "benchmark-result") |
| 75 | + |> filter(fn: (r) => r["name"] == "%s") |
| 76 | + |> filter(fn: (r) => r["unit"] == "%s") |
| 77 | + |> filter(fn: (r) => r["branch"] == "master") |
| 78 | + |> filter(fn: (r) => r["goos"] == "linux") |
| 79 | + |> filter(fn: (r) => r["goarch"] == "amd64") |
| 80 | + |> pivot(columnKey: ["_field"], rowKey: ["_time"], valueColumn: "_value") |
| 81 | + |> yield(name: "last") |
| 82 | +`, b.Name, b.Unit) |
| 83 | + |
| 84 | + ir, err := qc.Query(ctx, query) |
| 85 | + if err != nil { |
| 86 | + return fmt.Errorf("error performing query: %W", err) |
| 87 | + } |
| 88 | + |
| 89 | + for ir.Next() { |
| 90 | + rec := ir.Record() |
| 91 | + |
| 92 | + low, ok := rec.ValueByKey("low").(float64) |
| 93 | + if !ok { |
| 94 | + return fmt.Errorf("record %s low value got type %T want float64", rec, rec.ValueByKey("low")) |
| 95 | + } |
| 96 | + |
| 97 | + center, ok := rec.ValueByKey("center").(float64) |
| 98 | + if !ok { |
| 99 | + return fmt.Errorf("record %s center value got type %T want float64", rec, rec.ValueByKey("center")) |
| 100 | + } |
| 101 | + |
| 102 | + high, ok := rec.ValueByKey("high").(float64) |
| 103 | + if !ok { |
| 104 | + return fmt.Errorf("record %s high value got type %T want float64", rec, rec.ValueByKey("high")) |
| 105 | + } |
| 106 | + |
| 107 | + commit, ok := rec.ValueByKey("experiment-commit").(string) |
| 108 | + if !ok { |
| 109 | + return fmt.Errorf("record %s experiment-commit value got type %T want float64", rec, rec.ValueByKey("experiment-commit")) |
| 110 | + } |
| 111 | + |
| 112 | + b.Values = append(b.Values, ValueJSON{ |
| 113 | + CommitDate: rec.Time(), |
| 114 | + CommitHash: commit, |
| 115 | + Low: (low - 1) * 100, |
| 116 | + Center: (center - 1) * 100, |
| 117 | + High: (high - 1) * 100, |
| 118 | + }) |
| 119 | + } |
| 120 | + |
| 121 | + sort.Slice(b.Values, func(i, j int) bool { |
| 122 | + return b.Values[i].CommitDate.Before(b.Values[j].CommitDate) |
| 123 | + }) |
| 124 | + |
| 125 | + return nil |
| 126 | +} |
| 127 | + |
| 128 | +// search handles /dashboard/data.json. |
| 129 | +// |
| 130 | +// TODO(prattmic): Consider caching Influx results in-memory for a few mintures |
| 131 | +// to reduce load on Influx. |
| 132 | +func (a *App) dashboardData(w http.ResponseWriter, r *http.Request) { |
| 133 | + ctx := r.Context() |
| 134 | + |
| 135 | + start := time.Now() |
| 136 | + defer func() { |
| 137 | + log.Printf("Dashboard total query time: %s", time.Since(start)) |
| 138 | + }() |
| 139 | + |
| 140 | + ifxc, err := a.influxClient(ctx) |
| 141 | + if err != nil { |
| 142 | + log.Printf("Error getting Influx client: %v", err) |
| 143 | + http.Error(w, "Error connecting to Influx", 500) |
| 144 | + return |
| 145 | + } |
| 146 | + defer ifxc.Close() |
| 147 | + |
| 148 | + qc := ifxc.QueryAPI(influx.Org) |
| 149 | + |
| 150 | + // Keep benchmarks with the same name grouped together, which is |
| 151 | + // assumed by the JS. |
| 152 | + // |
| 153 | + // WARNING: Name and Unit are not sanitized. DO NOT pass user input. |
| 154 | + benchmarks := []BenchmarkJSON{ |
| 155 | + { |
| 156 | + Name: "Tile38WithinCircle100kmRequest", |
| 157 | + Unit: "sec/op", |
| 158 | + }, |
| 159 | + { |
| 160 | + Name: "Tile38WithinCircle100kmRequest", |
| 161 | + Unit: "p90-latency-sec", |
| 162 | + }, |
| 163 | + { |
| 164 | + Name: "Tile38WithinCircle100kmRequest", |
| 165 | + Unit: "average-RSS-bytes", |
| 166 | + }, |
| 167 | + { |
| 168 | + Name: "Tile38WithinCircle100kmRequest", |
| 169 | + Unit: "peak-RSS-bytes", |
| 170 | + }, |
| 171 | + { |
| 172 | + Name: "GoBuildKubelet", |
| 173 | + Unit: "sec/op", |
| 174 | + }, |
| 175 | + { |
| 176 | + Name: "GoBuildKubeletLink", |
| 177 | + Unit: "sec/op", |
| 178 | + }, |
| 179 | + } |
| 180 | + |
| 181 | + for i := range benchmarks { |
| 182 | + b := &benchmarks[i] |
| 183 | + // WARNING: Name and Unit are not sanitized. DO NOT pass user |
| 184 | + // input. |
| 185 | + if err := b.fetch(ctx, qc); err != nil { |
| 186 | + log.Printf("Error fetching benchmark %s/%s: %v", b.Name, b.Unit, err) |
| 187 | + http.Error(w, "Error fetching benchmark", 500) |
| 188 | + return |
| 189 | + } |
| 190 | + } |
| 191 | + |
| 192 | + w.Header().Set("Content-Type", "application/json") |
| 193 | + w.WriteHeader(http.StatusOK) |
| 194 | + e := json.NewEncoder(w) |
| 195 | + e.SetIndent("", "\t") |
| 196 | + e.Encode(benchmarks) |
| 197 | +} |
0 commit comments