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