Created
March 3, 2023 12:09
-
-
Save lxzan/5c75735f9cd8a211e637fa622efc858a to your computer and use it in GitHub Desktop.
This file contains hidden or 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 bench | |
import ( | |
"github.com/bytedance/gopkg/util/gopool" | |
"github.com/lesismal/nbio/taskpool" | |
ants "github.com/panjf2000/ants/v2" | |
"github.com/redis/go-redis/v9" | |
"sync" | |
"testing" | |
) | |
const ( | |
PoolSize = 16 | |
BenchTimes = 1000 | |
N = 100000 | |
) | |
func BenchmarkGwsWorkerQueue(b *testing.B) { | |
b.ReportAllocs() | |
b.ResetTimer() | |
wq := NewWorkerQueue(PoolSize) | |
for i := 0; i < b.N; i++ { | |
wg := sync.WaitGroup{} | |
wg.Add(BenchTimes) | |
for j := 0; j < BenchTimes; j++ { | |
wq.Push(Job(func() { | |
demoFunc() | |
wg.Done() | |
})) | |
} | |
wg.Wait() | |
} | |
} | |
func BenchmarkGopool(b *testing.B) { | |
p := gopool.NewPool("test", PoolSize, &gopool.Config{}) | |
b.ReportAllocs() | |
b.ResetTimer() | |
for i := 0; i < b.N; i++ { | |
wg := sync.WaitGroup{} | |
wg.Add(BenchTimes) | |
for j := 0; j < BenchTimes; j++ { | |
p.Go(func() { | |
demoFunc() | |
wg.Done() | |
}) | |
} | |
wg.Wait() | |
} | |
} | |
func BenchmarkAnts(b *testing.B) { | |
p, _ := ants.NewPool(PoolSize) | |
defer p.Release() | |
b.ReportAllocs() | |
b.ResetTimer() | |
for i := 0; i < b.N; i++ { | |
wg := sync.WaitGroup{} | |
wg.Add(BenchTimes) | |
for j := 0; j < BenchTimes; j++ { | |
p.Submit(func() { | |
demoFunc() | |
wg.Done() | |
}) | |
} | |
wg.Wait() | |
} | |
} | |
func BenchmarkNbio(b *testing.B) { | |
p := taskpool.NewMixedPool(PoolSize, 1, 10000) | |
//defer p.Stop() | |
b.ReportAllocs() | |
b.ResetTimer() | |
for i := 0; i < b.N; i++ { | |
wg := sync.WaitGroup{} | |
wg.Add(BenchTimes) | |
for j := 0; j < BenchTimes; j++ { | |
p.Go(func() { | |
demoFunc() | |
wg.Done() | |
}) | |
} | |
wg.Wait() | |
} | |
} | |
var client *redis.Client | |
func init() { | |
client = redis.NewClient(&redis.Options{}) | |
} | |
func demoFunc() { | |
//client.Get(context.Background(), "alc:from_timestamp") | |
var sum = 0 | |
for i := 0; i < N; i++ { | |
sum += i | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment