Skip to content

Commit

Permalink
test: add more benchmark
Browse files Browse the repository at this point in the history
  • Loading branch information
siyul-park committed Nov 29, 2023
1 parent 3e667f7 commit ff3b3da
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 0 deletions.
29 changes: 29 additions & 0 deletions pkg/primitive/map_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,3 +96,32 @@ func TestMap_EncodeAndDecode(t *testing.T) {
assert.Equal(t, v1.String(), decoded.K1)
})
}

func BenchmarkMap_Set(b *testing.B) {
m := NewMap()

for i := 0; i < b.N; i++ {
m = m.Set(NewString(faker.Word()), NewString(faker.Word()))
}
}

func BenchmarkMap_Get(b *testing.B) {
m := NewMap()
for i := 0; i < 1000; i++ {
m = m.Set(NewString(faker.Word()), NewString(faker.Word()))
}

b.ResetTimer()
for i := 0; i < b.N; i++ {
_, _ = m.Get(NewString(faker.Word()))
}
}

func BenchmarkMap_Interface(b *testing.B) {
v := NewMap(NewString("key"), NewString("value"))

b.ResetTimer()
for i := 0; i < b.N; i++ {
_ = v.Interface()
}
}
43 changes: 43 additions & 0 deletions pkg/primitive/slice_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package primitive

import (
"fmt"
"testing"

"github.com/go-faker/faker/v4"
Expand Down Expand Up @@ -102,3 +103,45 @@ func TestSlice_EncodeAndDecode(t *testing.T) {
assert.Equal(t, []any{v1.Interface(), v2.Interface()}, decoded)
})
}

func BenchmarkSlice_Append(b *testing.B) {
s := NewSlice()

for i := 0; i < b.N; i++ {
s = s.Append(NewString(faker.Word()))
}
}

func BenchmarkSlice_Sub(b *testing.B) {
s := NewSlice()
for i := 0; i < 1000; i++ {
s = s.Append(NewString(faker.Word()))
}

b.ResetTimer()
for i := 0; i < b.N; i++ {
_ = s.Sub(0, 500)
}
}
func BenchmarkSlice_Get(b *testing.B) {
size := 100000
s := NewSlice()
for i := 0; i < size; i++ {
s = s.Set(i, NewString(fmt.Sprintf("value%d", i)))
}

b.ResetTimer()
for i := 0; i < b.N; i++ {
index := i % size
_ = s.Get(index)
}
}

func BenchmarkSlice_Interface(b *testing.B) {
v := NewSlice(NewString("value"))

b.ResetTimer()
for i := 0; i < b.N; i++ {
_ = v.Interface()
}
}

0 comments on commit ff3b3da

Please sign in to comment.