Skip to content

Commit

Permalink
perf: improve performance
Browse files Browse the repository at this point in the history
  • Loading branch information
nekrassov01 committed Jul 14, 2024
1 parent 985308b commit a1e2d0a
Show file tree
Hide file tree
Showing 10 changed files with 525 additions and 357 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,9 @@ jobs:
- name: Run tests
run: |
git diff --cached --exit-code
go test ./... -v -cover -coverprofile=cover.out
go test ./... -v -cover -coverprofile=cover.Render
- name: Upload coverage to Codecov
uses: codecov/codecov-action@v3
with:
files: ./cover.out
files: ./cover.Render
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
*.test

# Output of the go coverage tool, specifically when used with LiteIDE
*.out
*.Render

# Dependency directories (remove the comment below to include it)
# vendor/
Expand Down
6 changes: 3 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,11 @@ endif

.PHONY: test
test:
go test ./... -v -cover -coverprofile=cover.out
go test ./... -v -cover -coverprofile=cover.Render

.PHONY: cover
cover:
go tool cover -html=cover.out -o cover.html
go tool cover -html=cover.Render -o cover.html

.PHONY: bench
bench:
Expand Down Expand Up @@ -78,4 +78,4 @@ publish: deps-gobump check-git
.PHONY: clean
clean:
go clean
rm -f cover.out cover.html cpu.prof mem.prof $(BIN).test
rm -f cover.Render cover.html cpu.prof mem.prof $(BIN).test
35 changes: 34 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ Support
- Group rows based on previous field value
- Ignore specified columns
- Escape HTML special characters
- Set multiple values to a field as a joined string
- Set byte slices as a string

Notes
-----
Expand All @@ -55,7 +57,38 @@ Notes
Usage
-----

[example](example_test.go)
[Example](example_test.go)

Benchmark
---------

[A quick benchmark](benchmark.go)

This is only for reference as the functions are different, but for simple drawing, it has better performance than TableWriter.

```text
go test -run=^$ -bench=. -benchmem -count 5
goos: darwin
goarch: arm64
pkg: github.com/nekrassov01/mintab
BenchmarkMintab-8 45578 25500 ns/op 20527 B/op 399 allocs/op
BenchmarkMintab-8 46488 25449 ns/op 20527 B/op 399 allocs/op
BenchmarkMintab-8 44702 26457 ns/op 20528 B/op 399 allocs/op
BenchmarkMintab-8 42699 28344 ns/op 20527 B/op 399 allocs/op
BenchmarkMintab-8 45213 31852 ns/op 20527 B/op 399 allocs/op
BenchmarkMintabSimple-8 55597 19234 ns/op 13033 B/op 242 allocs/op
BenchmarkMintabSimple-8 64444 18966 ns/op 13033 B/op 242 allocs/op
BenchmarkMintabSimple-8 53935 21939 ns/op 13034 B/op 242 allocs/op
BenchmarkMintabSimple-8 61573 18596 ns/op 13033 B/op 242 allocs/op
BenchmarkMintabSimple-8 64854 19147 ns/op 13033 B/op 242 allocs/op
BenchmarkTableWriter-8 21787 47804 ns/op 25421 B/op 701 allocs/op
BenchmarkTableWriter-8 26362 45354 ns/op 25365 B/op 701 allocs/op
BenchmarkTableWriter-8 26691 44275 ns/op 25332 B/op 701 allocs/op
BenchmarkTableWriter-8 26622 44199 ns/op 25360 B/op 701 allocs/op
BenchmarkTableWriter-8 27138 44492 ns/op 25297 B/op 701 allocs/op
PASS
ok github.com/nekrassov01/mintab 24.097s
```

Author
------
Expand Down
10 changes: 3 additions & 7 deletions benchmark_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ func BenchmarkMintab(b *testing.B) {
if err := table.Load(basicsample); err != nil {
b.Fatal(err)
}
table.Out()
table.Render()
}
}

Expand All @@ -37,12 +37,11 @@ func BenchmarkMintabSimple(b *testing.B) {
if err := table.Load(data); err != nil {
b.Fatal(err)
}
table.Out()
table.Render()
}
}

func BenchmarkTableWriter(b *testing.B) {
header := []string{"InstanceID", "InstanceName", "InstanceState"}
data := [][]string{
{"i-1", "server-1", "running"},
{"i-2", "server-2", "stopped"},
Expand All @@ -54,10 +53,7 @@ func BenchmarkTableWriter(b *testing.B) {
b.ResetTimer()
for i := 0; i < b.N; i++ {
table := tablewriter.NewWriter(&bytes.Buffer{})
table.SetHeader(header)
for _, v := range data {
table.Append(v)
}
table.AppendBulk(data)
table.Render()
}
}
42 changes: 21 additions & 21 deletions example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ func ExampleTable_Load_basic() {
if err := table.Load(s1); err != nil {
log.Fatal(err)
}
table.Out()
table.Render()

// Output:
// +------------+--------------+------------+------------+
Expand All @@ -111,11 +111,11 @@ func ExampleTable_Load_basic() {
}

func ExampleTable_Load_markdown() {
table := mintab.New(os.Stdout, mintab.WithFormat(mintab.FormatMarkdown))
table := mintab.New(os.Stdout, mintab.WithFormat(mintab.MarkdownFormat))
if err := table.Load(s1); err != nil {
log.Fatal(err)
}
table.Out()
table.Render()

// Output:
// | InstanceID | InstanceName | AttachedLB | AttachedTG |
Expand All @@ -129,11 +129,11 @@ func ExampleTable_Load_markdown() {
}

func ExampleTable_Load_backlog() {
table := mintab.New(os.Stdout, mintab.WithFormat(mintab.FormatBacklog))
table := mintab.New(os.Stdout, mintab.WithFormat(mintab.BacklogFormat))
if err := table.Load(s1); err != nil {
log.Fatal(err)
}
table.Out()
table.Render()

// Output:
// | InstanceID | InstanceName | AttachedLB | AttachedTG |h
Expand All @@ -150,7 +150,7 @@ func ExampleTable_Load_disableheader() {
if err := table.Load(s1); err != nil {
log.Fatal(err)
}
table.Out()
table.Render()

// Output:
// +------------+--------------+------------+------------+
Expand Down Expand Up @@ -178,7 +178,7 @@ func ExampleTable_Load_emptyfieldplaceholder() {
if err := table.Load(s1); err != nil {
log.Fatal(err)
}
table.Out()
table.Render()

// Output:
// +------------+--------------+------------+------------+
Expand Down Expand Up @@ -208,7 +208,7 @@ func ExampleTable_Load_worddelimiter() {
if err := table.Load(s1); err != nil {
log.Fatal(err)
}
table.Out()
table.Render()

// Output:
// +------------+--------------+------------+---------------------+
Expand All @@ -233,7 +233,7 @@ func ExampleTable_Load_margin() {
if err := table.Load(s1); err != nil {
log.Fatal(err)
}
table.Out()
table.Render()

// Output:
// +----------------+------------------+----------------+----------------+
Expand Down Expand Up @@ -263,7 +263,7 @@ func ExampleTable_Load_mergefields1() {
if err := table.Load(s2); err != nil {
log.Fatal(err)
}
table.Out()
table.Render()

// Output:
// +------------+--------------+-------+-----------------+---------------+------------+----------+--------+---------------+---------------+
Expand Down Expand Up @@ -292,7 +292,7 @@ func ExampleTable_Load_mergefields2() {
if err := table.Load(s2); err != nil {
log.Fatal(err)
}
table.Out()
table.Render()

// Output:
// +------------+--------------+-------+-----------------+---------------+------------+----------+--------+---------------+---------------+
Expand All @@ -317,11 +317,11 @@ func ExampleTable_Load_mergefields2() {
}

func ExampleTable_Load_mergefields3() {
table := mintab.New(os.Stdout, mintab.WithFormat(mintab.FormatCompressedText), mintab.WithMergeFields([]int{0, 1, 2, 3}))
table := mintab.New(os.Stdout, mintab.WithFormat(mintab.CompressedTextFormat), mintab.WithMergeFields([]int{0, 1, 2, 3}))
if err := table.Load(s2); err != nil {
log.Fatal(err)
}
table.Out()
table.Render()

// Output:
// +------------+--------------+-------+-----------------+---------------+------------+----------+--------+---------------+---------------+
Expand All @@ -344,7 +344,7 @@ func ExampleTable_Load_ignorefields1() {
if err := table.Load(s3); err != nil {
log.Fatal(err)
}
table.Out()
table.Render()

// Output:
// +---------------+---------------+
Expand All @@ -361,7 +361,7 @@ func ExampleTable_Load_ignorefields2() {
if err := table.Load(s3); err != nil {
log.Fatal(err)
}
table.Out()
table.Render()

// Output:
// +---------------+
Expand All @@ -378,7 +378,7 @@ func ExampleTable_Load_escape1() {
if err := table.Load(s4); err != nil {
log.Fatal(err)
}
table.Out()
table.Render()

// Output:
// +-------------------------+-----------------------------------------+
Expand All @@ -401,11 +401,11 @@ func ExampleTable_Load_escape1() {
}

func ExampleTable_Load_escape2() {
table := mintab.New(os.Stdout, mintab.WithFormat(mintab.FormatMarkdown), mintab.WithEscape(false))
table := mintab.New(os.Stdout, mintab.WithFormat(mintab.MarkdownFormat), mintab.WithEscape(false))
if err := table.Load(s4); err != nil {
log.Fatal(err)
}
table.Out()
table.Render()

// Output:
// | Name | EscatableValue |
Expand All @@ -417,11 +417,11 @@ func ExampleTable_Load_escape2() {
}

func ExampleTable_Load_escape3() {
table := mintab.New(os.Stdout, mintab.WithFormat(mintab.FormatMarkdown), mintab.WithEscape(true))
table := mintab.New(os.Stdout, mintab.WithFormat(mintab.MarkdownFormat), mintab.WithEscape(true))
if err := table.Load(s4); err != nil {
log.Fatal(err)
}
table.Out()
table.Render()

// Output:
// | Name | EscatableValue |
Expand All @@ -438,7 +438,7 @@ func ExampleTable_Load_string() {
if err := table.Load(s2); err != nil {
log.Fatal(err)
}
table.Out()
table.Render()
fmt.Println(builder.String())

// Output:
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module github.com/nekrassov01/mintab
go 1.21

require (
github.com/google/go-cmp v0.6.0
github.com/mattn/go-runewidth v0.0.15
github.com/olekukonko/tablewriter v0.0.5
)
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/mattn/go-runewidth v0.0.9/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m2gUSrubnMI=
github.com/mattn/go-runewidth v0.0.15 h1:UNAjwbU9l54TA3KzvqLGxwWjHmMgBUVhBiTjelZgg3U=
github.com/mattn/go-runewidth v0.0.15/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
Expand Down
Loading

0 comments on commit a1e2d0a

Please sign in to comment.