From eb6fe1f3040d26ff03ccd142acb6aec1a814eeae Mon Sep 17 00:00:00 2001 From: k1LoW Date: Sun, 3 Sep 2023 16:26:55 +0900 Subject: [PATCH] If the metric is determined to be an integer, the metric is displayed as an integer --- report/custom.go | 32 +++++++++++++++++-- .../custom_metric_set_out.1.golden | 4 +-- .../custom_metric_set_out.2.golden | 4 +-- .../custom_metric_set_table.1.golden | 6 ++-- .../custom_metric_set_table.2.golden | 6 ++-- .../diff_custom_metric_set_table.0.golden | 6 ++-- .../diff_custom_metric_set_table.1.golden | 8 ++--- 7 files changed, 46 insertions(+), 20 deletions(-) diff --git a/report/custom.go b/report/custom.go index e710ec04..f217c511 100644 --- a/report/custom.go +++ b/report/custom.go @@ -57,7 +57,13 @@ func (s *CustomMetricSet) Table() string { d := []string{} for _, m := range s.Metrics { h = append(h, m.Name) - d = append(d, fmt.Sprintf("%.1f%s", m.Value, m.Unit)) + var v string + if isInt(m.Value) { + v = fmt.Sprintf("%d%s", int(m.Value), m.Unit) + } else { + v = fmt.Sprintf("%.1f%s", m.Value, m.Unit) + } + d = append(d, v) } buf := new(bytes.Buffer) _, _ = buf.WriteString(fmt.Sprintf("## %s\n\n", s.Name)) @@ -89,7 +95,13 @@ func (s *CustomMetricSet) Out(w io.Writer) error { table.SetColumnAlignment([]int{tablewriter.ALIGN_LEFT, tablewriter.ALIGN_RIGHT}) for _, m := range s.Metrics { - table.Rich([]string{m.Name, fmt.Sprintf("%.1f%s", m.Value, m.Unit)}, []tablewriter.Colors{tablewriter.Colors{tablewriter.Bold}, tablewriter.Colors{}}) + var v string + if isInt(m.Value) { + v = fmt.Sprintf("%d%s", int(m.Value), m.Unit) + } else { + v = fmt.Sprintf("%.1f%s", m.Value, m.Unit) + } + table.Rich([]string{m.Name, v}, []tablewriter.Colors{tablewriter.Colors{tablewriter.Bold}, tablewriter.Colors{}}) } table.Render() @@ -181,8 +193,22 @@ func (d *DiffCustomMetricSet) Table() string { table.SetHeader([]string{"", makeHeadTitleWithLink(d.B.report.Ref, d.B.report.Commit, nil), makeHeadTitleWithLink(d.A.report.Ref, d.A.report.Commit, nil), "+/-"}) for _, m := range d.Metrics { - table.Append([]string{fmt.Sprintf("**%s**", m.Name), fmt.Sprintf("%.1f%s", *m.B, m.customMetricB.Unit), fmt.Sprintf("%.1f%s", *m.A, m.customMetricA.Unit), fmt.Sprintf("%.1f%s", m.Diff, m.customMetricA.Unit)}) + var va, vb, diff string + if isInt(*m.A) && isInt(*m.B) { + va = fmt.Sprintf("%d%s", int(*m.A), m.customMetricA.Unit) + vb = fmt.Sprintf("%d%s", int(*m.B), m.customMetricB.Unit) + diff = fmt.Sprintf("%d%s", int(m.Diff), m.customMetricA.Unit) + } else { + va = fmt.Sprintf("%.1f%s", *m.A, m.customMetricA.Unit) + vb = fmt.Sprintf("%.1f%s", *m.B, m.customMetricB.Unit) + diff = fmt.Sprintf("%.1f%s", m.Diff, m.customMetricA.Unit) + } + table.Append([]string{fmt.Sprintf("**%s**", m.Name), vb, va, diff}) } table.Render() return strings.Replace(strings.Replace(buf.String(), "---|", "--:|", 4), "--:|", "---|", 1) } + +func isInt(v float64) bool { + return v == float64(int64(v)) +} diff --git a/testdata/custom_metrics/custom_metric_set_out.1.golden b/testdata/custom_metrics/custom_metric_set_out.1.golden index 5c67bb44..c6b87810 100644 --- a/testdata/custom_metrics/custom_metric_set_out.1.golden +++ b/testdata/custom_metrics/custom_metric_set_out.1.golden @@ -1,4 +1,4 @@ Benchmark-0 main (1234567) ------------------------------- - Count 1000.0 - ns/op 676.0ns/op + Count 1000 + ns/op 676ns/op diff --git a/testdata/custom_metrics/custom_metric_set_out.2.golden b/testdata/custom_metrics/custom_metric_set_out.2.golden index 06f2b8e3..5d71de81 100644 --- a/testdata/custom_metrics/custom_metric_set_out.2.golden +++ b/testdata/custom_metrics/custom_metric_set_out.2.golden @@ -1,4 +1,4 @@ Benchmark-1 main (1234567) ------------------------------- - Count 1500.0 - ns/op 1340.0ns/op + Count 1500 + ns/op 1340ns/op diff --git a/testdata/custom_metrics/custom_metric_set_table.1.golden b/testdata/custom_metrics/custom_metric_set_table.1.golden index a6b1ee21..9469d3a1 100644 --- a/testdata/custom_metrics/custom_metric_set_table.1.golden +++ b/testdata/custom_metrics/custom_metric_set_table.1.golden @@ -1,5 +1,5 @@ ## Benchmark-0 -| Count | ns/op | -|-------:|-----------:| -| 1000.0 | 676.0ns/op | +| Count | ns/op | +|------:|---------:| +| 1000 | 676ns/op | diff --git a/testdata/custom_metrics/custom_metric_set_table.2.golden b/testdata/custom_metrics/custom_metric_set_table.2.golden index 4c4d95c7..3eb6a25f 100644 --- a/testdata/custom_metrics/custom_metric_set_table.2.golden +++ b/testdata/custom_metrics/custom_metric_set_table.2.golden @@ -1,5 +1,5 @@ ## Benchmark-1 -| Count | ns/op | -|-------:|------------:| -| 1500.0 | 1340.0ns/op | +| Count | ns/op | +|------:|----------:| +| 1500 | 1340ns/op | diff --git a/testdata/custom_metrics/diff_custom_metric_set_table.0.golden b/testdata/custom_metrics/diff_custom_metric_set_table.0.golden index a6b1ee21..9469d3a1 100644 --- a/testdata/custom_metrics/diff_custom_metric_set_table.0.golden +++ b/testdata/custom_metrics/diff_custom_metric_set_table.0.golden @@ -1,5 +1,5 @@ ## Benchmark-0 -| Count | ns/op | -|-------:|-----------:| -| 1000.0 | 676.0ns/op | +| Count | ns/op | +|------:|---------:| +| 1000 | 676ns/op | diff --git a/testdata/custom_metrics/diff_custom_metric_set_table.1.golden b/testdata/custom_metrics/diff_custom_metric_set_table.1.golden index dfd60bb5..223acde0 100644 --- a/testdata/custom_metrics/diff_custom_metric_set_table.1.golden +++ b/testdata/custom_metrics/diff_custom_metric_set_table.1.golden @@ -1,6 +1,6 @@ ## Benchmark-0 -| | ([2345678](https://github.com/owner/repo/commit/2345678901)) | ([1234567](https://github.com/owner/repo/commit/1234567890)) | +/- | -|-----------|--------------------------------------------------------------:|--------------------------------------------------------------:|-----------:| -| **Count** | 9393.0 | 1000.0 | -8393.0 | -| **ns/op** | 456.0ns/op | 676.0ns/op | 220.0ns/op | +| | ([2345678](https://github.com/owner/repo/commit/2345678901)) | ([1234567](https://github.com/owner/repo/commit/1234567890)) | +/- | +|-----------|--------------------------------------------------------------:|--------------------------------------------------------------:|---------:| +| **Count** | 9393 | 1000 | -8393 | +| **ns/op** | 456ns/op | 676ns/op | 220ns/op |