Skip to content

Commit

Permalink
chore: add errorgroup for benchmark
Browse files Browse the repository at this point in the history
  • Loading branch information
panjf2000 committed Dec 11, 2022
1 parent 846d76a commit 7b1e246
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 54 deletions.
85 changes: 32 additions & 53 deletions ants_benchmark_test.go
Expand Up @@ -25,15 +25,16 @@ package ants
import (
"runtime"
"sync"
"sync/atomic"
"testing"
"time"

"golang.org/x/sync/errgroup"
)

const (
RunTimes = 1000000
RunTimes = 1e6
PoolCap = 5e4
BenchParam = 10
BenchAntsSize = 200000
DefaultExpiredTime = 10 * time.Second
)

Expand Down Expand Up @@ -76,10 +77,11 @@ func BenchmarkGoroutines(b *testing.B) {
}
}

func BenchmarkSemaphore(b *testing.B) {
func BenchmarkChannel(b *testing.B) {
var wg sync.WaitGroup
sema := make(chan struct{}, BenchAntsSize)
sema := make(chan struct{}, PoolCap)

b.ResetTimer()
for i := 0; i < b.N; i++ {
wg.Add(RunTimes)
for j := 0; j < RunTimes; j++ {
Expand All @@ -94,12 +96,31 @@ func BenchmarkSemaphore(b *testing.B) {
}
}

func BenchmarkErrGroup(b *testing.B) {
var wg sync.WaitGroup
var pool errgroup.Group
pool.SetLimit(PoolCap)

b.ResetTimer()
for i := 0; i < b.N; i++ {
wg.Add(RunTimes)
for j := 0; j < RunTimes; j++ {
pool.Go(func() error {
demoFunc()
wg.Done()
return nil
})
}
wg.Wait()
}
}

func BenchmarkAntsPool(b *testing.B) {
var wg sync.WaitGroup
p, _ := NewPool(BenchAntsSize, WithExpiryDuration(DefaultExpiredTime))
p, _ := NewPool(PoolCap, WithExpiryDuration(DefaultExpiredTime))
defer p.Release()

b.StartTimer()
b.ResetTimer()
for i := 0; i < b.N; i++ {
wg.Add(RunTimes)
for j := 0; j < RunTimes; j++ {
Expand All @@ -110,7 +131,6 @@ func BenchmarkAntsPool(b *testing.B) {
}
wg.Wait()
}
b.StopTimer()
}

func BenchmarkGoroutinesThroughput(b *testing.B) {
Expand All @@ -122,7 +142,7 @@ func BenchmarkGoroutinesThroughput(b *testing.B) {
}

func BenchmarkSemaphoreThroughput(b *testing.B) {
sema := make(chan struct{}, BenchAntsSize)
sema := make(chan struct{}, PoolCap)
for i := 0; i < b.N; i++ {
for j := 0; j < RunTimes; j++ {
sema <- struct{}{}
Expand All @@ -135,54 +155,13 @@ func BenchmarkSemaphoreThroughput(b *testing.B) {
}

func BenchmarkAntsPoolThroughput(b *testing.B) {
p, _ := NewPool(BenchAntsSize, WithExpiryDuration(DefaultExpiredTime))
p, _ := NewPool(PoolCap, WithExpiryDuration(DefaultExpiredTime))
defer p.Release()
b.StartTimer()
for i := 0; i < b.N; i++ {
for j := 0; j < RunTimes; j++ {
_ = p.Submit(demoFunc)
}
}
b.StopTimer()
}

func BenchmarkTimeNow(b *testing.B) {
for i := 0; i < b.N; i++ {
_ = time.Now()
}
}

func BenchmarkTimeNowCache(b *testing.B) {
var (
now atomic.Value
offset int32
)

now.Store(time.Now())
go func() {
for range time.Tick(500 * time.Millisecond) {
now.Store(time.Now())
atomic.StoreInt32(&offset, 0)
}
}()

b.ResetTimer()
for i := 0; i < b.N; i++ {
_ = now.Load().(time.Time).Add(time.Duration(atomic.AddInt32(&offset, 1)))
}
}

func BenchmarkTimeNowCache1(b *testing.B) {
var now atomic.Value
now.Store(time.Now())
go func() {
for range time.Tick(500 * time.Millisecond) {
now.Store(time.Now())
for j := 0; j < RunTimes; j++ {
_ = p.Submit(demoFunc)
}
}()

b.ResetTimer()
for i := 0; i < b.N; i++ {
_ = now.Load().(time.Time)
}
}
5 changes: 4 additions & 1 deletion go.mod
Expand Up @@ -2,4 +2,7 @@ module github.com/panjf2000/ants/v2

go 1.13

require github.com/stretchr/testify v1.8.1
require (
github.com/stretchr/testify v1.8.1
golang.org/x/sync v0.1.0
)
2 changes: 2 additions & 0 deletions go.sum
Expand Up @@ -10,6 +10,8 @@ github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk=
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
golang.org/x/sync v0.1.0 h1:wsuoTGHzEhffawBOhz5CYhcrV4IdKZbEyZjBMuTp12o=
golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
Expand Down

0 comments on commit 7b1e246

Please sign in to comment.