Created
August 31, 2015 14:20
-
-
Save zabawaba99/7d9193ff0752186ad6e6 to your computer and use it in GitHub Desktop.
Benchmarking new go routine vs pool
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package wrkpool | |
import ( | |
"sync" | |
"testing" | |
) | |
const RunCount = 10000 | |
func BenchmarkTestNewRoutine(b *testing.B) { | |
for i := 0; i < b.N; i++ { | |
var wg sync.WaitGroup | |
for j := 0; j < RunCount; j++ { | |
wg.Add(1) | |
go func() { | |
SomeJob() | |
wg.Done() | |
}() | |
} | |
wg.Wait() | |
} | |
} | |
func BenchmarkTestPool(b *testing.B) { | |
for i := 0; i < b.N; i++ { | |
var wg sync.WaitGroup | |
pool := make(chan struct{}, 25) | |
// fill up the pool | |
for i := 0; i < 25; i++ { | |
pool <- struct{}{} | |
} | |
for j := 0; j < RunCount; j++ { | |
<-pool | |
wg.Add(1) | |
go func() { | |
SomeJob() | |
pool <- struct{}{} | |
wg.Done() | |
}() | |
} | |
wg.Wait() | |
} | |
} | |
func SomeJob() int { | |
var result int | |
for i := 0; i < 10000; i++ { | |
result += i | |
} | |
return result | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
RunCount at 1000
RunCount at 10000