github.com/zwh8800/pool@v3.1.0+incompatible/unlimited_pool_benchmarks_test.go (about) 1 package pool 2 3 import ( 4 "testing" 5 "time" 6 ) 7 8 func BenchmarkUnlimitedSmallRun(b *testing.B) { 9 10 res := make([]WorkUnit, 10) 11 12 b.ReportAllocs() 13 14 pool := New() 15 defer pool.Close() 16 17 fn := func(wu WorkUnit) (interface{}, error) { 18 time.Sleep(time.Millisecond * 500) 19 if wu.IsCancelled() { 20 return nil, nil 21 } 22 time.Sleep(time.Millisecond * 500) 23 return 1, nil 24 } 25 26 for i := 0; i < 10; i++ { 27 res[i] = pool.Queue(fn) 28 } 29 30 var count int 31 32 for _, cw := range res { 33 34 cw.Wait() 35 36 if cw.Error() == nil { 37 count += cw.Value().(int) 38 } 39 } 40 41 if count != 10 { 42 b.Fatal("Count Incorrect") 43 } 44 } 45 46 func BenchmarkUnlimitedSmallCancel(b *testing.B) { 47 48 res := make([]WorkUnit, 0, 20) 49 50 b.ReportAllocs() 51 52 pool := New() 53 defer pool.Close() 54 55 newFunc := func(i int) WorkFunc { 56 return func(wu WorkUnit) (interface{}, error) { 57 time.Sleep(time.Millisecond * 500) 58 if wu.IsCancelled() { 59 return nil, nil 60 } 61 time.Sleep(time.Millisecond * 500) 62 return i, nil 63 } 64 } 65 66 for i := 0; i < 20; i++ { 67 if i == 6 { 68 pool.Cancel() 69 } 70 res = append(res, pool.Queue(newFunc(i))) 71 } 72 73 for _, wrk := range res { 74 if wrk == nil { 75 continue 76 } 77 wrk.Wait() 78 } 79 } 80 81 func BenchmarkUnlimitedLargeCancel(b *testing.B) { 82 83 res := make([]WorkUnit, 0, 1000) 84 85 b.ReportAllocs() 86 87 pool := New() 88 defer pool.Close() 89 90 newFunc := func(i int) WorkFunc { 91 return func(wu WorkUnit) (interface{}, error) { 92 time.Sleep(time.Millisecond * 500) 93 if wu.IsCancelled() { 94 return nil, nil 95 } 96 time.Sleep(time.Millisecond * 500) 97 return i, nil 98 } 99 } 100 101 for i := 0; i < 1000; i++ { 102 if i == 6 { 103 pool.Cancel() 104 } 105 res = append(res, pool.Queue(newFunc(i))) 106 } 107 108 for _, wrk := range res { 109 if wrk == nil { 110 continue 111 } 112 wrk.Wait() 113 } 114 } 115 116 func BenchmarkUnlimitedLargeRun(b *testing.B) { 117 118 res := make([]WorkUnit, 100) 119 120 b.ReportAllocs() 121 122 pool := New() 123 defer pool.Close() 124 125 newFunc := func(i int) WorkFunc { 126 return func(wu WorkUnit) (interface{}, error) { 127 time.Sleep(time.Millisecond * 500) 128 if wu.IsCancelled() { 129 return nil, nil 130 } 131 time.Sleep(time.Millisecond * 500) 132 return 1, nil 133 } 134 } 135 136 for i := 0; i < 100; i++ { 137 res[i] = pool.Queue(newFunc(i)) 138 } 139 140 var count int 141 142 for _, cw := range res { 143 144 cw.Wait() 145 146 count += cw.Value().(int) 147 } 148 149 if count != 100 { 150 b.Fatalf("Count Incorrect, Expected '100' Got '%d'", count) 151 } 152 } 153 154 func BenchmarkUnlimitedBatchSmallRun(b *testing.B) { 155 156 fn := func(wu WorkUnit) (interface{}, error) { 157 time.Sleep(time.Millisecond * 500) 158 if wu.IsCancelled() { 159 return nil, nil 160 } 161 time.Sleep(time.Millisecond * 500) 162 return 1, nil 163 } 164 165 pool := New() 166 defer pool.Close() 167 168 batch := pool.Batch() 169 170 for i := 0; i < 10; i++ { 171 batch.Queue(fn) 172 } 173 174 batch.QueueComplete() 175 176 var count int 177 178 for cw := range batch.Results() { 179 count += cw.Value().(int) 180 } 181 182 if count != 10 { 183 b.Fatal("Count Incorrect") 184 } 185 }