-
Notifications
You must be signed in to change notification settings - Fork 4
/
scheduler_test.go
195 lines (165 loc) · 4.76 KB
/
scheduler_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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
package scheduler
import (
"testing"
"time"
"github.com/AkihiroSuda/go-dag"
"github.com/docker/docker/pkg/testutil/assert"
)
const (
defaultSeed = 42
defaultMaxSleep = 50 * time.Millisecond
)
func TestExecute0_0(t *testing.T) {
testExecute0(t, 0)
}
func TestExecute0_1(t *testing.T) {
testExecute0(t, 1)
}
func TestExecute0_2(t *testing.T) {
testExecute0(t, 2)
}
func TestExecute0_5(t *testing.T) {
testExecute0(t, 5)
}
func TestExecute0_32(t *testing.T) {
testExecute0(t, 32)
}
func testExecute0(t *testing.T, concurrency uint) {
/*
0 1
| |
2 3
|
/ \
4 5
*/
g := &dag.Graph{
Nodes: []dag.Node{0, 1, 2, 3, 4, 5},
Edges: []dag.Edge{
{Depender: 2, Dependee: 0},
{Depender: 3, Dependee: 1},
{Depender: 4, Dependee: 2},
{Depender: 5, Dependee: 2},
},
}
w := determineFakeWorkload(g, defaultMaxSleep, defaultSeed)
ideal := max(w[0]+w[2]+w[4], w[0]+w[2]+w[5], w[1]+w[3])
sequential := sum(w)
t.Logf("Ideal: %v (%.2f times faster than sequential %v)",
ideal, float64(sequential)/float64(ideal), sequential)
begun := time.Now()
got := testExecute(t, g, concurrency, w)
took := time.Now().Sub(begun)
t.Logf("Took: %v (%2.2f%% of ideal)", took, 100*float64(took)/float64(ideal))
assert.Equal(t, len(got), 6)
assert.Equal(t, indexOf(got, 0) < indexOf(got, 2), true)
assert.Equal(t, indexOf(got, 2) < indexOf(got, 4), true)
assert.Equal(t, indexOf(got, 2) < indexOf(got, 5), true)
assert.Equal(t, indexOf(got, 1) < indexOf(got, 3), true)
}
func TestExecute1_0(t *testing.T) {
testExecute1(t, 0)
}
func TestExecute1_1(t *testing.T) {
testExecute1(t, 1)
}
func TestExecute1_2(t *testing.T) {
testExecute1(t, 2)
}
func TestExecute1_5(t *testing.T) {
testExecute1(t, 5)
}
func TestExecute1_32(t *testing.T) {
testExecute1(t, 32)
}
func testExecute1(t *testing.T, concurrency uint) {
/*
0 1
| |
2 3
| / \
/ \ / |
4 5 |
| |
| ______/
| /
6
*/
g := &dag.Graph{
Nodes: []dag.Node{0, 1, 2, 3, 4, 5, 6},
Edges: []dag.Edge{
{Depender: 2, Dependee: 0},
{Depender: 3, Dependee: 1},
{Depender: 4, Dependee: 2},
{Depender: 5, Dependee: 2},
{Depender: 5, Dependee: 3},
{Depender: 6, Dependee: 4},
{Depender: 6, Dependee: 3},
},
}
w := determineFakeWorkload(g, defaultMaxSleep, defaultSeed)
ideal := max(w[0]+w[2]+w[4]+w[6], w[0]+w[2]+w[3]+w[5], w[0]+w[2]+w[5], w[1]+w[3]+w[5], w[1]+w[3]+w[6])
sequential := sum(w)
t.Logf("Ideal: %v (%.2f times faster than sequential %v)",
ideal, float64(sequential)/float64(ideal), sequential)
begun := time.Now()
got := testExecute(t, g, concurrency, w)
took := time.Now().Sub(begun)
t.Logf("Took: %v (%2.2f%% of ideal)", took, 100*float64(took)/float64(ideal))
assert.Equal(t, len(got), 7)
assert.Equal(t, indexOf(got, 0) < indexOf(got, 2), true)
assert.Equal(t, indexOf(got, 2) < indexOf(got, 4), true)
assert.Equal(t, indexOf(got, 2) < indexOf(got, 5) && indexOf(got, 3) < indexOf(got, 5), true)
assert.Equal(t, indexOf(got, 1) < indexOf(got, 3), true)
assert.Equal(t, indexOf(got, 4) < indexOf(got, 6) && indexOf(got, 3) < indexOf(got, 6), true)
}
func TestExecute2_0(t *testing.T) {
testExecute2(t, 0)
}
func TestExecute2_1(t *testing.T) {
testExecute2(t, 1)
}
func TestExecute2_2(t *testing.T) {
testExecute2(t, 2)
}
func TestExecute2_5(t *testing.T) {
testExecute2(t, 5)
}
func TestExecute2_32(t *testing.T) {
testExecute2(t, 32)
}
func testExecute2(t *testing.T, concurrency uint) {
/*
42
/ \
100 200
\ / |
101 |
\ |
102
*/
g := &dag.Graph{
Nodes: []dag.Node{42, 100, 101, 102, 200},
Edges: []dag.Edge{
{Depender: 100, Dependee: 42},
{Depender: 200, Dependee: 42},
{Depender: 101, Dependee: 100},
{Depender: 101, Dependee: 200},
{Depender: 102, Dependee: 101},
{Depender: 102, Dependee: 101},
},
}
w := determineFakeWorkload(g, defaultMaxSleep, defaultSeed)
ideal := max(w[42]+w[100]+w[101]+w[102], w[42]+w[200]+w[101]+w[102], w[42]+w[200]+w[102])
sequential := sum(w)
t.Logf("Ideal: %v (%.2f times faster than sequential %v)",
ideal, float64(sequential)/float64(ideal), sequential)
begun := time.Now()
got := testExecute(t, g, concurrency, w)
took := time.Now().Sub(begun)
t.Logf("Took: %v (%2.2f%% of ideal)", took, 100*float64(took)/float64(ideal))
assert.Equal(t, len(got), 5)
assert.Equal(t, indexOf(got, 42) < indexOf(got, 100) && indexOf(got, 42) < indexOf(got, 200), true)
assert.Equal(t, indexOf(got, 100) < indexOf(got, 101) && indexOf(got, 200) < indexOf(got, 101), true)
assert.Equal(t, indexOf(got, 101) < indexOf(got, 102) && indexOf(got, 200) < indexOf(got, 102), true)
}