|
| 1 | +// Copyright 2025 FishGoddess. All rights reserved. |
| 2 | +// Use of this source code is governed by a MIT style |
| 3 | +// license that can be found in the LICENSE file. |
| 4 | + |
| 5 | +package main |
| 6 | + |
| 7 | +import ( |
| 8 | + "sync/atomic" |
| 9 | + "testing" |
| 10 | + "time" |
| 11 | + |
| 12 | + "github.com/FishGoddess/goes" |
| 13 | + //"github.com/panjf2000/ants/v2" |
| 14 | +) |
| 15 | + |
| 16 | +const ( |
| 17 | + limit = 256 |
| 18 | + workerNum = limit |
| 19 | + size = limit |
| 20 | + timeLoop = 100_0000 |
| 21 | +) |
| 22 | + |
| 23 | +func bench(num *uint32) { |
| 24 | + atomic.AddUint32(num, 1) |
| 25 | +} |
| 26 | + |
| 27 | +// go test -v -run=none -bench=^BenchmarkLimiter$ -benchmem -benchtime=1s |
| 28 | +func BenchmarkLimiter(b *testing.B) { |
| 29 | + limiter := goes.NewLimiter(limit) |
| 30 | + |
| 31 | + num := uint32(0) |
| 32 | + f := func() { |
| 33 | + bench(&num) |
| 34 | + } |
| 35 | + |
| 36 | + b.RunParallel(func(pb *testing.PB) { |
| 37 | + for pb.Next() { |
| 38 | + limiter.Go(f) |
| 39 | + } |
| 40 | + }) |
| 41 | + |
| 42 | + limiter.Wait() |
| 43 | + b.Logf("num is %d", num) |
| 44 | +} |
| 45 | + |
| 46 | +// go test -v -run=none -bench=^BenchmarkLimiterTime$ -benchmem -benchtime=1s |
| 47 | +func BenchmarkLimiterTime(b *testing.B) { |
| 48 | + limiter := goes.NewLimiter(limit) |
| 49 | + |
| 50 | + num := uint32(0) |
| 51 | + f := func() { |
| 52 | + bench(&num) |
| 53 | + } |
| 54 | + |
| 55 | + beginTime := time.Now() |
| 56 | + for range timeLoop { |
| 57 | + limiter.Go(f) |
| 58 | + } |
| 59 | + |
| 60 | + limiter.Wait() |
| 61 | + |
| 62 | + cost := time.Since(beginTime) |
| 63 | + b.Logf("num is %d, cost is %s", num, cost) |
| 64 | +} |
| 65 | + |
| 66 | +// go test -v -run=none -bench=^BenchmarkExecutor$ -benchmem -benchtime=1s |
| 67 | +func BenchmarkExecutor(b *testing.B) { |
| 68 | + executor := goes.NewExecutor(workerNum) |
| 69 | + |
| 70 | + num := uint32(0) |
| 71 | + task := func() { |
| 72 | + bench(&num) |
| 73 | + } |
| 74 | + |
| 75 | + b.RunParallel(func(pb *testing.PB) { |
| 76 | + for pb.Next() { |
| 77 | + executor.Submit(task) |
| 78 | + } |
| 79 | + }) |
| 80 | + |
| 81 | + executor.Close() |
| 82 | + b.Logf("num is %d", num) |
| 83 | +} |
| 84 | + |
| 85 | +// go test -v -run=none -bench=^BenchmarkExecutorTime$ -benchmem -benchtime=1s |
| 86 | +func BenchmarkExecutorTime(b *testing.B) { |
| 87 | + executor := goes.NewExecutor(size) |
| 88 | + |
| 89 | + num := uint32(0) |
| 90 | + task := func() { |
| 91 | + bench(&num) |
| 92 | + } |
| 93 | + |
| 94 | + beginTime := time.Now() |
| 95 | + for range timeLoop { |
| 96 | + executor.Submit(task) |
| 97 | + } |
| 98 | + |
| 99 | + executor.Close() |
| 100 | + |
| 101 | + cost := time.Since(beginTime) |
| 102 | + b.Logf("num is %d, cost is %s", num, cost) |
| 103 | +} |
| 104 | + |
| 105 | +// // go test -v -run=none -bench=^BenchmarkAntsPool$ -benchmem -benchtime=1s |
| 106 | +// func BenchmarkAntsPool(b *testing.B) { |
| 107 | +// pool, _ := ants.NewPool(size) |
| 108 | +// |
| 109 | +// num := uint32(0) |
| 110 | +// task := func() { |
| 111 | +// bench(&num) |
| 112 | +// } |
| 113 | +// |
| 114 | +// b.RunParallel(func(pb *testing.PB) { |
| 115 | +// for pb.Next() { |
| 116 | +// pool.Submit(task) |
| 117 | +// } |
| 118 | +// }) |
| 119 | +// |
| 120 | +// pool.Release() |
| 121 | +// b.Logf("num is %d", num) |
| 122 | +// } |
| 123 | +// |
| 124 | +// // go test -v -run=none -bench=^BenchmarkAntsPoolTime$ -benchmem -benchtime=1s |
| 125 | +// func BenchmarkAntsPoolTime(b *testing.B) { |
| 126 | +// pool, _ := ants.NewPool(size) |
| 127 | +// |
| 128 | +// num := uint32(0) |
| 129 | +// task := func() { |
| 130 | +// bench(&num) |
| 131 | +// } |
| 132 | +// |
| 133 | +// beginTime := time.Now() |
| 134 | +// for range timeLoop { |
| 135 | +// pool.Submit(task) |
| 136 | +// } |
| 137 | +// |
| 138 | +// pool.Release() |
| 139 | +// |
| 140 | +// cost := time.Since(beginTime) |
| 141 | +// b.Logf("num is %d, cost is %s", num, cost) |
| 142 | +// } |
0 commit comments