-
Notifications
You must be signed in to change notification settings - Fork 0
/
deadpool_test.go
105 lines (80 loc) · 1.59 KB
/
deadpool_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
package deadpool
import (
"fmt"
"os"
"testing"
"time"
)
func Test_deadpool_ErrsOnCreate(t *testing.T) {
_, err := New(WithMax(-1))
if err == nil {
t.Log("ErrInvalidMaxWorkers expected")
t.FailNow()
}
_, err = New(WithCap(-1))
if err == nil {
t.Log("ErrInvalidCap expected")
t.FailNow()
}
_, err = New(WithExecutor(nil))
if err == nil {
t.Log("ErrInvalidExecutor expected")
t.FailNow()
}
_, err = New(WithSpawner(nil))
if err == nil {
t.Log("ErrInvalidSpawner expected")
t.FailNow()
}
}
func Test_deadpool_Flow(t *testing.T) {
// os.Setenv("DEBUG", "TRUE")
d, err := New(WithMax(6))
if err != nil {
t.Log(err)
t.FailNow()
}
start := time.Now().UTC()
for i := 1; i <= 128; i++ {
func(i int) {
d.Submit(newTaskTest(i))
}(i)
}
d.WaitAll()
t.Logf("tiempo acumulado de proceso: %v\n", d.CummlatedWorkTime())
t.Logf("tiempo promedio por tarea: %v \n", d.AVGTime())
t.Logf("tiempo total de proceso: %v", time.Since(start))
}
func Benchmark_deadpool_Flow(b *testing.B) {
// os.Setenv("DEBUG", "TRUE")
task := newTaskTest(-1)
for k := 0; k <= b.N; k++ {
d, _ := New(WithMax(6))
for i := 1; i <= 128; i++ {
func(i int) {
task.id = i
d.Submit(task)
}(i)
}
d.WaitAll()
}
}
type taskTest struct {
id int
}
func newTaskTest(id int) *taskTest {
return &taskTest{
id: id,
}
}
func (t *taskTest) Run() {
defer func() {
if os.Getenv("DEBUG") == "TRUE" {
fmt.Printf(" stoping task %d\n", t.id)
}
}()
if os.Getenv("DEBUG") == "TRUE" {
fmt.Printf(" starting task %d\n", t.id)
time.Sleep(10 * time.Millisecond)
}
}