-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathteam.go
61 lines (46 loc) · 1.37 KB
/
team.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
package team
import (
"log"
"os"
"time"
)
type Team struct {
logger *log.Logger
tasks []Task
start time.Time
end time.Time
elapsed time.Duration
average time.Duration
maxParellelJobs int
maxJobsPerSec int
}
const queueSizeMult = 2
func NewTeam(maxParellelJobs int, maxJobsPerSec int) *Team {
team := Team{
logger: log.New(os.Stdout, "Team", log.LstdFlags),
maxParellelJobs: maxParellelJobs,
maxJobsPerSec: maxJobsPerSec,
}
return &team
}
func (team *Team) Run(tasks []Task) {
team.start = time.Now() // record start time
team.tasks = tasks
newJobs := make(chan Job, team.maxParellelJobs*queueSizeMult)
finJobs := make(chan Job, team.maxParellelJobs*queueSizeMult)
aggJobs := make(chan []Job)
team.makeSender(newJobs, team.maxJobsPerSec)
team.makeWorkers(newJobs, finJobs, team.maxParellelJobs)
team.makeReaper(finJobs, aggJobs)
<-aggJobs // wait on job reaper
team.end = time.Now() // record start time
team.elapsed = team.end.Sub(team.start)
team.logger.Printf("Jobs: %v", len(team.tasks))
//team.logger.Printf("Time: %v -> %v", team.start., team.end)
team.logger.Printf("Elapsed: %v", team.elapsed)
team.logger.Printf("Average: %v", team.average)
team.logger.Printf("jobs/s: %v", float64(len(team.tasks))/float64(team.elapsed.Seconds()))
}
func (team *Team) Printf(format string, a ...interface{}) {
//team.logger.Printf(format, a...)
}