-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
340 lines (283 loc) · 7.49 KB
/
main.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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
package main
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
//
// Copyright 2019, Gunnar Þór Magnússon
import (
"flag"
"fmt"
"math"
"math/rand"
"strings"
"time"
exprand "golang.org/x/exp/rand"
"gonum.org/v1/gonum/stat/distuv"
)
func help(def Defaults) string {
return strings.TrimSpace(fmt.Sprintf(`
queuesim - Simulate a simple bounded queue
USE:
queuesim [OPTION]...
queuesim simulates a simple bounded queue using discrete ticks for time. It
simulates a single producer and consumer of the queue, where waiting requests
can time out, and keeps track of the successes, timeouts, and rejections.
It can be helpful to imagine a single tick being one millisecond long when
setting values for the various program options.
OPTIONS:
-method=METHOD Method to use when popping items from queue (default: %s)
Accepted values: FIFO, FILO, RANDOM.
-rate=RATE A new request comes every RATE ticks (default %d)
-size=SIZE Size of queue (default %d)
-timeout=TIMEOUT Requests have TIMEOUT ticks to complete (default %d)
-work=WORK Requests take WORK ticks to complete (default %d)
-ticks=TICKS Run for TICKS ticks (default %d)
-h, -help Print help and exit
-version Print version and exit
CONTRIBUTING:
Patches are welcome on the project's GitHub page:
https://www.github.com/gunnihinn/queuesim
COPYRIGHT:
This software is licensed under the GPLv3.
Copyright 2019, Gunnar Þór Magnússon <gunnar@magnusson.io>.
`, def.method, def.rate, def.size, def.timeout, def.work, def.ticks))
}
const VERSION = "2"
const (
// Methods to use when popping items from a queue.
FIFO = iota
FILO
RAND
)
// Simulation models a run of a queue.
type Simulation struct {
queue []*Request
size int
rate int
work func() int
timeout int
method int
counter Result
current *Request
tick int
}
// Result holds the result of simulating a queue run.
type Result struct {
successes int
timeouts int
rejections int
}
// Availability computes the availability of the queue over the run.
func (r Result) Availability() float64 {
return float64(r.successes) / float64(r.successes+r.timeouts+r.rejections)
}
// Config holds the parameters of a queue simulation.
type Config struct {
size int
rate int
work func() int
timeout int
method int
}
// NewSimulation creates a new simulation from a configuration.
func NewSimulation(cfg Config) *Simulation {
switch cfg.method {
case FIFO, FILO, RAND:
default:
panic("Unsupported pop method given")
}
return &Simulation{
queue: make([]*Request, 0, cfg.size),
size: cfg.size,
rate: cfg.rate,
work: cfg.work,
timeout: cfg.timeout,
method: cfg.method,
}
}
// Run runs a queue simulation.
func (s *Simulation) Run(ticks int) Result {
for t := 0; t < ticks; t++ {
s.Tick()
}
return s.counter
}
// Tick advances a simulation by a single tick.
func (s *Simulation) Tick() {
s.tick++
// Work on current request
if s.current != nil {
s.current.Tick()
s.current.Work()
if s.current.Timedout() {
s.counter.timeouts++
s.current = nil
} else if s.current.Done() {
s.counter.successes++
s.current = nil
}
}
// Time out queued requests
nq := make([]*Request, 0, s.size)
for _, r := range s.queue {
r.Tick()
if r.Timedout() {
s.counter.timeouts++
} else {
nq = append(nq, r)
}
}
s.queue = nq
// Accept or reject incoming request
if s.tick%s.rate == 0 {
if len(s.queue) == s.size {
s.counter.rejections++
} else {
s.queue = append(s.queue, NewRequest(s.work(), s.timeout))
}
}
// Pick new request to work on
if s.current == nil && len(s.queue) > 0 {
switch s.method {
case FIFO:
s.current = s.queue[0]
s.queue = s.queue[1:len(s.queue)]
case FILO:
s.current = s.queue[len(s.queue)-1]
s.queue = s.queue[0 : len(s.queue)-1]
case RAND:
k := rand.Intn(len(s.queue))
s.current = s.queue[k]
nq := s.queue[0:k]
nq = append(nq, s.queue[k+1:len(s.queue)]...)
s.queue = nq
default:
panic("Nonsense pop method used")
}
}
}
// Request is an item in the queue.
type Request struct {
work int
budget int
}
// NewRequest creates a new Request.
func NewRequest(work int, timeout int) *Request {
return &Request{work, timeout}
}
// Tick advances a request by a single tick.
func (r *Request) Tick() {
r.budget--
}
// Work acheives one unit of work on a Request.
func (r *Request) Work() {
r.work--
}
// Timedout reports whether a Request has timed out.
func (r Request) Timedout() bool { return r.budget <= 0 }
// Done reports whether a Request is done.
func (r Request) Done() bool { return r.work <= 0 }
// Defaults holds the main program's default parameter values.
type Defaults struct {
rate int
timeout int
work int
size int
method string
ticks int
}
func main() {
defaults := Defaults{
rate: 10,
timeout: 100,
work: 30,
size: 5,
method: "FIFO",
ticks: 100000,
}
flags := struct {
rate *int
timeout *int
work *int
size *int
method *string
ticks *int
raw *bool
help *bool
h *bool
version *bool
}{
flag.Int("rate", defaults.rate, "A new request comes every RATE ticks"),
flag.Int("timeout", defaults.timeout, "Requests have TIMEOUT ticks to complete"),
flag.Int("work", defaults.work, "Requests take WORK ticks to complete"),
flag.Int("size", defaults.size, "Size of queue"),
flag.String("method", defaults.method, "Method to use when popping elements from queue"),
flag.Int("ticks", defaults.ticks, "Number of ticks to run for"),
flag.Bool("raw", false, "Don't pretty-print result"),
flag.Bool("help", false, "Print help and exit"),
flag.Bool("h", false, "Print help and exit"),
flag.Bool("version", false, "Print version and exit"),
}
flag.Parse()
if *flags.h || *flags.help {
fmt.Println(help(defaults))
return
}
if *flags.version {
fmt.Println(VERSION)
return
}
if *flags.rate <= 0 {
panic("Rate must be positive")
}
if *flags.timeout <= 0 {
panic("Timeout must be positive")
}
if *flags.work <= 0 {
panic("Work must be positive")
}
if *flags.size <= 0 {
panic("Size must be positive")
}
method := 0
m := strings.ToLower(*flags.method)
if strings.Contains(m, "fifo") {
method = FIFO
} else if strings.Contains(m, "filo") {
method = FILO
} else if strings.Contains(m, "rand") {
rand.Seed(time.Now().Unix())
method = RAND
} else {
panic("Unknown pop method given")
}
w := distuv.Poisson{
Lambda: 1,
Src: exprand.NewSource(uint64(time.Now().Unix())),
}
work := func() int {
return int(float64(*flags.work) * math.Pow(2, w.Rand()))
}
sim := NewSimulation(Config{
rate: *flags.rate,
timeout: *flags.timeout,
work: work,
size: *flags.size,
method: method,
})
res := sim.Run(*flags.ticks)
if *flags.raw {
fmt.Printf("%f\n", res.Availability())
} else {
fmt.Printf("Availability: %.02f%%\n", 100*res.Availability())
}
}