-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Dima Koss
committed
Jan 27, 2024
1 parent
532c8e3
commit d67b56c
Showing
3 changed files
with
219 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
all: | ||
go test -bench=. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |