From e6824488671e5d4d3f72dec901b228859baa3f5b Mon Sep 17 00:00:00 2001 From: Sebastian Scheibe Date: Tue, 26 Mar 2024 23:42:55 +0800 Subject: [PATCH 1/3] benchmarking --- .gitignore | 3 ++ Makefile | 5 +++ list_test.go | 97 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 105 insertions(+) create mode 100644 Makefile diff --git a/.gitignore b/.gitignore index 3ca94b3..5007b9b 100644 --- a/.gitignore +++ b/.gitignore @@ -28,3 +28,6 @@ go.work # This is the output of compiling `go build .` dist/ + + +.idea \ No newline at end of file diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..514fc43 --- /dev/null +++ b/Makefile @@ -0,0 +1,5 @@ +test: + go test -v ./... -count 1 -race + +benchmark: + go test -bench=. -benchmem \ No newline at end of file diff --git a/list_test.go b/list_test.go index 419638e..94c0ee5 100644 --- a/list_test.go +++ b/list_test.go @@ -16,6 +16,12 @@ func TestNewList(t *testing.T) { assert.NotNil(t, stringListPointer) } +func BenchmarkNewList(b *testing.B) { + for i := 0; i < b.N; i++ { + _ = collections.NewList[int](10) + } +} + func TestToList(t *testing.T) { inputs := []struct { array []int @@ -37,6 +43,13 @@ func TestToList(t *testing.T) { } } +func BenchmarkToList(b *testing.B) { + example := []int{1, 2, 3} + for i := 0; i < b.N; i++ { + _ = collections.ToList(example) + } +} + func TestRepeatingList(t *testing.T) { inputs := []struct { element int @@ -71,6 +84,12 @@ func TestRepeatingList(t *testing.T) { } } +func BenchmarkRepeatingList1_10(b *testing.B) { + for i := 0; i < b.N; i++ { + _ = collections.RepeatingList(1, 10) + } +} + func TestListAdd(t *testing.T) { l := collections.NewList[int](10) inputs := []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10} @@ -81,6 +100,13 @@ func TestListAdd(t *testing.T) { } } +func BenchmarkListAdd(b *testing.B) { + l := collections.NewList[int](10) + for i := 0; i < b.N; i++ { + l.Add(1) + } +} + func TestListContains(t *testing.T) { l := collections.ToList([]int{1, 2, 2, 4, 6, 7, 9, 10}) testCases := []struct { @@ -105,6 +131,20 @@ func TestListContains(t *testing.T) { } } +func BenchmarkListContainsTrue(b *testing.B) { + l := collections.ToList([]int{1, 2, 2, 4, 6, 7, 9, 10}) + for i := 0; i < b.N; i++ { + l.Contains(6) + } +} + +func BenchmarkListContainsFalse(b *testing.B) { + l := collections.ToList([]int{1, 2, 2, 4, 6, 7, 9, 10}) + for i := 0; i < b.N; i++ { + l.Contains(5) + } +} + func TestListCountOf(t *testing.T) { l := collections.ToList([]int{1, 2, 2, 1, 3, 5, 5, 3, 3, 1, 4, 2, 1, 3, 3}) testCases := []struct { @@ -124,6 +164,13 @@ func TestListCountOf(t *testing.T) { } } +func BenchmarkListCountOf(b *testing.B) { + l := collections.ToList([]int{1, 2, 2, 1, 3, 5, 5, 3, 3, 1, 4, 2, 1, 3, 3}) + for i := 0; i < b.N; i++ { + l.CountOf(3) + } +} + func TestListDistinct(t *testing.T) { testCases := []struct { input *collections.List[int] @@ -153,6 +200,13 @@ func TestListDistinct(t *testing.T) { } } +func BenchmarkListDistinct(b *testing.B) { + l := collections.ToList([]int{1, 2, 2, 1, 3, 5, 5, 3, 3, 1, 4, 2, 1, 3, 3}) + for i := 0; i < b.N; i++ { + l.Distinct() + } +} + func TestListExtend(t *testing.T) { testCases := []struct { list1 *collections.List[int] @@ -182,6 +236,14 @@ func TestListExtend(t *testing.T) { } } +func BenchmarkListExtend(b *testing.B) { + list1 := collections.ToList([]int{1, 1, 1, 1, 1}) + list2 := collections.ToList([]int{0, 0}) + for i := 0; i < b.N; i++ { + list1.Extend(list2) + } +} + func TestListGet(t *testing.T) { l := collections.ToList([]int{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10}) testCases := []struct { @@ -211,6 +273,20 @@ func TestListGet(t *testing.T) { } } +func BenchmarkListGetFound(b *testing.B) { + l := collections.ToList([]int{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10}) + for i := 0; i < b.N; i++ { + l.Get(5) + } +} + +func BenchmarkListGetNotFound(b *testing.B) { + l := collections.ToList([]int{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10}) + for i := 0; i < b.N; i++ { + l.Get(-1) + } +} + func TestIndexOf(t *testing.T) { l := collections.ToList([]int{1, 2, 3, 4, 5}) testCases := []struct { @@ -232,6 +308,13 @@ func TestIndexOf(t *testing.T) { } } +func BenchmarkListIndexOf(b *testing.B) { + l := collections.ToList([]int{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10}) + for i := 0; i < b.N; i++ { + l.IndexOf(5) + } +} + func TestListRemoveAll(t *testing.T) { l := collections.ToList([]int{1, 2, 2, 1, 3, 5, 5, 3, 3, 1, 4, 2, 1, 3, 3}) testCases := []struct { @@ -254,6 +337,13 @@ func TestListRemoveAll(t *testing.T) { } } +func BenchmarkListRemoveAll(b *testing.B) { + l := collections.ToList([]int{1, 2, 2, 1, 3, 5, 5, 3, 3, 1, 4, 2, 1, 3, 3}) + for i := 0; i < b.N; i++ { + l.RemoveAll(5) + } +} + func TestListRemoveDuplicates(t *testing.T) { testCases := []struct { input *collections.List[int] @@ -283,6 +373,13 @@ func TestListRemoveDuplicates(t *testing.T) { } } +func BenchmarkListRemoveDuplicates(b *testing.B) { + l := collections.ToList([]int{1, 2, 2, 1, 3, 5, 5, 3, 3, 1, 4, 2, 1, 3, 3}) + for i := 0; i < b.N; i++ { + l.RemoveDuplicates() + } +} + func TestListRemoveFirst(t *testing.T) { testCases := []struct { inputList *collections.List[int] From e7dfa20ffd6ea6a0e48602182fecb65fc781d7df Mon Sep 17 00:00:00 2001 From: Sebastian Scheibe Date: Wed, 27 Mar 2024 11:18:12 +0800 Subject: [PATCH 2/3] feat: add benchmark for list --- list_test.go | 44 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/list_test.go b/list_test.go index 94c0ee5..6770b10 100644 --- a/list_test.go +++ b/list_test.go @@ -414,6 +414,13 @@ func TestListRemoveFirst(t *testing.T) { } } +func BenchmarkListRemoveFirst(b *testing.B) { + l := collections.ToList([]int{1, 2, 2, 1, 3, 5, 5, 3, 3, 1, 4, 2, 1, 3, 3}) + for i := 0; i < b.N; i++ { + l.RemoveFirst(1) + } +} + func TestListWhere(t *testing.T) { testCases := []struct { inputList *collections.List[int] @@ -438,6 +445,14 @@ func TestListWhere(t *testing.T) { } } +func BenchmarkListWhere(b *testing.B) { + l := collections.ToList([]int{1, 2, 2, 1, 3, 5, 5, 3, 3, 1, 4, 2, 1, 3, 3}) + filterFunc := func(i int) bool { return i%2 == 0 } + for i := 0; i < b.N; i++ { + l.Where(filterFunc) + } +} + func TestListSize(t *testing.T) { testCases := []struct { inputList *collections.List[int] @@ -463,6 +478,13 @@ func TestListSize(t *testing.T) { } } +func BenchmarkListSize(b *testing.B) { + l := collections.ToList([]int{1, 2, 3, 4, 5}) + for i := 0; i < b.N; i++ { + l.Size() + } +} + func TestListString(t *testing.T) { testCases := []struct { inputList *collections.List[int] @@ -488,12 +510,26 @@ func TestListString(t *testing.T) { } } +func BenchmarkListString(b *testing.B) { + l := collections.ToList([]int{1, 2, 3, 4, 5}) + for i := 0; i < b.N; i++ { + l.String() + } +} + func TestListType(t *testing.T) { l := collections.NewList[int](0) assert.Equal(t, collections.TypeList, l.Type()) } +func BenchmarkListType(b *testing.B) { + l := collections.ToList([]int{1, 2, 3, 4, 5}) + for i := 0; i < b.N; i++ { + l.Type() + } +} + func TestListMap(t *testing.T) { studentMarks := []struct { name string @@ -521,6 +557,14 @@ func TestListMap(t *testing.T) { assert.Equal(t, expected, nl) } +func BenchmarkListMap(b *testing.B) { + l := collections.ToList([]int{1, 2, 3, 4, 5}) + funcMap := func(e int, i int) int { return e * e } + for i := 0; i < b.N; i++ { + collections.Map(l, funcMap) + } +} + func TestListReduce(t *testing.T) { testCases := []struct { inputList *collections.List[int] From dc58ba0b7c6a6e3ed53602ca104dc2ded13abce2 Mon Sep 17 00:00:00 2001 From: Sebastian Scheibe Date: Wed, 27 Mar 2024 11:28:47 +0800 Subject: [PATCH 3/3] feat: add benchmark for list --- list_test.go | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/list_test.go b/list_test.go index 6770b10..bdeb214 100644 --- a/list_test.go +++ b/list_test.go @@ -598,6 +598,14 @@ func TestListReduce(t *testing.T) { } } +func BenchmarkListReduce(b *testing.B) { + l := collections.ToList([]int{1, 2, 3, 4, 5}) + funcMap := func(e int, i int) int { return e * e } + for i := 0; i < b.N; i++ { + collections.Reduce(l, funcMap, 0) + } +} + func TestSortIntegers(t *testing.T) { testCases := []struct { inputList *collections.List[int] @@ -630,6 +638,21 @@ func TestSortIntegers(t *testing.T) { } } +func BenchmarkListSortIntegers(b *testing.B) { + l := collections.ToList([]int{1, 2, 3, 4, 5}) + funcSort := func(a, b int) int { + if a < b { + return -1 + } else if a > b { + return 1 + } + return 0 + } + for i := 0; i < b.N; i++ { + l.Sort(funcSort) + } +} + func TestSortStrings(t *testing.T) { testCases := []struct { inputList *collections.List[string] @@ -658,3 +681,18 @@ func TestSortStrings(t *testing.T) { } } + +func BenchmarkListSortStrings(b *testing.B) { + l := collections.ToList([]string{"e", "d", "c", "b", "a"}) + funcSort := func(a, b string) int { + if a < b { + return -1 + } else if a > b { + return 1 + } + return 0 + } + for i := 0; i < b.N; i++ { + l.Sort(funcSort) + } +}