10000 fix markdown issue with csv escaping by WillAbides · Pull Request #9 · WillAbides/benchdiff · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

fix markdown issue with csv escaping #9

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 GitHu 8000 b”, 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

Merged
merged 1 commit into from
Jan 9, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ go 1.15
require (
github.com/alecthomas/kong v0.2.12
github.com/mattn/go-runewidth v0.0.9 // indirect
github.com/monochromegane/mdt v0.0.0-20150922125922-3ae3b51eea5d
github.com/olekukonko/tablewriter v0.0.4
github.com/stretchr/testify v1.5.1
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2
golang.org/x/perf v0.0.0-20201207232921-bdcc6220ee90
Expand Down
5 changes: 3 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,12 @@ github.com/gonum/internal v0.0.0-20181124074243-f884aa714029/go.mod h1:Pu4dmpkhS
github.com/gonum/lapack v0.0.0-20181123203213-e4cdc5a0bff9/go.mod h1:XA3DeT6rxh2EAE789SSiSJNqxPaC0aE9J8NTOI0Jo/A=
github.com/gonum/matrix v0.0.0-20181209220409-c518dec07be9/go.mod h1:0EXg4mc1CNP0HCqCz+K4ts155PXIlUywf0wqN+GfPZw=
github.com/googleapis/gax-go v0.0.0-20161107002406-da06d194a00e/go.mod h1:SFVmujtThgffbyetf+mdk2eWhX2bMyUtNHzFKcPA9HY=
github.com/mattn/go-runewidth v0.0.7/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m2gUSrubnMI=
github.com/mattn/go-runewidth v0.0.9 h1:Lm995f3rfxdpd6TSmuVCHVb/QhupuXlYr8sCI/QdE+0=
github.com/mattn/go-runewidth v0.0.9/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m2gUSrubnMI=
github.com/mattn/go-sqlite3 v1.14.5/go.mod h1:WVKg1VTActs4Qso6iwGbiFih2UIHo0ENGwNd0Lj+XmI=
github.com/monochromegane/mdt v0.0.0-20150922125922-3ae3b51eea5d h1:/C31REOLkRFhj5eQPxXhqqSos9vKM3bUuszTDabNuuY=
github.com/monochromegane/mdt v0.0.0-20150922125922-3ae3b51eea5d/go.mod h1:swv9I1N84fPPcZxDL25kWMN5FL8J5/uJGZdstRFC1hs=
github.com/olekukonko/tablewriter v0.0.4 h1:vHD/YYe1Wolo78koG299f7V/VAS08c6IpCLn+Ejf/w8=
github.com/olekukonko/tablewriter v0.0.4/go.mod h1:zq6QwlOf5SlnkVbMSr5EoBv3636FWnp+qbPhuoO21uA=
github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I=
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
Expand Down
61 changes: 34 additions & 27 deletions pkg/benchstatter/markdown.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@ import (
"bufio"
"bytes"
"encoding/csv"
"fmt"
"io"
"regexp"
"strconv"
"strings"

"github.com/monochromegane/mdt"
"github.com/olekukonko/tablewriter"
"golang.org/x/perf/benchstat"
)

Expand Down Expand Up @@ -67,13 +68,14 @@ func csv2Markdown(data []byte) ([]string, error) {
}
var mdTables []string
for _, csvTable := range csvTables {
var buf bytes.Buffer
err = reFloatCsv(&buf, bytes.NewReader(csvTable))
var rows [][]string
rows, err = buildRows(csvTable)
if err != nil {
return nil, err
}

var mdTable string
mdTable, err = mdt.Convert("", &buf)
mdTable, err = buildMD(rows)
if err != nil {
return nil, err
}
Expand All @@ -82,40 +84,45 @@ func csv2Markdown(data []byte) ([]string, error) {
return mdTables, nil
}

func buildMD(rows [][]string) (string, error) {
var buf bytes.Buffer
if len(rows) < 2 {
return "", fmt.Errorf("need at least one row plus header")
}
hRow := rows[0]
rows = rows[1:]
table := tablewriter.NewWriter(&buf)
table.SetHeader(hRow)
table.SetAutoFormatHeaders(false)
table.SetBorders(tablewriter.Border{Left: true, Top: false, Right: true, Bottom: false})
table.SetCenterSeparator("|")
table.AppendBulk(rows)
table.Render()
return buf.String(), nil
}

// MarkdownFormatterOptions options for a markdown OutputFormatter
type MarkdownFormatterOptions struct {
CSVFormatterOptions
}

func reFloatCsv(dest io.Writer, src io.Reader) error {
csvSrc := csv.NewReader(src)
csvSrc.FieldsPerRecord = -1
csvDest := csv.NewWriter(dest)
var err error
var row []string
for {
row, err = csvSrc.Read()
if err != nil {
break
}
for i, val := range row {
func buildRows(src []byte) ([][]string, error) {
cRdr := csv.NewReader(bytes.NewReader(src))
cRdr.FieldsPerRecord = -1
rows, err := cRdr.ReadAll()
if err != nil {
return nil, err
}
for i := range rows {
for j, val := range rows[i] {
f, fErr := strconv.ParseFloat(val, 64)
if fErr != nil {
continue
}
row[i] = strconv.FormatFloat(f, 'f', -1, 64)
}
err = csvDest.Write(row)
if err != nil {
break
rows[i][j] = strconv.FormatFloat(f, 'f', -1, 64)
}
}
if err != io.EOF {
return err
}

csvDest.Flush()
return csvDest.Error()
return rows, nil
}

// FormatMarkdown formats benchstat output as markdown
Expand Down
68 changes: 68 additions & 0 deletions pkg/benchstatter/markdown_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package benchstatter

import (
"strings"
"testing"

"github.com/stretchr/testify/require"
Expand All @@ -15,3 +16,70 @@ foo: bar`
got := formatGroup(input)
require.Equal(t, want, got)
}

func Test_csv2markdown(t *testing.T) {
for _, td := range []struct {
name string
csv string
want []string
}{
{
name: "basic",
csv: `
foo,bar
baz,qux
`,
want: []string{`
| foo | bar |
|-----|-----|
| baz | qux |
`},
},
{
name: "double",
csv: `
foo,bar
baz,qux

a,b
c,d
`,
want: []string{
`
| foo | bar |
|-----|-----|
| baz | qux |
`,
`
| a | b |
|---|---|
| c | d |
`,
},
},
{
name: "escaped",
csv: `
foo,bar
"b,q",foo
`,
want: []string{`
| foo | bar |
|-----|-----|
| b,q | foo |
`},
},
} {
t.Run(td.name, func(t *testing.T) {
got, err := csv2Markdown([]byte(td.csv))
require.NoError(t, err)
for i, s := range td.want {
td.want[i] = strings.TrimSpace(s)
}
for i, s := range got {
got[i] = strings.TrimSpace(s)
}
require.Equal(t, td.want, got)
})
}
}
16 changes: 8 additions & 8 deletions pkg/benchstatter/testdata/_golden-example-norange.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
| name | old time/op (ns/op) | new time/op (ns/op) | delta | |
| ---------- | ------------------- | ------------------- | ------- | --------------- |
| GobEncode | 13599100 | 11789300 | -13.31% | (p=0.016 n=4+5) |
| JSONEncode | 32114300 | 31761400 | ~ | (p=0.286 n=4+5) |
| name | old time/op (ns/op) | new time/op (ns/op) | delta | |
|------------|---------------------|---------------------|---------|-----------------|
| GobEncode | 13599100 | 11789300 | -13.31% | (p=0.016 n=4+5) |
| JSONEncode | 32114300 | 31761400 | ~ | (p=0.286 n=4+5) |

| name | old speed (MB/s) | new speed (MB/s) | delta | |
| ---------- | ---------------- | ---------------- | ------- | --------------- |
| GobEncode | 56.44 | 65.108 | +15.36% | (p=0.016 n=4+5) |
| JSONEncode | 60.4275 | 61.102 | ~ | (p=0.286 n=4+5) |
| name | old speed (MB/s) | new speed (MB/s) | delta | |
|------------|------------------|------------------|---------|-----------------|
| GobEncode | 56.44 | 65.108 | +15.36% | (p=0.016 n=4+5) |
| JSONEncode | 60.4275 | 61.102 | ~ | (p=0.286 n=4+5) |
16 changes: 8 additions & 8 deletions pkg/benchstatter/testdata/_golden-example.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
| name | old time/op (ns/op) | ± | new time/op (ns/op) | ± | delta | ± |
| ---------- | ------------------- | --- | ------------------- | --- | ------- | --------------- |
| GobEncode | 13599100 | 1% | 11789300 | 1% | -13.31% | (p=0.016 n=4+5) |
| JSONEncode | 32114300 | 1% | 31761400 | 1% | ~ | (p=0.286 n=4+5) |
| name | old time/op (ns/op) | ± | new time/op (ns/op) | ± | delta | ± |
|------------|---------------------|----|---------------------|----|---------|-----------------|
| GobEncode | 13599100 | 1% | 11789300 | 1% | -13.31% | (p=0.016 n=4+5) |
| JSONEncode | 32114300 | 1% | 31761400 | 1% | ~ | (p=0.286 n=4+5) |

| name | old speed (MB/s) | ± | new speed (MB/s) | ± | delta | ± |
| ---------- | ---------------- | --- | ---------------- | --- | ------- | --------------- |
| GobEncode | 56.44 | 1% | 65.108 | 1% | +15.36% | (p=0.016 n=4+5) |
| JSONEncode | 60.4275 | 1% | 61.102 | 2% | ~ | (p=0.286 n=4+5) |
| name | old speed (MB/s) | ± | new speed (MB/s) | ± | delta | ± |
|------------|------------------|----|------------------|----|---------|-----------------|
| GobEncode | 56.44 | 1% | 65.108 | 1% | +15.36% | (p=0.016 n=4+5) |
| JSONEncode | 60.4275 | 1% | 61.102 | 2% | ~ | (p=0.286 n=4+5) |
Loading
0