Skip to content

Commit

Permalink
chore: Added more Benchmark tests
Browse files Browse the repository at this point in the history
Signed-off-by: Manfred Touron <94029+moul@users.noreply.github.com>

chore: Added Slice Benchmark tests

Signed-off-by: Manfred Touron <94029+moul@users.noreply.github.com>

Added Slice benchmark tests

update data for slice benchmarks

Added Slice benchmark tests

Fix feedback, Added big slices for slice 2 example

added time benchmark test

added benchmark for command exist func

added exec benchmark tests

added benchmark test for random letters

added pattern benchmark tests

reslove feedback

resolve feedbacks

fix exec feedback

fix random letters fix

Fix pattern tests

resolve fback
  • Loading branch information
moul authored and Darko Djalevski committed May 26, 2021
1 parent b44b8fe commit c1426de
Show file tree
Hide file tree
Showing 7 changed files with 282 additions and 17 deletions.
65 changes: 49 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -200,22 +200,55 @@ $ go get moul.io/u

[embedmd]:# (.tmp/bench.txt txt)
```txt
benchmark iter time/iter
--------- ---- ---------
BenchmarkUnzip-12 4725 273823.00 ns/op
BenchmarkUnzipBytes-12 4614 270191.00 ns/op
BenchmarkB64Encode/1-12 14198170 79.61 ns/op
BenchmarkB64Encode/1-parallel-12 100040912 10.75 ns/op
BenchmarkB64Encode/1000-12 435790 5167.00 ns/op
BenchmarkB64Encode/1000-parallel-12 1369093 885.30 ns/op
BenchmarkB64Encode/1000000-12 278 4470872.00 ns/op
BenchmarkB64Encode/1000000-parallel-12 2394 490425.00 ns/op
BenchmarkB64Decode/1000-12 458520 4065.00 ns/op
BenchmarkB64Decode/1000-parallel-12 1927333 641.00 ns/op
BenchmarkB64Decode/10000-12 32613 38194.00 ns/op
BenchmarkB64Decode/10000-parallel-12 193815 5905.00 ns/op
BenchmarkB64Decode/100000-12 5877 364683.00 ns/op
BenchmarkB64Decode/100000-parallel-12 25208 65093.00 ns/op
benchmark iter time/iter
--------- ---- ---------
BenchmarkUnzip-8 4392 234151.00 ns/op
BenchmarkUnzipBytes-8 7386 156000.00 ns/op
BenchmarkB64Encode/1-8 56815041 21.02 ns/op
BenchmarkB64Encode/1-parallel-8 198828087 6.30 ns/op
BenchmarkB64Encode/1000-8 1469884 837.40 ns/op
BenchmarkB64Encode/1000-parallel-8 2455197 422.40 ns/op
BenchmarkB64Encode/1000000-8 1740 670752.00 ns/op
BenchmarkB64Encode/1000000-parallel-8 5971 216736.00 ns/op
BenchmarkB64Decode/1000-8 2133470 564.80 ns/op
BenchmarkB64Decode/1000-parallel-8 3857101 305.30 ns/op
BenchmarkB64Decode/10000-8 241730 4902.00 ns/op
BenchmarkB64Decode/10000-parallel-8 501278 2557.00 ns/op
BenchmarkB64Decode/100000-8 25467 46986.00 ns/op
BenchmarkB64Decode/100000-parallel-8 60162 21874.00 ns/op
BenchmarkCommandExists/go-8 422287 2896.00 ns/op
BenchmarkCommandExists/go-parallel-8 307030 3639.00 ns/op
BenchmarkCommandExists/gofmt-8 425854 2762.00 ns/op
BenchmarkCommandExists/gofmt-parallel-8 314979 3795.00 ns/op
BenchmarkCommandExists/foo-8 69224 17332.00 ns/op
BenchmarkCommandExists/foo-parallel-8 86791 14672.00 ns/op
BenchmarkCommandExists/bar-8 66331 17619.00 ns/op
BenchmarkCommandExists/bar-parallel-8 81376 14422.00 ns/op
BenchmarkSafeExec-8 1344 913235.00 ns/op
BenchmarkCombineFuncs-8 31152040 38.07 ns/op
BenchmarkFuture-8 7089960 172.70 ns/op
BenchmarkRandomLetters/1000-8 832617 1435.00 ns/op
BenchmarkRandomLetters/1000-parallel-8 88868 13516.00 ns/op
BenchmarkRandomLetters/10000-8 84782 14192.00 ns/op
BenchmarkRandomLetters/10000-parallel-8 10000 134053.00 ns/op
BenchmarkRandomLetters/100000-8 8295 140675.00 ns/op
BenchmarkRandomLetters/100000-parallel-8 914 1346413.00 ns/op
BenchmarkUniqueStrings/slice1-8 3959287 301.50 ns/op
BenchmarkUniqueStrings/slice1-parallel-8 11342516 105.80 ns/op
BenchmarkUniqueStrings/slice2-8 7676 152554.00 ns/op
BenchmarkUniqueStrings/slice2-parallel-8 20694 58215.00 ns/op
BenchmarkUniqueInts/slice1-8 5480984 217.70 ns/op
BenchmarkUniqueInts/slice1-parallel-8 18684979 64.46 ns/op
BenchmarkUniqueInts/slice2-8 31842 37353.00 ns/op
BenchmarkUniqueInts/slice2-parallel-8 110750 10925.00 ns/op
BenchmarkUniqueInterfaces/slice1-8 2369629 503.10 ns/op
BenchmarkUniqueInterfaces/slice1-parallel-8 8526723 140.30 ns/op
BenchmarkUniqueInterfaces/slice2-8 7380 161550.00 ns/op
BenchmarkUniqueInterfaces/slice2-parallel-8 19438 62007.00 ns/op
BenchmarkShortDuration/Simple-8 39205808 29.88 ns/op
BenchmarkShortDuration/Simple-parallel-8 199198874 6.21 ns/op
BenchmarkShortDuration/Complex-8 8160741 145.40 ns/op
BenchmarkShortDuration/Complex-parallel-8 29133106 42.44 ns/op
```

## Contribute
Expand Down
2 changes: 1 addition & 1 deletion encoding_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,9 @@ func BenchmarkB64Decode(b *testing.B) {
{"10000", strings.Repeat("a", 10000)},
{"100000", strings.Repeat("a", 100000)},
}
b.ResetTimer()
for _, bc := range cases {
b.Run(bc.Name, func(b *testing.B) {
b.ResetTimer()
for i := 0; i < b.N; i++ {
_, err := u.B64Decode(bc.Data)
if err != nil {
Expand Down
32 changes: 32 additions & 0 deletions exec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package u_test
import (
"fmt"
"os/exec"
"testing"

"moul.io/u"
)
Expand Down Expand Up @@ -35,3 +36,34 @@ func ExampleCommandExists() {
// true
// false
}

func BenchmarkCommandExists(b *testing.B) {
cases := []struct {
Command string
}{
{"go"},
{"asddsa"},
}
for _, bc := range cases {
b.Run(bc.Command, func(b *testing.B) {
b.ResetTimer()
for i := 0; i < b.N; i++ {
u.CommandExists(bc.Command)
}
})
b.Run(bc.Command+"-parallel", func(b *testing.B) {
b.ResetTimer()
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
u.CommandExists(bc.Command)
}
})
})
}
}

func BenchmarkSafeExec(b *testing.B) {
for i := 0; i < b.N; i++ {
u.SafeExec(exec.Command("true"))
}
}
27 changes: 27 additions & 0 deletions pattern_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package u_test
import (
"fmt"
"net/http"
"testing"
"time"

"moul.io/u"
Expand Down Expand Up @@ -37,3 +38,29 @@ func ExampleFuture() {
// Ret: foobar
// Err: <nil>
}

func BenchmarkCombineFuncs(b *testing.B) {
f1 := func() {}
f2 := func() {
fmt.Println("A")
}
f3 := func() {
fmt.Println("B")
fmt.Println("C")
fmt.Println("D")
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
u.CombineFuncs(f1, f2, f3)
}
}

func BenchmarkFuture(b *testing.B) {
f1 := func() (interface{}, error) {
return nil, nil
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
u.Future(f1)
}
}
28 changes: 28 additions & 0 deletions rand_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package u_test
import (
"fmt"
"math/rand"
"testing"

"moul.io/u"
)
Expand All @@ -19,3 +20,30 @@ func ExampleRandomLetters() {
// unhuksqV
// GzAdxlgghEjkMVeZJpmKqakmTRgKfBSWYjUNGkdmdt
}

func BenchmarkRandomLetters(b *testing.B) {
cases := []struct {
Name string
NumberOfLetters int
}{
{"1000", 100},
{"10000", 1000},
{"100000", 10000},
}
for _, bc := range cases {
b.Run(bc.Name, func(b *testing.B) {
b.ResetTimer()
for i := 0; i < b.N; i++ {
u.RandomLetters(bc.NumberOfLetters)
}
})
b.Run(bc.Name+"-parallel", func(b *testing.B) {
b.ResetTimer()
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
u.RandomLetters(bc.NumberOfLetters)
}
})
})
}
}
118 changes: 118 additions & 0 deletions slice_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ package u_test

import (
"fmt"
"math/rand"
"strconv"
"testing"

"moul.io/u"
)
Expand All @@ -20,3 +23,118 @@ func ExampleUniqueInterfaces() {
fmt.Println(u.UniqueInterfaces([]interface{}{13, "foo", "bar", 42, 13, 43, "baz"}))
// Output: [13 foo bar 42 43 baz]
}

func BenchmarkUniqueStrings(b *testing.B) {
cases := []struct {
Name string
Data []string
}{
{"slice1", []string{"foofoo", "barbar", "foo", "bazbaz", "foo", "bar", "foobar", "baz", "foobaz"}},
{"slice2", bigSliceOfString()},
}

for _, bc := range cases {
b.Run(bc.Name, func(b *testing.B) {
b.ResetTimer()
for i := 0; i < b.N; i++ {
u.UniqueStrings(bc.Data)
}
})
b.Run(bc.Name+"-parallel", func(b *testing.B) {
b.ResetTimer()
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
u.UniqueStrings(bc.Data)
}
})
})
}
}

func BenchmarkUniqueInts(b *testing.B) {
cases := []struct {
Name string
Data []int
}{
{"slice1", []int{4, 5, 2, 6, 7, 7, 2, 4, 2, 1, 1, 2, 3, 4}},
{"slice2", bigSliceOfInt()},
}

for _, bc := range cases {
b.Run(bc.Name, func(b *testing.B) {
b.ResetTimer()
for i := 0; i < b.N; i++ {
u.UniqueInts(bc.Data)
}
})
b.Run(bc.Name+"-parallel", func(b *testing.B) {
b.ResetTimer()
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
u.UniqueInts(bc.Data)
}
})
})
}
}

func BenchmarkUniqueInterfaces(b *testing.B) {
cases := []struct {
Name string
Data []interface{}
}{
{"slice1", []interface{}{1, 2, 2, 1, 3, 4, "foo", "bar", "foo", "foo", "barfoo"}},
{"slice2", bigSliceOfInterface()},
}

for _, bc := range cases {
b.Run(bc.Name, func(b *testing.B) {
b.ResetTimer()
for i := 0; i < b.N; i++ {
u.UniqueInterfaces(bc.Data)
}
})
b.Run(bc.Name+"-parallel", func(b *testing.B) {
b.ResetTimer()
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
u.UniqueInterfaces(bc.Data)
}
})
})
}
}

func bigSliceOfInt() []int {
var ns []int
rand.Seed(42)
for i := 0; i < 1000; i++ {
nb := rand.Intn(500)
ns = append(ns, nb)
}
return ns
}

func bigSliceOfString() []string {
var ss []string
rand.Seed(42)
for i := 0; i < 1000; i++ {
rn := rand.Intn(500)
ss = append(ss, strconv.Itoa(rn))
}
return ss
}

func bigSliceOfInterface() []interface{} {
var is []interface{}
rand.Seed(42)
for i := 0; i < 1000; i++ {
nb := rand.Intn(500)
is = append(is, nb)
}
for i := 0; i < 1000; i++ {
rn := rand.Intn(500)
is = append(is, strconv.Itoa(rn))
}
return is
}
27 changes: 27 additions & 0 deletions time_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package u_test

import (
"fmt"
"testing"
"time"

"moul.io/u"
Expand Down Expand Up @@ -68,3 +69,29 @@ func ExampleShortDuration() {
// 1d
// 1h0m1s
}

func BenchmarkShortDuration(b *testing.B) {
cases := []struct {
Name string
Data time.Duration
}{
{"Simple", time.Second * 5},
{"Complex", time.Nanosecond * 123456789101112},
}
for _, bc := range cases {
b.Run(bc.Name, func(b *testing.B) {
b.ResetTimer()
for i := 0; i < b.N; i++ {
u.ShortDuration(bc.Data)
}
})
b.Run(bc.Name+"-parallel", func(b *testing.B) {
b.ResetTimer()
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
u.ShortDuration(bc.Data)
}
})
})
}
}

0 comments on commit c1426de

Please sign in to comment.