Skip to content

Commit

Permalink
Merge pull request #270 from k1LoW/metadata
Browse files Browse the repository at this point in the history
Support metadata for custom metrics
  • Loading branch information
k1LoW authored Sep 23, 2023
2 parents bd079c7 + 83d683f commit 84fd096
Show file tree
Hide file tree
Showing 11 changed files with 295 additions and 7 deletions.
4 changes: 2 additions & 2 deletions cmd/comment.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,13 +71,13 @@ func createReportContent(ctx context.Context, c *config.Config, r, rPrev *report
table = d.Table()
fileTable = d.FileCoveagesTable(files)
for _, s := range d.CustomMetrics {
customTables = append(customTables, s.Table())
customTables = append(customTables, s.Table(), s.MetadataTable())
}
} else {
table = r.Table()
fileTable = r.FileCoveagesTable(files)
for _, s := range r.CustomMetrics {
customTables = append(customTables, s.Table())
customTables = append(customTables, s.Table(), s.MetadataTable())
}
}

Expand Down
80 changes: 75 additions & 5 deletions report/custom.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,18 @@ import (
//go:embed custom_metrics_schema.json
var schema []byte

type MetadataKV struct {
Key string `json:"key"`
Name string `json:"name,omitempty"`
Value string `json:"value"`
}

type CustomMetricSet struct {
Key string `json:"key"`
Name string `json:"name,omitempty"`
Metrics []*CustomMetric `json:"metrics"`
report *Report
Key string `json:"key"`
Name string `json:"name,omitempty"`
Metadata []*MetadataKV `json:"metadata,omitempty"`
Metrics []*CustomMetric `json:"metrics"`
report *Report
}

type CustomMetric struct {
Expand Down Expand Up @@ -79,6 +86,33 @@ func (s *CustomMetricSet) Table() string {
return strings.Replace(buf.String(), "---|", "--:|", len(h))
}

func (s *CustomMetricSet) MetadataTable() string {
if len(s.Metadata) == 0 {
return ""
}
var h []string
var d []string
for _, m := range s.Metadata {
if m.Name == "" {
m.Name = m.Key
}
h = append(h, m.Name)
d = append(d, m.Value)
}
buf := new(bytes.Buffer)
buf.WriteString("<details><summary>Metadata</summary>\n\n")
table := tablewriter.NewWriter(buf)
table.SetHeader(h)
table.SetAutoFormatHeaders(false)
table.SetAutoWrapText(false)
table.SetBorders(tablewriter.Border{Left: true, Top: false, Right: true, Bottom: false})
table.SetCenterSeparator("|")
table.Append(d)
table.Render()
buf.WriteString("\n</details>\n")
return strings.Replace(buf.String(), "---|", "--:|", len(h))
}

func (s *CustomMetricSet) Out(w io.Writer) error {
if len(s.Metrics) == 0 {
return nil
Expand Down Expand Up @@ -180,6 +214,13 @@ func (s *CustomMetricSet) Validate() error {
return m.Key
}))
}
if len(s.Metadata) != len(lo.UniqBy(s.Metadata, func(m *MetadataKV) string {
return m.Key
})) {
return fmt.Errorf("key of metadata must be unique: %s", lo.Map(s.Metadata, func(m *MetadataKV, _ int) string {
return m.Key
}))
}

return nil
}
Expand All @@ -199,7 +240,6 @@ func (d *DiffCustomMetricSet) Table() string {
table.SetBorders(tablewriter.Border{Left: true, Top: false, Right: true, Bottom: false})
table.SetCenterSeparator("|")
table.SetColumnAlignment([]int{tablewriter.ALIGN_LEFT, tablewriter.ALIGN_RIGHT, tablewriter.ALIGN_RIGHT, tablewriter.ALIGN_RIGHT})

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 {
var va, vb, diff string
Expand Down Expand Up @@ -239,6 +279,36 @@ func (d *DiffCustomMetricSet) Table() string {
return strings.Replace(strings.Replace(buf.String(), "---|", "--:|", 4), "--:|", "---|", 1)
}

func (d *DiffCustomMetricSet) MetadataTable() string {
if len(d.A.Metadata) == 0 {
return ""
}
if d.B == nil || len(d.B.Metadata) == 0 {
return d.A.MetadataTable()
}
buf := new(bytes.Buffer)
buf.WriteString("<details><summary>Metadata</summary>\n\n")
table := tablewriter.NewWriter(buf)
table.SetAutoFormatHeaders(false)
table.SetAutoWrapText(false)
table.SetBorders(tablewriter.Border{Left: true, Top: false, Right: true, Bottom: false})
table.SetCenterSeparator("|")
table.SetColumnAlignment([]int{tablewriter.ALIGN_LEFT, tablewriter.ALIGN_RIGHT, tablewriter.ALIGN_RIGHT, tablewriter.ALIGN_RIGHT})
table.SetHeader([]string{"", makeHeadTitleWithLink(d.B.report.Ref, d.B.report.Commit, nil), makeHeadTitleWithLink(d.A.report.Ref, d.A.report.Commit, nil)})
for _, ma := range d.A.Metadata {
mb, ok := lo.Find(d.B.Metadata, func(m *MetadataKV) bool {
return m.Key == ma.Key
})
if !ok {
mb = &MetadataKV{}
}
table.Append([]string{fmt.Sprintf("**%s**", ma.Key), mb.Value, ma.Value})
}
table.Render()
buf.WriteString("\n</details>\n")
return strings.Replace(strings.Replace(buf.String(), "---|", "--:|", 4), "--:|", "---|", 1)
}

func isInt(v float64) bool {
return v == float64(int64(v))
}
127 changes: 127 additions & 0 deletions report/custom_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,37 @@ func TestCustomMetricSetTable(t *testing.T) {
}
}

func TestCustomMetricSetMetadataTable(t *testing.T) {
tests := []struct {
s *CustomMetricSet
}{
{&CustomMetricSet{}},
{&CustomMetricSet{
Key: "benchmark_0",
Name: "Benchmark-0",
Metadata: []*MetadataKV{
{Key: "goos", Value: "darwin"},
{Key: "goarch", Value: "amd64"},
{Key: "pkg", Value: "github.com/k1LoW/octocov/metrics"},
{Key: "commit", Value: "a1b2c3d4e5f6"},
},
}},
}
for i, tt := range tests {
t.Run(fmt.Sprintf("%d", i), func(t *testing.T) {
got := tt.s.MetadataTable()
f := filepath.Join("custom_metrics", fmt.Sprintf("custom_metric_set_metadata_table.%d", i))
if os.Getenv("UPDATE_GOLDEN") != "" {
golden.Update(t, testdataDir(t), f, got)
return
}
if diff := golden.Diff(t, testdataDir(t), f, got); diff != "" {
t.Error(diff)
}
})
}
}

func TestCustomMetricSetOut(t *testing.T) {
tests := []struct {
s *CustomMetricSet
Expand Down Expand Up @@ -114,6 +145,33 @@ func TestCustomMetricsSetValidate(t *testing.T) {
Key: "key",
Metrics: []*CustomMetric{},
}, true},
{&CustomMetricSet{
Key: "key",
Metrics: []*CustomMetric{
{Key: "count", Value: 1000.0},
{Key: "count", Value: 1001.0},
},
}, true},
{&CustomMetricSet{
Key: "key",
Metrics: []*CustomMetric{
{Key: "count", Value: 1000.0},
},
Metadata: []*MetadataKV{
{Key: "goos", Value: "darwin"},
{Key: "goarch", Value: "amd64"},
},
}, false},
{&CustomMetricSet{
Key: "key",
Metrics: []*CustomMetric{
{Key: "count", Value: 1000.0},
},
Metadata: []*MetadataKV{
{Key: "goos", Value: "darwin"},
{Key: "goos", Value: "linux"},
},
}, true},
}
for i, tt := range tests {
t.Run(fmt.Sprintf("%d", i), func(t *testing.T) {
Expand Down Expand Up @@ -198,3 +256,72 @@ func TestDiffCustomMetricSetTable(t *testing.T) {
})
}
}

func TestDiffCustomMetricSetMetadataTable(t *testing.T) {
tests := []struct {
a *CustomMetricSet
b *CustomMetricSet
}{
{
&CustomMetricSet{
Key: "benchmark_0",
Name: "Benchmark-0",
Metadata: []*MetadataKV{
{Key: "goos", Value: "darwin"},
{Key: "goarch", Value: "amd64"},
},
report: &Report{
Ref: "main",
Commit: "1234567890",
covPaths: []string{"testdata/cover.out"},
},
},
nil,
},
{
&CustomMetricSet{
Key: "benchmark_0",
Name: "Benchmark-0",
Metadata: []*MetadataKV{
{Key: "goos", Value: "darwin"},
{Key: "goarch", Value: "amd64"},
},
report: &Report{
Ref: "main",
Commit: "1234567890",
covPaths: []string{"testdata/cover.out"},
},
},
&CustomMetricSet{
Key: "benchmark_0",
Name: "Benchmark-0",
Metadata: []*MetadataKV{
{Key: "goos", Value: "arwin"},
{Key: "goarch", Value: "arm64"},
},
report: &Report{
Ref: "main",
Commit: "2345678901",
covPaths: []string{"testdata/cover.out"},
},
},
},
}

t.Setenv("GITHUB_SERVER_URL", "https://github.com")
t.Setenv("GITHUB_REPOSITORY", "owner/repo")
for i, tt := range tests {
t.Run(fmt.Sprintf("%d", i), func(t *testing.T) {
d := tt.a.Compare(tt.b)
got := d.MetadataTable()
f := filepath.Join("custom_metrics", fmt.Sprintf("diff_custom_metric_set_metadata_table.%d", i))
if os.Getenv("UPDATE_GOLDEN") != "" {
golden.Update(t, testdataDir(t), f, got)
return
}
if diff := golden.Diff(t, testdataDir(t), f, got); diff != "" {
t.Error(diff)
}
})
}
}
21 changes: 21 additions & 0 deletions report/report_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,10 @@ func TestCollectCustomMetrics(t *testing.T) {
{
Key: "benchmark_0",
Name: "Benchmark-0 (this is custom metrics test)",
Metadata: []*MetadataKV{
{Key: "goos", Name: "GOOS", Value: "darwin"},
{Key: "goarch", Name: "GOARCH", Value: "amd64"},
},
Metrics: []*CustomMetric{
{Key: "N", Name: "Number of iterations", Value: 1000.0, Unit: ""},
{Key: "NsPerOp", Name: "Nanoseconds per iteration", Value: 676.5, Unit: " ns/op"},
Expand All @@ -132,6 +136,10 @@ func TestCollectCustomMetrics(t *testing.T) {
{
Key: "benchmark_0",
Name: "Benchmark-0 (this is custom metrics test)",
Metadata: []*MetadataKV{
{Key: "goos", Name: "GOOS", Value: "darwin"},
{Key: "goarch", Name: "GOARCH", Value: "amd64"},
},
Metrics: []*CustomMetric{
{Key: "N", Name: "Number of iterations", Value: 1000.0, Unit: ""},
{Key: "NsPerOp", Name: "Nanoseconds per iteration", Value: 676.5, Unit: " ns/op"},
Expand All @@ -140,6 +148,10 @@ func TestCollectCustomMetrics(t *testing.T) {
{
Key: "benchmark_1",
Name: "Benchmark-1 (this is custom metrics test)",
Metadata: []*MetadataKV{
{Key: "goos", Name: "GOOS", Value: "darwin"},
{Key: "goarch", Name: "GOARCH", Value: "amd64"},
},
Metrics: []*CustomMetric{
{Key: "N", Name: "Number of iterations", Value: 1500.0, Unit: ""},
{Key: "NsPerOp", Name: "Nanoseconds per iteration", Value: 1345.0, Unit: " ns/op"},
Expand All @@ -156,6 +168,10 @@ func TestCollectCustomMetrics(t *testing.T) {
{
Key: "benchmark_0",
Name: "Benchmark-0 (this is custom metrics test)",
Metadata: []*MetadataKV{
{Key: "goos", Name: "GOOS", Value: "darwin"},
{Key: "goarch", Name: "GOARCH", Value: "amd64"},
},
Metrics: []*CustomMetric{
{Key: "N", Name: "Number of iterations", Value: 1000.0, Unit: ""},
{Key: "NsPerOp", Name: "Nanoseconds per iteration", Value: 676.5, Unit: " ns/op"},
Expand All @@ -164,6 +180,10 @@ func TestCollectCustomMetrics(t *testing.T) {
{
Key: "benchmark_1",
Name: "Benchmark-1 (this is custom metrics test)",
Metadata: []*MetadataKV{
{Key: "goos", Name: "GOOS", Value: "darwin"},
{Key: "goarch", Name: "GOARCH", Value: "amd64"},
},
Metrics: []*CustomMetric{
{Key: "N", Name: "Number of iterations", Value: 1500.0, Unit: ""},
{Key: "NsPerOp", Name: "Nanoseconds per iteration", Value: 1345.0, Unit: " ns/op"},
Expand All @@ -185,6 +205,7 @@ func TestCollectCustomMetrics(t *testing.T) {
t.Fatal(err)
}
}
return
}
for k, v := range tt.envs {
t.Setenv(k, v)
Expand Down
12 changes: 12 additions & 0 deletions testdata/custom_metrics/benchmark_0.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,18 @@
{
"key": "benchmark_0",
"name": "Benchmark-0 (this is custom metrics test)",
"metadata": [
{
"key": "goos",
"name": "GOOS",
"value": "darwin"
},
{
"key": "goarch",
"name": "GOARCH",
"value": "amd64"
}
],
"metrics": [
{
"key": "N",
Expand Down
24 changes: 24 additions & 0 deletions testdata/custom_metrics/benchmark_0_1.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,18 @@
{
"key": "benchmark_0",
"name": "Benchmark-0 (this is custom metrics test)",
"metadata": [
{
"key": "goos",
"name": "GOOS",
"value": "darwin"
},
{
"key": "goarch",
"name": "GOARCH",
"value": "amd64"
}
],
"metrics": [
{
"key": "N",
Expand All @@ -19,6 +31,18 @@
{
"key": "benchmark_1",
"name": "Benchmark-1 (this is custom metrics test)",
"metadata": [
{
"key": "goos",
"name": "GOOS",
"value": "darwin"
},
{
"key": "goarch",
"name": "GOARCH",
"value": "amd64"
}
],
"metrics": [
{
"key": "N",
Expand Down
Loading

0 comments on commit 84fd096

Please sign in to comment.