|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "bufio" |
| 5 | + "fmt" |
| 6 | + "io" |
| 7 | + "os" |
| 8 | + "sort" |
| 9 | + "strconv" |
| 10 | + "strings" |
| 11 | + |
| 12 | + "golang.org/x/perf/benchstat" |
| 13 | +) |
| 14 | + |
| 15 | +func main() { |
| 16 | + if len(os.Args) < 2 { |
| 17 | + panic("need to specify input file") |
| 18 | + } |
| 19 | + fIn, err := os.Open(os.Args[1]) |
| 20 | + if err != nil { |
| 21 | + panic(err) |
| 22 | + } |
| 23 | + |
| 24 | + c := benchstat.Collection{} |
| 25 | + if err := c.AddFile("config1", fIn); err != nil { |
| 26 | + panic(err) |
| 27 | + } |
| 28 | + tables := c.Tables() |
| 29 | + |
| 30 | + reports, err := genReports(tables) |
| 31 | + if err != nil { |
| 32 | + panic(err) |
| 33 | + } |
| 34 | + if err := processReports(reports); err != nil { |
| 35 | + panic(err) |
| 36 | + } |
| 37 | + |
| 38 | + fOut, err := os.Create("out.csv") |
| 39 | + if err != nil { |
| 40 | + panic(err) |
| 41 | + } |
| 42 | + defer fOut.Close() |
| 43 | + if err := writeReports(reports, fOut); err != nil { |
| 44 | + panic(err) |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +// All metrics for all benchmarks for one HAMT configuration |
| 49 | +type HAMTReport struct { |
| 50 | + ID string |
| 51 | + Benchmarks map[string]Benchmark |
| 52 | +} |
| 53 | + |
| 54 | +func (hr *HAMTReport) Write(datawriter *bufio.Writer) error { |
| 55 | + // Write title |
| 56 | + if _, err := datawriter.WriteString(hr.ID + "\n"); err != nil { |
| 57 | + return err |
| 58 | + } |
| 59 | + |
| 60 | + // Write benchmarks |
| 61 | + for _, benchmark := range hr.Benchmarks { |
| 62 | + if err := benchmark.Write(datawriter); err != nil { |
| 63 | + return err |
| 64 | + } |
| 65 | + } |
| 66 | + return nil |
| 67 | +} |
| 68 | + |
| 69 | +type Benchmark struct { |
| 70 | + Name string |
| 71 | + Metrics map[string]struct{} |
| 72 | + Sweep map[int]map[string]float64 // map[Bitwidth]MetricsMap |
| 73 | +} |
| 74 | + |
| 75 | +func (b *Benchmark) Write(datawriter *bufio.Writer) error { |
| 76 | + // Get sorted keys |
| 77 | + metrics := make([]string, 0) |
| 78 | + for metric := range b.Metrics { |
| 79 | + metrics = append(metrics, metric) |
| 80 | + } |
| 81 | + sort.Strings(metrics) |
| 82 | + |
| 83 | + bws := make([]int, 0) |
| 84 | + for bw := range b.Sweep { |
| 85 | + bws = append(bws, bw) |
| 86 | + } |
| 87 | + sort.Ints(bws) |
| 88 | + |
| 89 | + // Write headers |
| 90 | + headers := []string{"Benchmark", "Bitwidth"} |
| 91 | + headers = append(headers, metrics...) |
| 92 | + if _, err := datawriter.WriteString(strings.Join(headers, ",")); err != nil { |
| 93 | + return err |
| 94 | + } |
| 95 | + if _, err := datawriter.WriteString("\n"); err != nil { |
| 96 | + return err |
| 97 | + } |
| 98 | + |
| 99 | + // Write sweeps |
| 100 | + for _, bw := range bws { |
| 101 | + if _, err := datawriter.WriteString(b.Name); err != nil { |
| 102 | + return err |
| 103 | + } |
| 104 | + if _, err := datawriter.WriteString("," + strconv.Itoa(bw)); err != nil { |
| 105 | + return err |
| 106 | + } |
| 107 | + for _, metric := range metrics { |
| 108 | + m, found := b.Sweep[bw][metric] |
| 109 | + if !found { |
| 110 | + // empty column |
| 111 | + if _, err := datawriter.WriteString(","); err != nil { |
| 112 | + return err |
| 113 | + } |
| 114 | + continue |
| 115 | + } |
| 116 | + if _, err := datawriter.WriteString(fmt.Sprintf(",%.3f", m)); err != nil { |
| 117 | + return err |
| 118 | + } |
| 119 | + } |
| 120 | + if _, err := datawriter.WriteString("\n"); err != nil { |
| 121 | + return err |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 125 | + return nil |
| 126 | +} |
| 127 | + |
| 128 | +func writeReports(reports map[string]*HAMTReport, w io.Writer) error { |
| 129 | + datawriter := bufio.NewWriter(w) |
| 130 | + for _, report := range reports { |
| 131 | + if err := report.Write(datawriter); err != nil { |
| 132 | + return err |
| 133 | + } |
| 134 | + if _, err := datawriter.WriteString("\n\n"); err != nil { |
| 135 | + return err |
| 136 | + } |
| 137 | + } |
| 138 | + return datawriter.Flush() |
| 139 | +} |
| 140 | + |
| 141 | +func genReports(tables []*benchstat.Table) (map[string]*HAMTReport, error) { |
| 142 | + reports := make(map[string]*HAMTReport) |
| 143 | + for _, table := range tables { |
| 144 | + for _, row := range table.Rows { |
| 145 | + if len(row.Metrics) == 0 { |
| 146 | + continue |
| 147 | + } |
| 148 | + if len(row.Metrics) >= 2 { |
| 149 | + panic("don't know how to handle lots of metrics") |
| 150 | + } |
| 151 | + good, bname, hamtID, bw := parseBench(row.Benchmark) |
| 152 | + if !good { |
| 153 | + continue |
| 154 | + } |
| 155 | + fmt.Printf("bname: %s, hamtID: %s, bw: %d\n", bname, hamtID, bw) |
| 156 | + |
| 157 | + if _, found := reports[hamtID]; !found { |
| 158 | + reports[hamtID] = &HAMTReport{ |
| 159 | + ID: hamtID, |
| 160 | + Benchmarks: make(map[string]Benchmark), |
| 161 | + } |
| 162 | + } |
| 163 | + if _, found := reports[hamtID].Benchmarks[bname]; !found { |
| 164 | + reports[hamtID].Benchmarks[bname] = Benchmark{ |
| 165 | + Name: bname, |
| 166 | + Metrics: make(map[string]struct{}), |
| 167 | + Sweep: make(map[int]map[string]float64), |
| 168 | + } |
| 169 | + } |
| 170 | + if _, found := reports[hamtID].Benchmarks[bname].Sweep[bw]; !found { |
| 171 | + reports[hamtID].Benchmarks[bname].Sweep[bw] = make(map[string]float64) |
| 172 | + } |
| 173 | + reports[hamtID].Benchmarks[bname].Metrics[table.Metric] = struct{}{} |
| 174 | + reports[hamtID].Benchmarks[bname].Sweep[bw][table.Metric] = row.Metrics[0].Mean |
| 175 | + } |
| 176 | + } |
| 177 | + return reports, nil |
| 178 | +} |
| 179 | + |
| 180 | +const GasCostGet = 114617 |
| 181 | +const GasCostPut = 353640 |
| 182 | +const GasCostPerBytePut = 1300 |
| 183 | + |
| 184 | +const EffectiveTimeGet = 11462 |
| 185 | +const EffectiveTimePut = 35364 |
| 186 | + |
| 187 | +const getsKey = "getEvts" |
| 188 | +const putsKey = "putsEvts" |
| 189 | +const bytesPutKey = "bytesPut" |
| 190 | +const timeKey = "time/op" |
| 191 | + |
| 192 | +// Add GasCost, Time/1000, and EffectiveRuntime |
| 193 | +func processReports(reports map[string]*HAMTReport) error { |
| 194 | + for _, report := range reports { |
| 195 | + for _, bench := range report.Benchmarks { |
| 196 | + bench.Metrics["gasCost"] = struct{}{} |
| 197 | + bench.Metrics["time/1000"] = struct{}{} |
| 198 | + bench.Metrics["effectiveRuntime"] = struct{}{} |
| 199 | + |
| 200 | + for _, row := range bench.Sweep { |
| 201 | + row["gasCost"] = row[getsKey]*GasCostGet + row[putsKey]*GasCostPut + row[bytesPutKey]*GasCostPerBytePut |
| 202 | + row["time/1000"] = row[timeKey] / float64(1000) |
| 203 | + row["effectiveRuntime"] = row["time/1000"] + row[getsKey]*EffectiveTimeGet + row[putsKey]*EffectiveTimePut |
| 204 | + } |
| 205 | + } |
| 206 | + } |
| 207 | + return nil |
| 208 | +} |
| 209 | + |
| 210 | +// Unedited hamt benchmark strings are ugly but consistent |
| 211 | +// BenchmarkName/hamtID_--_bw=Bitwidth-8 |
| 212 | +// This can be simplified once we clean up generation |
| 213 | +func parseBench(bstatBench string) (bool, string, string, int) { |
| 214 | + slashSeps := strings.Split(bstatBench, "/") |
| 215 | + if len(slashSeps) != 2 { |
| 216 | + return false, "", "", 0 |
| 217 | + } |
| 218 | + bname := slashSeps[0] |
| 219 | + |
| 220 | + uScoreSeps := strings.Split(slashSeps[1], "_") |
| 221 | + if len(uScoreSeps) != 3 { |
| 222 | + return false, "", "", 0 |
| 223 | + } |
| 224 | + hamtID := uScoreSeps[0] |
| 225 | + |
| 226 | + bitwidthStr := strings.TrimPrefix(uScoreSeps[2], "bw=") |
| 227 | + bitwidthStr = strings.TrimSuffix(bitwidthStr, "-8") |
| 228 | + bw, err := strconv.Atoi(bitwidthStr) |
| 229 | + if err != nil { |
| 230 | + panic(err) |
| 231 | + } |
| 232 | + return true, bname, hamtID, bw |
| 233 | +} |
0 commit comments