forked from mirror/ants
chore: add errorgroup for benchmark
This commit is contained in:
parent
846d76a437
commit
7b1e246b0e
|
@ -25,15 +25,16 @@ package ants
|
||||||
import (
|
import (
|
||||||
"runtime"
|
"runtime"
|
||||||
"sync"
|
"sync"
|
||||||
"sync/atomic"
|
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"golang.org/x/sync/errgroup"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
RunTimes = 1000000
|
RunTimes = 1e6
|
||||||
|
PoolCap = 5e4
|
||||||
BenchParam = 10
|
BenchParam = 10
|
||||||
BenchAntsSize = 200000
|
|
||||||
DefaultExpiredTime = 10 * time.Second
|
DefaultExpiredTime = 10 * time.Second
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -76,10 +77,11 @@ func BenchmarkGoroutines(b *testing.B) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func BenchmarkSemaphore(b *testing.B) {
|
func BenchmarkChannel(b *testing.B) {
|
||||||
var wg sync.WaitGroup
|
var wg sync.WaitGroup
|
||||||
sema := make(chan struct{}, BenchAntsSize)
|
sema := make(chan struct{}, PoolCap)
|
||||||
|
|
||||||
|
b.ResetTimer()
|
||||||
for i := 0; i < b.N; i++ {
|
for i := 0; i < b.N; i++ {
|
||||||
wg.Add(RunTimes)
|
wg.Add(RunTimes)
|
||||||
for j := 0; j < RunTimes; j++ {
|
for j := 0; j < RunTimes; j++ {
|
||||||
|
@ -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) {
|
func BenchmarkAntsPool(b *testing.B) {
|
||||||
var wg sync.WaitGroup
|
var wg sync.WaitGroup
|
||||||
p, _ := NewPool(BenchAntsSize, WithExpiryDuration(DefaultExpiredTime))
|
p, _ := NewPool(PoolCap, WithExpiryDuration(DefaultExpiredTime))
|
||||||
defer p.Release()
|
defer p.Release()
|
||||||
|
|
||||||
b.StartTimer()
|
b.ResetTimer()
|
||||||
for i := 0; i < b.N; i++ {
|
for i := 0; i < b.N; i++ {
|
||||||
wg.Add(RunTimes)
|
wg.Add(RunTimes)
|
||||||
for j := 0; j < RunTimes; j++ {
|
for j := 0; j < RunTimes; j++ {
|
||||||
|
@ -110,7 +131,6 @@ func BenchmarkAntsPool(b *testing.B) {
|
||||||
}
|
}
|
||||||
wg.Wait()
|
wg.Wait()
|
||||||
}
|
}
|
||||||
b.StopTimer()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func BenchmarkGoroutinesThroughput(b *testing.B) {
|
func BenchmarkGoroutinesThroughput(b *testing.B) {
|
||||||
|
@ -122,7 +142,7 @@ func BenchmarkGoroutinesThroughput(b *testing.B) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func BenchmarkSemaphoreThroughput(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 i := 0; i < b.N; i++ {
|
||||||
for j := 0; j < RunTimes; j++ {
|
for j := 0; j < RunTimes; j++ {
|
||||||
sema <- struct{}{}
|
sema <- struct{}{}
|
||||||
|
@ -135,54 +155,13 @@ func BenchmarkSemaphoreThroughput(b *testing.B) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func BenchmarkAntsPoolThroughput(b *testing.B) {
|
func BenchmarkAntsPoolThroughput(b *testing.B) {
|
||||||
p, _ := NewPool(BenchAntsSize, WithExpiryDuration(DefaultExpiredTime))
|
p, _ := NewPool(PoolCap, WithExpiryDuration(DefaultExpiredTime))
|
||||||
defer p.Release()
|
defer p.Release()
|
||||||
b.StartTimer()
|
|
||||||
|
b.ResetTimer()
|
||||||
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++ {
|
||||||
_ = p.Submit(demoFunc)
|
_ = 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())
|
|
||||||
}
|
|
||||||
}()
|
|
||||||
|
|
||||||
b.ResetTimer()
|
|
||||||
for i := 0; i < b.N; i++ {
|
|
||||||
_ = now.Load().(time.Time)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
5
go.mod
5
go.mod
|
@ -2,4 +2,7 @@ module github.com/panjf2000/ants/v2
|
||||||
|
|
||||||
go 1.13
|
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
go.sum
2
go.sum
|
@ -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.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
|
||||||
github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk=
|
github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk=
|
||||||
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
|
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 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
|
||||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
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=
|
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||||
|
|
Loading…
Reference in New Issue