@@ -14,53 +14,58 @@ import (
14
14
)
15
15
16
16
const (
17
- workerCount = 1000
17
+ workerCount = 100000
18
18
workerTimeout = time .Millisecond * 300
19
19
runTimes = 100000
20
20
)
21
21
22
22
type WorkerOne struct {
23
+ Count int
24
+ sync.Mutex
23
25
}
24
26
type WorkerTwo struct {
27
+ Count int
28
+ sync.Mutex
25
29
}
26
30
27
- func NewWorkerOne () Worker {
31
+ func NewWorkerOne () * WorkerOne {
28
32
return & WorkerOne {}
29
33
}
30
34
31
- func NewWorkerTwo () Worker {
35
+ func NewWorkerTwo () * WorkerTwo {
32
36
return & WorkerTwo {}
33
37
}
34
38
39
+ func (wo * WorkerOne ) CurrentCount () int {
40
+ wo .Lock ()
41
+ defer wo .Unlock ()
42
+ return wo .Count
43
+ }
44
+
35
45
func (wo * WorkerOne ) Work (in interface {}, out chan <- interface {}) error {
36
- var workerOne = "worker_one"
37
46
mut .Lock ()
38
- if val , ok := count [workerOne ]; ok {
39
- count [workerOne ] = val + 1
40
- } else {
41
- count [workerOne ] = 1
42
- }
47
+ wo .Count = wo .Count + 1
43
48
mut .Unlock ()
44
49
45
50
total := in .(int ) * 2
46
51
out <- total
47
52
return nil
48
53
}
49
54
55
+ func (wt * WorkerTwo ) CurrentCount () int {
56
+ wt .Lock ()
57
+ defer wt .Unlock ()
58
+ return wt .Count
59
+ }
60
+
50
61
func (wt * WorkerTwo ) Work (in interface {}, out chan <- interface {}) error {
51
- var workerTwo = "worker_two"
52
62
mut .Lock ()
53
- if val , ok := count [workerTwo ]; ok {
54
- count [workerTwo ] = val + 1
55
- } else {
56
- count [workerTwo ] = 1
57
- }
63
+ wt .Count = wt .Count + 1
58
64
mut .Unlock ()
59
65
return nil
60
66
}
61
67
62
68
var (
63
- count = make (map [string ]int )
64
69
mut = sync.RWMutex {}
65
70
err = errors .New ("test error" )
66
71
deadline = func () time.Time { return time .Now ().Add (workerTimeout ) }
@@ -184,24 +189,57 @@ func TestWorkers(t *testing.T) {
184
189
workerOne .Send (i )
185
190
}
186
191
187
- if err := workerOne .Wait (); err != nil && ! tt .errExpected {
188
- fmt .Println (err )
189
- t .Fail ()
192
+ if err := workerOne .Wait (); err != nil && (! tt .errExpected ) {
193
+ t .Error (err )
190
194
}
191
195
if err := workerTwo .Wait (); err != nil && ! tt .errExpected {
192
- fmt .Println (err )
193
- t .Fail ()
196
+ t .Error (err )
194
197
}
195
198
})
196
199
}
197
200
}
198
201
199
- func TestWorkersFinish (t * testing.T ) {
202
+ func TestWorkersFinish100 (t * testing.T ) {
203
+ const workCount = 100
204
+ ctx := context .Background ()
205
+ w1 := NewWorkerOne ()
206
+ w2 := NewWorkerTwo ()
207
+ workerOne := NewRunner (ctx , w1 , 1000 ).Start ()
208
+ workerTwo := NewRunner (ctx , w2 , 1000 ).InFrom (workerOne ).Start ()
209
+
210
+ for i := 0 ; i < workCount ; i ++ {
211
+ workerOne .Send (rand .Intn (100 ))
212
+ }
213
+
214
+ if err := workerOne .Wait (); err != nil {
215
+ fmt .Println (err )
216
+ }
217
+
218
+ if err := workerTwo .Wait (); err != nil {
219
+ fmt .Println (err )
220
+ }
221
+
222
+ if w1 .CurrentCount () != workCount {
223
+ t .Log ("worker one failed to finish," , "worker_one count" , w1 .CurrentCount (), "/ 100000" )
224
+ t .Fail ()
225
+ }
226
+ if w2 .CurrentCount () != workCount {
227
+ t .Log ("worker two failed to finish," , "worker_two count" , w2 .CurrentCount (), "/ 100000" )
228
+ t .Fail ()
229
+ }
230
+
231
+ t .Logf ("worker_one count: %d, worker_two count: %d" , w1 .CurrentCount (), w2 .CurrentCount ())
232
+ }
233
+
234
+ func TestWorkersFinish100000 (t * testing.T ) {
235
+ const workCount = 100000
200
236
ctx := context .Background ()
201
- workerOne := NewRunner (ctx , NewWorkerOne (), 1000 ).Start ()
202
- workerTwo := NewRunner (ctx , NewWorkerTwo (), 1000 ).InFrom (workerOne ).Start ()
237
+ w1 := NewWorkerOne ()
238
+ w2 := NewWorkerTwo ()
239
+ workerOne := NewRunner (ctx , w1 , 1000 ).Start ()
240
+ workerTwo := NewRunner (ctx , w2 , 1000 ).InFrom (workerOne ).Start ()
203
241
204
- for i := 0 ; i < 100000 ; i ++ {
242
+ for i := 0 ; i < workCount ; i ++ {
205
243
workerOne .Send (rand .Intn (100 ))
206
244
}
207
245
@@ -213,28 +251,103 @@ func TestWorkersFinish(t *testing.T) {
213
251
fmt .Println (err )
214
252
}
215
253
216
- if count [ "worker_one" ] != 100000 {
217
- fmt . Println ("worker one failed to finish," , "worker_one count" , count [ "worker_one" ] , "/ 100000" )
254
+ if w1 . CurrentCount () != workCount {
255
+ t . Log ("worker one failed to finish," , "worker_one count" , w1 . CurrentCount () , "/ 100000" )
218
256
t .Fail ()
219
257
}
220
- if count [ "worker_two" ] != 100000 {
221
- fmt . Println ("worker two failed to finish," , "worker_two count" , count [ "worker_two" ] , "/ 100000" )
258
+ if w2 . CurrentCount () != workCount {
259
+ t . Log ("worker two failed to finish," , "worker_two count" , w2 . CurrentCount () , "/ 100000" )
222
260
t .Fail ()
223
261
}
262
+
263
+ t .Logf ("worker_one count: %d, worker_two count: %d" , w1 .CurrentCount (), w2 .CurrentCount ())
224
264
}
225
265
226
- func BenchmarkGoWorkers (b * testing.B ) {
266
+ func TestWorkersFinish1000000 (t * testing.T ) {
267
+ const workCount = 1000000
227
268
ctx := context .Background ()
228
- worker := NewRunner (ctx , NewTestWorkerObject (workBasicNoOut ()), workerCount ).Start ()
269
+ w1 := NewWorkerOne ()
270
+ w2 := NewWorkerTwo ()
271
+ workerOne := NewRunner (ctx , w1 , 1000 ).Start ()
272
+ workerTwo := NewRunner (ctx , w2 , 1000 ).InFrom (workerOne ).Start ()
273
+
274
+ for i := 0 ; i < workCount ; i ++ {
275
+ workerOne .Send (rand .Intn (100 ))
276
+ }
277
+
278
+ if err := workerOne .Wait (); err != nil {
279
+ fmt .Println (err )
280
+ }
281
+
282
+ if err := workerTwo .Wait (); err != nil {
283
+ fmt .Println (err )
284
+ }
229
285
230
- b .StartTimer ()
286
+ if w1 .CurrentCount () != workCount {
287
+ t .Log ("worker one failed to finish," , "worker_one count" , w1 .CurrentCount (), "/ 100000" )
288
+ t .Fail ()
289
+ }
290
+ if w2 .CurrentCount () != workCount {
291
+ t .Log ("worker two failed to finish," , "worker_two count" , w2 .CurrentCount (), "/ 100000" )
292
+ t .Fail ()
293
+ }
294
+
295
+ t .Logf ("worker_one count: %d, worker_two count: %d" , w1 .CurrentCount (), w2 .CurrentCount ())
296
+ }
297
+
298
+ func BenchmarkGoWorkers1to1 (b * testing.B ) {
299
+ worker := NewRunner (context .Background (), NewTestWorkerObject (workBasicNoOut ()), 1000 ).Start ()
300
+
301
+ b .ResetTimer ()
231
302
for i := 0 ; i < b .N ; i ++ {
232
- for j := 0 ; j < runTimes ; j ++ {
303
+ for j := 0 ; j < 1000 ; j ++ {
233
304
worker .Send (j )
234
305
}
235
306
}
236
-
237
307
b .StopTimer ()
308
+
309
+ if err := worker .Wait (); err != nil {
310
+ b .Error (err )
311
+ }
312
+ }
313
+
314
+ func Benchmark100GoWorkers (b * testing.B ) {
315
+ b .ReportAllocs ()
316
+ worker := NewRunner (context .Background (), NewTestWorkerObject (workBasicNoOut ()), 100 ).Start ()
317
+
318
+ b .ResetTimer ()
319
+ for i := 0 ; i < b .N ; i ++ {
320
+ worker .Send (i )
321
+ }
322
+
323
+ if err := worker .Wait (); err != nil {
324
+ b .Error (err )
325
+ }
326
+ }
327
+
328
+ func Benchmark1000GoWorkers (b * testing.B ) {
329
+ b .ReportAllocs ()
330
+ worker := NewRunner (context .Background (), NewTestWorkerObject (workBasicNoOut ()), 1000 ).Start ()
331
+
332
+ b .ResetTimer ()
333
+ for i := 0 ; i < b .N ; i ++ {
334
+ worker .Send (i )
335
+ }
336
+
337
+ if err := worker .Wait (); err != nil {
338
+ b .Error (err )
339
+ }
340
+ }
341
+
342
+ func Benchmark10000GoWorkers (b * testing.B ) {
343
+ b .ReportAllocs ()
344
+ worker := NewRunner (context .Background (), NewTestWorkerObject (workBasicNoOut ()), 10000 ).Start ()
345
+
346
+ b .ResetTimer ()
347
+ for i := 0 ; i < b .N ; i ++ {
348
+ worker .Send (i )
349
+ }
350
+
238
351
if err := worker .Wait (); err != nil {
239
352
b .Error (err )
240
353
}
0 commit comments