nettools/data_structures/worker_pool_test.go
2024-11-12 09:10:09 +01:00

55 lines
1.1 KiB
Go

package ds_utils_test
import (
"context"
"math/rand"
"testing"
common_utils "nettools/common"
ds_utils "nettools/data_structures"
)
func BenchmarkWorkerPool(b *testing.B) {
wp := ds_utils.NewWorkerPool(8, 0)
for i := 0; i < b.N; i++ {
wp.Exec(func(ctx context.Context) error {
arr := make([]byte, 128)
rand.Read(arr)
return nil
})
}
// Prevent exit before tasks are finished
endBenchmark := make(chan struct{})
wp.Exec(func(ctx context.Context) error {
close(endBenchmark)
return nil
})
<-endBenchmark
}
func BenchmarkWorkerPoolFunc(b *testing.B) {
taskChan := make(chan common_utils.Task)
go func() {
_ = ds_utils.RunWorkerPool(context.Background(), taskChan, 8, func(err error) error {
b.Error(err.Error())
return nil
})
}()
for i := 0; i < b.N; i++ {
taskChan <- func(ctx context.Context) error {
arr := make([]byte, 128)
rand.Read(arr)
return nil
}
}
// Prevent exit before tasks are finished
endBenchmark := make(chan struct{})
taskChan <- func(ctx context.Context) error {
close(endBenchmark)
return nil
}
<-endBenchmark
}