Skip to content

Commit

Permalink
add perfgormance testing
Browse files Browse the repository at this point in the history
  • Loading branch information
Dima Koss committed Jan 27, 2024
1 parent 532c8e3 commit d67b56c
Show file tree
Hide file tree
Showing 3 changed files with 219 additions and 0 deletions.
2 changes: 2 additions & 0 deletions perf/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
all:
go test -bench=.
3 changes: 3 additions & 0 deletions perf/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Performance testing

All performance tests will be accumulated under the current directory
214 changes: 214 additions & 0 deletions perf/perf_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,214 @@
package perf_test

import (
"runtime"
"testing"

"github.com/koss-null/funcfrog/pkg/pipe"
)

func fib(n int) int {
n = n % 91
a, b := 0, 1
for i := 0; i < n; i++ {
a, b = b, a+b
}
return a
}

// Define the predicate function for the filter operation
func filterFunc(x *int) bool {
// Perform a complex condition check
return fib(*x%2) == 0
}

// Define the binary function for the reduce operation
func reduceFunc(x, y *int) int {
// Perform a complex calculation or aggregation
return fib(*x)%1024 + fib(*y)%1024
}

func BenchmarkMap(b *testing.B) {
b.StopTimer()
input := make([]int, 1_000_000)
for i := 0; i < len(input); i++ {
input[i] = i
}
b.StartTimer()

for j := 0; j < b.N; j++ {

// Create a Pipe from the input slice
pipe := pipe.Slice(input)

// Apply the map operation to the Pipe
result := pipe.Map(fib).Do()

// Perform any necessary assertions on the result
_ = result
}
}

func BenchmarkMapParallel(b *testing.B) {
b.StopTimer()
input := make([]int, 1_000_000)
for i := 0; i < len(input); i++ {
input[i] = i
}
b.StartTimer()

for j := 0; j < b.N; j++ {

// Create a Pipe from the input slice
pipe := pipe.Slice(input).Parallel(uint16(runtime.NumCPU()))

// Apply the map operation to the Pipe
result := pipe.Map(fib).Do()

// Perform any necessary assertions on the result
_ = result
}
}

func BenchmarkMapFor(b *testing.B) {
b.StopTimer()
input := make([]int, 1_000_000)
for i := 0; i < len(input); i++ {
input[i] = i
}
b.StartTimer()

for j := 0; j < b.N; j++ {

result := make([]int, 0, len(input))
for i := range input {
result = append(result, fib(input[i]))
}

// Perform any necessary assertions on the result
_ = result
}
}

func BenchmarkFilter(b *testing.B) {
b.StopTimer()
input := make([]int, 1_000_000)
for i := 0; i < len(input); i++ {
input[i] = i
}
b.StartTimer()

for j := 0; j < b.N; j++ {
// Create a Pipe from the input slice
pipe := pipe.Slice(input)

// Apply the filter operation to the Pipe
result := pipe.Filter(filterFunc).Do()

// Perform any necessary assertions on the result
_ = result
}
}

func BenchmarkFilterParallel(b *testing.B) {
b.StopTimer()
input := make([]int, 1_000_000)
for i := 0; i < len(input); i++ {
input[i] = i
}
b.StartTimer()

for j := 0; j < b.N; j++ {
// Create a Pipe from the input slice
pipe := pipe.Slice(input).Parallel(uint16(runtime.NumCPU()))

// Apply the filter operation to the Pipe
result := pipe.Filter(filterFunc).Do()

// Perform any necessary assertions on the result
_ = result
}
}

func BenchmarkFilterFor(b *testing.B) {
b.StopTimer()
input := make([]int, 1_000_000)
for i := 0; i < len(input); i++ {
input[i] = i
}
b.StartTimer()

for j := 0; j < b.N; j++ {

// Apply the filter operation to the Pipe
result := make([]int, 0)
for i := range input {
if filterFunc(&input[i]) {
result = append(result, input[i])
}
}

// Perform any necessary assertions on the result
_ = result
}
}

func BenchmarkReduce(b *testing.B) {
b.StopTimer()
input := make([]int, 1_000_000)
for i := 0; i < len(input); i++ {
input[i] = i
}
b.StartTimer()

for j := 0; j < b.N; j++ {
pipe := pipe.Slice(input)

// Apply the reduce operation to the Pipe
result := pipe.Reduce(reduceFunc)

// Perform any necessary assertions on the result
_ = result
}
}

func BenchmarkSumParallel(b *testing.B) {
b.StopTimer()
input := make([]int, 1_000_000)
for i := 0; i < len(input); i++ {
input[i] = i
}
b.StartTimer()

for j := 0; j < b.N; j++ {
// Create a Pipe from the input slice
pipe := pipe.Slice(input).Parallel(uint16(runtime.NumCPU()))

// Apply the reduce operation to the Pipe
result := pipe.Sum(reduceFunc)

// Perform any necessary assertions on the result
_ = result
}
}

// Benchmark the reduce with for-loop
func BenchmarkReduceFor(b *testing.B) {
b.StopTimer()
input := make([]int, 1_000_000)
for i := 0; i < len(input); i++ {
input[i] = i
}
b.StartTimer()

for j := 0; j < b.N; j++ {
// Apply the reduce operation to the Pipe
result := 0
for i := range input {
result = reduceFunc(&result, &input[i])
}

// Perform any necessary assertions on the result
_ = result
}
}

0 comments on commit d67b56c

Please sign in to comment.