fix benchmarks and add semaphore

This commit is contained in:
Egon Elbre 2018-10-03 10:12:58 +03:00
parent 29730bb703
commit c1738ae964
1 changed files with 44 additions and 3 deletions

View File

@ -43,7 +43,7 @@ const (
)
const (
RunTimes = 10000000
Param = 100
Param = 0
AntsSize = 1000
TestSize = 10000
)
@ -68,7 +68,6 @@ func demoPoolFunc(args interface{}) error {
func BenchmarkGoroutineWithFunc(b *testing.B) {
var wg sync.WaitGroup
for i := 0; i < b.N; i++ {
for j := 0; j < RunTimes; j++ {
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) {
var wg sync.WaitGroup
p, _ := ants.NewPoolWithFunc(AntsSize, func(i interface{}) error {
@ -102,11 +119,35 @@ func BenchmarkAntsPoolWithFunc(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 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) {