I am trying to make function with worker pool and without, after that I create Benchmark test to compare with which want faster, but I got result that function with worker pool take longer than without.
here is the result
goos: linux
goarch: amd64
BenchmarkWithoutWorker-4 4561 228291 ns/op 13953 B/op 1744 allocs/op
BenchmarkWithWorker-4 1561 651845 ns/op 54429 B/op 2746 allocs/op
the worker pool looks simple and I am following the example from this stackoverflow question here is the scenario of my worker pool and without
var wg sync.WaitGroup
// i will get data from the DB, let say the data lenght about 1000
const dataFromDB int = 1000
// numOfProduce in benchmarking value is dataFromDB i defined
func WithoutWorker(numOfProduce int) {
for i := 0; i < numOfProduce; i++ {
if doSomething(fmt.Sprintf("data %d", i)) != nil {
fmt.Println("error")
}
}
}
func WithWorker(numWorker int) {
jobs := make(chan *Job, dataFromDB)
result := make(chan *Result, 10)
for i := 0; i < numWorker; i++ {
wg.Add(1)
go consume(i, jobs, result)
}
go produce(jobs)
wg.Wait()
// i might analyze the result channel
// here later to return any error to client if any error i got
}
func doSomething(str string) error {
if str == "" {
return errors.New("empty")
}
return nil
}
func consume(workerID int, jobs <-chan *Job, result chan<- *Result) {
defer wg.Done()
for job := range jobs {
//log.Printf("worker %d", workerID)
//log.Printf("job %v", job.ValueJob)
err := doSomething(job.ValueJob)
if err != nil {
result <- &Result{Err: err}
}
}
}
func produce(jobs chan<- *Job) {
for i := 1; i < dataFromDB; i++ {
jobs <- &Job{
Id: i,
ValueJob: fmt.Sprintf("data %d", i),
}
}
close(jobs)
}
am I missing something in my worker pool?
for the benchmark test code, it looks like codes from tutorial outs there :) just simple codes to call the functions and I added b.ReportAllocs()
as well