nettools/data_structures/worker_pool_test.go

55 lines
1.1 KiB
Go

package dbUtils_test
import (
"context"
"math/rand"
"testing"
common_utils "git.mic.pp.ua/anderson/nettools/common"
ds_utils "git.mic.pp.ua/anderson/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
}