-
Notifications
You must be signed in to change notification settings - Fork 0
/
workers_test.go
144 lines (131 loc) · 2.78 KB
/
workers_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
package workers
import (
"fmt"
"testing"
"time"
)
var workers = New(10)
type tester struct {
name string
n int
}
//单实例
func TestWorkers(t *testing.T) {
type copyAt struct {
n int
}
for i := 1; i <= 100; i++ {
c := copyAt{n: i}
//利用 Submit 提交一个等候执行的方法
workers.Submit(func() {
time.Sleep(time.Second * 2)
fmt.Println(i, c.n)
})
}
workers.Wait()
for {
time.Sleep(time.Second)
fmt.Println("==================", workers.JobTotal, "==================")
}
}
//任务终止
func TestWorkersStop(t *testing.T) {
for i := 0; i < 100; i++ {
workers.Add(tester{n: i})
}
fmt.Println("等待1")
timeout := workers.Wait(time.Second * 3)
if timeout {
workers.Stop()
}
if timeout {
fmt.Println("结果超时")
} else {
fmt.Println("结果完成")
}
fmt.Println("等待2")
timeout = workers.Wait(time.Second * 3)
if timeout {
fmt.Println("结果超时")
} else {
fmt.Println("结果完成")
}
fmt.Println("等待3")
timeout = workers.Wait(time.Second * 3)
if timeout {
fmt.Println("结果超时")
} else {
fmt.Println("结果完成")
}
time.Sleep(time.Second * 600)
}
//任务终止后继续
func TestWorkersStopNext(t *testing.T) {
for i := 0; i < 100; i++ {
workers.Add(tester{n: i})
}
if workers.Wait(time.Second * 3) {
//超时后停止
workers.Stop()
fmt.Println("超时,终止任务")
}
fmt.Println("继续任务")
//继续
for i := 100; i < 150; i++ {
workers.Add(tester{n: i})
}
fmt.Println("next wait:", workers.Wait())
}
//多实例等待
func TestMultipleWorkers(t *testing.T) {
workers1 := New(5)
workers2 := New(5)
//超时时间
//workers1.Timeout = time.Second * 1
//workers2.Timeout = time.Second * 3
for i := 0; i < 50; i++ {
workers1.Add(tester{n: i, name: "A"})
}
for i := 0; i < 50; i++ {
workers2.Add(tester{n: i, name: "B"})
}
timeout := WaitAll(workers1, workers2)
fmt.Println(timeout)
time.Sleep(time.Second * 600)
}
func (t tester) ToWork() error {
time.Sleep(time.Second * 2)
fmt.Println(t.n, t.name)
return nil
}
const (
BenchParam = 10
BenchAntsSize = 200000
DefaultExpiredTime = 10 * time.Second
)
//cpu: Intel(R) Core(TM) i7-1068NG7 CPU @ 2.30GHz
//BenchmarkWorkers
//BenchmarkWorkers-8 14158902 72.60 ns/op
func BenchmarkWorkers(b *testing.B) {
for i := 0; i < b.N; i++ {
workers.Submit(func() {
demoFunc()
})
}
}
//
//var antsPool, _ = ants.NewPool(BenchAntsSize, ants.WithExpiryDuration(DefaultExpiredTime))
//
////cpu: Intel(R) Core(TM) i7-1068NG7 CPU @ 2.30GHz
////BenchmarkAnts
////BenchmarkAnts-8 1448713 786.7 ns/op
//func BenchmarkAnts(b *testing.B) {
// for i := 0; i < b.N; i++ {
// antsPool.Submit(func() {
// demoFunc()
// })
// }
//}
func demoFunc() {
time.Sleep(time.Duration(BenchParam) * time.Millisecond)
}