Merge pull request #15 from egonelbre/fix-benchmarks

Fixes to benchmarks and added semaphore comparison
This commit is contained in:
Andy Pan 2018-10-03 20:33:58 +08:00 committed by GitHub
commit c10f80f7b7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 43 additions and 2 deletions

View File

@ -68,7 +68,6 @@ func demoPoolFunc(args interface{}) error {
func BenchmarkGoroutineWithFunc(b *testing.B) { func BenchmarkGoroutineWithFunc(b *testing.B) {
var wg sync.WaitGroup var wg sync.WaitGroup
for i := 0; i < b.N; i++ { for i := 0; i < b.N; i++ {
for j := 0; j < RunTimes; j++ { for j := 0; j < RunTimes; j++ {
wg.Add(1) wg.Add(1)
@ -81,6 +80,24 @@ func BenchmarkGoroutineWithFunc(b *testing.B) {
} }
} }
func BenchmarkSemaphoreWithFunc(b *testing.B) {
var wg sync.WaitGroup
sema := make(chan struct{}, AntsSize)
for i := 0; i < b.N; i++ {
wg.Add(RunTimes)
for j := 0; j < RunTimes; j++ {
sema <- struct{}{}
go func() {
demoPoolFunc(Param)
<-sema
wg.Done()
}()
}
wg.Wait()
}
}
func BenchmarkAntsPoolWithFunc(b *testing.B) { func BenchmarkAntsPoolWithFunc(b *testing.B) {
var wg sync.WaitGroup var wg sync.WaitGroup
p, _ := ants.NewPoolWithFunc(AntsSize, func(i interface{}) error { p, _ := ants.NewPoolWithFunc(AntsSize, func(i interface{}) error {
@ -102,11 +119,35 @@ func BenchmarkAntsPoolWithFunc(b *testing.B) {
} }
func BenchmarkGoroutine(b *testing.B) { func BenchmarkGoroutine(b *testing.B) {
var wg sync.WaitGroup
wg.Add(b.N * RunTimes)
for i := 0; i < b.N; i++ { for i := 0; i < b.N; i++ {
for j := 0; j < RunTimes; j++ { for j := 0; j < RunTimes; j++ {
go demoPoolFunc(Param) go func() {
demoPoolFunc(Param)
wg.Done()
}()
} }
} }
wg.Wait()
}
func BenchmarkSemaphore(b *testing.B) {
var wg sync.WaitGroup
sema := make(chan struct{}, AntsSize)
wg.Add(RunTimes * b.N)
for i := 0; i < b.N; i++ {
for j := 0; j < RunTimes; j++ {
sema <- struct{}{}
go func() {
demoPoolFunc(Param)
<-sema
wg.Done()
}()
}
}
wg.Wait()
} }
func BenchmarkAntsPool(b *testing.B) { func BenchmarkAntsPool(b *testing.B) {