Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improving readability of benchmark scores #308

Merged
merged 31 commits into from
Jan 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
ce63331
Using the x/text package for number formatting
k2tzumi Jan 2, 2024
71bfa2a
Make custom metrics numbers easy to read
k2tzumi Jan 2, 2024
ba6759f
Test the added functions
k2tzumi Jan 2, 2024
63ae99b
Modify expectations
k2tzumi Jan 2, 2024
fa3133a
Merge branch 'k1LoW:main' into main
k2tzumi Jan 14, 2024
b7346e5
Append locale
k2tzumi Jan 3, 2024
9c2fc21
Isolate formatter
k2tzumi Jan 3, 2024
d236639
Set locale
k2tzumi Jan 3, 2024
ea34599
Revert & append
k2tzumi Jan 3, 2024
22cfc10
If it can be converted to int, it is processed as int.
k2tzumi Jan 3, 2024
0debbc9
Append config test
k2tzumi Jan 3, 2024
2b06dcd
EOL
k2tzumi Jan 4, 2024
cd2283a
Define option with Locale
k2tzumi Jan 13, 2024
8342745
dip
k2tzumi Jan 13, 2024
a1a3180
Transferring the conversion process to report
k2tzumi Jan 13, 2024
944fb24
Conversion processing by resport.
k2tzumi Jan 13, 2024
0ed3542
Allow options to carry over to reports
k2tzumi Jan 13, 2024
89592ca
Test modifications due to implementation changes
k2tzumi Jan 13, 2024
3f1cc7f
Fix
k2tzumi Jan 13, 2024
918c0c9
Fix lint
k2tzumi Jan 13, 2024
0a683e7
Testing Functional Options Pattern
k2tzumi Jan 14, 2024
cf30bd8
Nil guard
k2tzumi Jan 14, 2024
2edb674
UPDATE_GOLDEN
k2tzumi Jan 14, 2024
5099def
Fix
k2tzumi Jan 14, 2024
776d499
Fix
k2tzumi Jan 14, 2024
ad8c7d2
Fix panic
k2tzumi Jan 21, 2024
c79bfab
Fix gostyle
k2tzumi Jan 21, 2024
6c79005
Fix panic
k2tzumi Jan 21, 2024
7844b9f
Remove unnecessary nil checks
k2tzumi Jan 21, 2024
98cba55
Rename
k2tzumi Jan 22, 2024
19b968f
Remove unnecessary struct variables
k2tzumi Jan 22, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,12 @@ import (
"strings"
"time"

"github.com/k1LoW/octocov/badge"
"github.com/k1LoW/octocov/central"
"github.com/k1LoW/octocov/config"
"github.com/k1LoW/octocov/datastore"
"github.com/k1LoW/octocov/gh"
"github.com/k1LoW/octocov/internal"
"github.com/k1LoW/octocov/badge"
"github.com/k1LoW/octocov/report"
"github.com/k1LoW/octocov/version"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -161,7 +161,7 @@ var rootCmd = &cobra.Command{
return nil
}

r, err := report.New(c.Repository)
r, err := report.New(c.Repository, report.Locale(c.Locale))
if err != nil {
return err
}
Expand Down Expand Up @@ -354,7 +354,7 @@ var rootCmd = &cobra.Command{
}
}
if c.Diff.Path != "" {
rt, err := report.New(c.Repository)
rt, err := report.New(c.Repository, report.Locale(c.Locale))
if err != nil {
return err
}
Expand Down Expand Up @@ -502,7 +502,7 @@ func printMetrics(cmd *cobra.Command) error {
c.CodeToTestRatio = nil
c.TestExecutionTime = nil
}
r, err := report.New(c.Repository)
r, err := report.New(c.Repository, report.Locale(c.Locale))
if err != nil {
return err
}
Expand Down
28 changes: 14 additions & 14 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (
"github.com/k1LoW/duration"
"github.com/k1LoW/expand"
"github.com/k1LoW/octocov/gh"
"github.com/k1LoW/octocov/report"
"golang.org/x/text/language"
)

const defaultBadgesDatastore = "local://reports"
Expand Down Expand Up @@ -48,6 +48,7 @@ type Config struct {
Body *Body `yaml:"body,omitempty"`
Diff *Diff `yaml:"diff,omitempty"`
Timeout time.Duration `yaml:"timeout,omitempty"`
Locale *language.Tag `yaml:"locale,omitempty"`
GitRoot string `yaml:"-"`
// working directory
wd string
Expand Down Expand Up @@ -190,34 +191,33 @@ func (c *Config) Loaded() bool {
return c.path != ""
}

func (c *Config) Acceptable(r, rPrev *report.Report) error {
type Reporter interface {
CoveragePercent() float64
CodeToTestRatioRatio() float64
TestExecutionTimeNano() float64
IsMeasuredTestExecutionTime() bool
}

func (c *Config) Acceptable(r, rPrev Reporter) error {
var result *multierror.Error
if err := c.CoverageConfigReady(); err == nil {
prev := 0.0
if rPrev != nil {
prev = rPrev.CoveragePercent()
}
prev := rPrev.CoveragePercent()
if err := coverageAcceptable(r.CoveragePercent(), prev, c.Coverage.Acceptable); err != nil {
result = multierror.Append(result, err)
}
}

if err := c.CodeToTestRatioConfigReady(); err == nil {
prev := 0.0
if rPrev != nil {
prev = rPrev.CodeToTestRatioRatio()
}
prev := rPrev.CodeToTestRatioRatio()
if err := codeToTestRatioAcceptable(r.CodeToTestRatioRatio(), prev, c.CodeToTestRatio.Acceptable); err != nil {
result = multierror.Append(result, err)
}
}

if err := c.TestExecutionTimeConfigReady(); err == nil {
prev := largeEnoughTime
if rPrev != nil {
if rPrev.IsMeasuredTestExecutionTime() {
prev = rPrev.TestExecutionTimeNano()
}
if rPrev.IsMeasuredTestExecutionTime() {
prev = rPrev.TestExecutionTimeNano()
}

if err := testExecutionTimeAcceptable(r.TestExecutionTimeNano(), prev, c.TestExecutionTime.Acceptable); err != nil {
Expand Down
35 changes: 35 additions & 0 deletions config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"time"

"github.com/google/go-cmp/cmp"
"golang.org/x/text/language"
)

func TestMain(m *testing.M) {
Expand Down Expand Up @@ -92,6 +93,40 @@ func TestLoadCentralPush(t *testing.T) {
}
}

func TestLoadLocale(t *testing.T) {
tests := []struct {
path string
want *language.Tag
wantError bool
}{
{"locale_nothing.yml", nil, false},
{"locale_empty.yml", nil, false},
{"locale_ja.yml", &language.Japanese, false},
{"locale_ja_uppercase.yml", &language.Japanese, false},
{"locale_fr.yml", &language.French, false},
{"locale_unkown.yml", nil, true},
}
for _, tt := range tests {
c := New()
t.Run(fmt.Sprintf("%v", tt.path), func(t *testing.T) {
p := filepath.Join(testdataDir(t), tt.path)
if err := c.Load(p); err != nil {
if tt.wantError {
return
}
t.Fatal(err)
}
got := c.Locale
if tt.want == nil && got == nil {
return
}
if diff := cmp.Diff(got.String(), tt.want.String(), nil); diff != "" {
t.Error(diff)
}
})
}
}

func TestCoveragePaths(t *testing.T) {
tests := []struct {
paths []string
Expand Down
1 change: 1 addition & 0 deletions config/testdata/locale_empty.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
locale:
1 change: 1 addition & 0 deletions config/testdata/locale_fr.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
locale: "fr"
1 change: 1 addition & 0 deletions config/testdata/locale_ja.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
locale: "ja"
1 change: 1 addition & 0 deletions config/testdata/locale_ja_uppercase.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
locale: "JA"
Empty file.
1 change: 1 addition & 0 deletions config/testdata/locale_unknown.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
locale: "unknown"
11 changes: 11 additions & 0 deletions config/yaml.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (

"github.com/goccy/go-yaml"
"github.com/k1LoW/duration"
"golang.org/x/text/language"
)

var commentRe = regexp.MustCompile(`(?m)^comment:`)
Expand All @@ -25,6 +26,7 @@ func (c *Config) UnmarshalYAML(data []byte) error {
Body *Body `yaml:"body,omitempty"`
Diff *Diff `yaml:"diff,omitempty"`
Timeout string `yaml:"timeout,omitempty"`
Locale string `yaml:"locale,omitempty"`
}{}
err := yaml.Unmarshal(data, &s)
if err != nil {
Expand Down Expand Up @@ -85,6 +87,15 @@ func (c *Config) UnmarshalYAML(data []byte) error {
c.Push = v
}

if s.Locale != "" {
l, err := language.Parse(s.Locale)
if err != nil {
return err
}

c.Locale = &l
}

return nil
}

Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ require (
golang.org/x/exp v0.0.0-20230213192124-5e25df0256eb
golang.org/x/image v0.10.0
golang.org/x/oauth2 v0.7.0
golang.org/x/text v0.14.0
golang.org/x/tools v0.13.0
google.golang.org/api v0.114.0
gopkg.in/ini.v1 v1.67.0
Expand Down Expand Up @@ -115,7 +116,6 @@ require (
golang.org/x/net v0.19.0 // indirect
golang.org/x/sync v0.3.0 // indirect
golang.org/x/sys v0.15.0 // indirect
golang.org/x/text v0.14.0 // indirect
golang.org/x/xerrors v0.0.0-20220907171357-04be3eba64a2 // indirect
google.golang.org/appengine v1.6.7 // indirect
google.golang.org/genproto v0.0.0-20230410155749-daa745c078e1 // indirect
Expand Down
78 changes: 37 additions & 41 deletions report/custom.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,19 +64,17 @@ func (s *CustomMetricSet) Table() string {
if len(s.Metrics) >= swapXYMin {
return s.tableSwaped()
}
report := s.report
if report == nil {
report = &Report{}
}
var (
h []string
d []string
)
for _, m := range s.Metrics {
h = append(h, m.Name)
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)
d = append(d, fmt.Sprintf("%s%s", report.convertFormat(m.Value), m.Unit))
}
buf := new(bytes.Buffer)
_, _ = buf.WriteString(fmt.Sprintf("## %s\n\n", s.Name)) //nostyle:handlerrors
Expand All @@ -100,14 +98,10 @@ func (s *CustomMetricSet) tableSwaped() string {
table.SetBorders(tablewriter.Border{Left: true, Top: false, Right: true, Bottom: false})
table.SetCenterSeparator("|")
table.SetHeader([]string{"", makeHeadTitleWithLink(s.report.Ref, s.report.Commit, nil)})

report := s.report
for _, m := range s.Metrics {
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.Append([]string{m.Name, v})
table.Append([]string{m.Name, fmt.Sprintf("%s%s", report.convertFormat(m.Value), m.Unit)})
}
table.Render()
return strings.Replace(buf.String(), "---|", "--:|", len(s.Metrics))
Expand Down Expand Up @@ -159,17 +153,16 @@ func (s *CustomMetricSet) Out(w io.Writer) error {
table.SetBorder(false)
table.SetColumnAlignment([]int{tablewriter.ALIGN_LEFT, tablewriter.ALIGN_RIGHT})

report := s.report
if report == nil {
report = &Report{}
}

for _, m := range s.Metrics {
if m.Name == "" {
m.Name = m.Key
}
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.Rich([]string{m.Name, fmt.Sprintf("%s%s", report.convertFormat(m.Value), m.Unit)}, []tablewriter.Colors{tablewriter.Colors{tablewriter.Bold}, tablewriter.Colors{}})
}

table.Render()
Expand Down Expand Up @@ -276,37 +269,29 @@ func (d *DiffCustomMetricSet) Table() string {
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), "+/-"})
report := d.report()

for _, m := range d.Metrics {
var va, vb, diff string
switch {
case m.A == nil && m.B == nil:
continue
case m.A != nil && m.B == nil:
vb = ""
if isInt(*m.A) {
va = fmt.Sprintf("%d%s", int(*m.A), m.customMetricA.Unit)
diff = fmt.Sprintf("%d%s", int(m.Diff), m.customMetricA.Unit)
} else {
va = fmt.Sprintf("%.1f%s", *m.A, m.customMetricA.Unit)
diff = fmt.Sprintf("%.1f%s", m.Diff, m.customMetricA.Unit)
}
va = fmt.Sprintf("%s%s", report.convertFormat(*m.A), m.customMetricA.Unit)
diff = fmt.Sprintf("%s%s", report.convertFormat(m.Diff), m.customMetricA.Unit)
case m.A == nil && m.B != nil:
va = ""
if isInt(*m.B) {
vb = fmt.Sprintf("%d%s", int(*m.B), m.customMetricB.Unit)
diff = fmt.Sprintf("%d%s", int(m.Diff), m.customMetricB.Unit)
} else {
vb = fmt.Sprintf("%.1f%s", *m.B, m.customMetricB.Unit)
diff = fmt.Sprintf("%.1f%s", m.Diff, m.customMetricB.Unit)
}
vb = fmt.Sprintf("%s%s", report.convertFormat(*m.B), m.customMetricB.Unit)
diff = fmt.Sprintf("%s%s", report.convertFormat(m.Diff), m.customMetricB.Unit)
case 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)
va = fmt.Sprintf("%s%s", report.convertFormat(*m.A), m.customMetricA.Unit)
vb = fmt.Sprintf("%s%s", report.convertFormat(*m.B), m.customMetricB.Unit)
diff = fmt.Sprintf("%s%s", report.convertFormat(m.Diff), m.customMetricA.Unit)
default:
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)
va = fmt.Sprintf("%s%s", report.convertFormat(*m.A), m.customMetricA.Unit)
vb = fmt.Sprintf("%s%s", report.convertFormat(*m.B), m.customMetricB.Unit)
diff = fmt.Sprintf("%s%s", report.convertFormat(m.Diff), m.customMetricA.Unit)
}
if m.Name == "" {
m.Name = m.Key
Expand Down Expand Up @@ -350,6 +335,17 @@ func (d *DiffCustomMetricSet) MetadataTable() string {
return strings.Replace(strings.Replace(buf.String(), "---|", "--:|", 4), "--:|", "---|", 1)
}

func (d *DiffCustomMetricSet) report() *Report {
if d.A != nil && d.A.report != nil {
return d.A.report
}
if d.B != nil && d.B.report != nil {
return d.B.report
}

return &Report{}
}

func isInt(v float64) bool {
return v == float64(int64(v))
}
Loading
Loading