-
Notifications
You must be signed in to change notification settings - Fork 0
/
sim.go
77 lines (72 loc) · 2.26 KB
/
sim.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
package schedule
import (
"fmt"
"strconv"
)
type SimTask struct {
Identifier int
UserId int
RuntimeMs int
}
func (s *SimTask) Id() string {
return strconv.Itoa(s.Identifier)
}
// Simulate takes a scheduler and a slice of SimTasks, simulates
// the runtime of those tasks as they are removed from the scheduler,
// and prints latency results to standard output.
func Simulate(scheduler Scheduler, tasks []*SimTask) {
for _, t := range tasks {
scheduler.Put(t)
}
currentTimeMs := 0
endtimesPerUser := make(map[int][]int)
taskLatencyPerUser := make(map[int][]int)
runningTasks := map[ScheduledTask]int{}
for scheduler.Size() > 0 || len(runningTasks) > 0 {
if scheduler.Size() > 0 {
for nextTask := scheduler.Next(); nextTask != nil; nextTask = scheduler.Next() {
st := nextTask.Task().(*SimTask)
runningTasks[nextTask] = currentTimeMs + st.RuntimeMs
}
}
if len(runningTasks) > 0 {
// simulate completion of shortest task
earliestCompTimeMs := -1
earliestCompTimeTasks := []ScheduledTask{}
for ta, tm := range runningTasks {
if earliestCompTimeMs == -1 || tm < earliestCompTimeMs {
earliestCompTimeMs = tm
earliestCompTimeTasks = nil
}
if tm == earliestCompTimeMs {
earliestCompTimeTasks = append(earliestCompTimeTasks, ta)
}
}
if len(earliestCompTimeTasks) > 0 {
currentTimeMs += earliestCompTimeTasks[0].Task().(*SimTask).RuntimeMs
for i := range earliestCompTimeTasks {
st := earliestCompTimeTasks[i].Task().(*SimTask)
endtimesPerUser[st.UserId] = append(endtimesPerUser[st.UserId], earliestCompTimeMs)
taskLatencyPerUser[st.UserId] = append(taskLatencyPerUser[st.UserId], currentTimeMs)
earliestCompTimeTasks[i].Close()
delete(runningTasks, earliestCompTimeTasks[i])
}
}
}
}
userIds := []int{}
for k := range endtimesPerUser {
userIds = append(userIds, k)
for i := len(userIds) - 1; i > 0 && userIds[i] < userIds[i-1]; i-- {
temp := userIds[i]
userIds[i] = userIds[i-1]
userIds[i-1] = temp
}
}
for _, id := range userIds {
et := endtimesPerUser[id]
fmt.Printf("\t\tuser %d:\n", id)
fmt.Printf("\t\t\tclock time:\t\t\t %d ms\n", et[len(et)-1])
fmt.Printf("\t\t\tthroughput (tasks / sec):\t %f\n", float32(len(et))/float32(et[len(et)-1])*1000)
}
}