Skip to content

Commit

Permalink
Add unit and performance tests for GoPool
Browse files Browse the repository at this point in the history
- Added a unit test to verify the basic functionality of GoPool.
- Added a performance test to measure the time taken by GoPool to process a million tasks with a pool size of 10000.
- Added a performance test to measure the time taken by direct goroutines to process a million tasks.

Signed-off-by: Daniel Hu <[email protected]>
  • Loading branch information
daniel-hutao committed Jul 21, 2023
1 parent 7e12cc1 commit 10f9796
Showing 1 changed file with 51 additions and 0 deletions.
51 changes: 51 additions & 0 deletions gopool_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package gopool

import (
"sync"
"testing"
"time"
)

func TestGoPool(t *testing.T) {
pool := NewGoPool(100)
for i := 0; i < 1000; i++ {
pool.AddTask(func() {
time.Sleep(10 * time.Millisecond)
})
}
pool.Release()
}

func BenchmarkGoPool(b *testing.B) {
var wg sync.WaitGroup
var taskNum = int(1e6)
pool := NewGoPool(10000)

b.ResetTimer()
for i := 0; i < b.N; i++ {
wg.Add(taskNum)
for num := 0; num < taskNum; num++ {
pool.AddTask(func() {
time.Sleep(10 * time.Millisecond)
wg.Done()
})
}
}
wg.Wait()
pool.Release()
}

func BenchmarkGoroutines(b *testing.B) {
var wg sync.WaitGroup
var taskNum = int(1e6)

for i := 0; i < b.N; i++ {
wg.Add(taskNum)
for num := 0; num < taskNum; num++ {
go func() {
time.Sleep(10 * time.Millisecond)
wg.Done()
}()
}
}
}

0 comments on commit 10f9796

Please sign in to comment.