-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.go
171 lines (157 loc) · 3.46 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
// Copyright 2016-2017 Diffeo, Inc.
// This software is released under an MIT/X11 open source license.
// Package coordbench provides a load-generation tool for Coordinate.
package main
import (
"github.com/diffeo/go-coordinate/backend"
"github.com/diffeo/go-coordinate/coordinate"
"github.com/satori/go.uuid"
"github.com/urfave/cli"
"runtime"
"sync"
"time"
)
type benchWork struct {
Coordinate coordinate.Coordinate
Namespace coordinate.Namespace
WorkSpec coordinate.WorkSpec
Concurrency int
}
func (bench *benchWork) Run(runner func()) {
wg := sync.WaitGroup{}
wg.Add(bench.Concurrency)
for i := 0; i < bench.Concurrency; i++ {
go func() {
defer wg.Done()
runner()
}()
}
wg.Wait()
}
var bench benchWork
var addUnits = cli.Command{
Name: "add",
Usage: "create many work units",
Flags: []cli.Flag{
cli.IntFlag{
Name: "count",
Value: 100,
Usage: "number of work units to create",
},
},
Action: func(c *cli.Context) {
count := c.Int("count")
numbers := make(chan int)
go func() {
for i := 1; i <= count; i++ {
numbers <- i
}
close(numbers)
}()
bench.Run(func() {
for <-numbers != 0 {
name := uuid.NewV4().String()
bench.WorkSpec.AddWorkUnit(name, map[string]interface{}{}, coordinate.WorkUnitMeta{})
}
})
},
}
var doWork = cli.Command{
Name: "do",
Usage: "do work as long as there is more",
Flags: []cli.Flag{
cli.IntFlag{
Name: "batch",
Value: 100,
Usage: "request this many attempts in one batch",
},
cli.DurationFlag{
Name: "delay",
Value: 0,
Usage: "wait this long per work unit before completion",
},
},
Action: func(c *cli.Context) {
batch := c.Int("batch")
delay := c.Duration("delay")
name := uuid.NewV4().String()
parent, err := bench.Namespace.Worker(name)
if err != nil {
return
}
bench.Run(func() {
name := uuid.NewV4().String()
worker, err := bench.Namespace.Worker(name)
if err != nil {
return
}
err = worker.SetParent(parent)
if err != nil {
return
}
for {
attempts, err := worker.RequestAttempts(coordinate.AttemptRequest{NumberOfWorkUnits: batch})
if err != nil || len(attempts) == 0 {
break
}
for _, attempt := range attempts {
time.Sleep(delay)
_ = attempt.Finish(nil)
}
}
_ = worker.Deactivate()
})
},
}
var clear = cli.Command{
Name: "clear",
Usage: "delete all of the work units",
Action: func(c *cli.Context) {
bench.WorkSpec.DeleteWorkUnits(coordinate.WorkUnitQuery{})
},
}
func main() {
backend := backend.Backend{Implementation: "memory"}
app := cli.NewApp()
app.Usage = "benchmark the Coordinate job queue system"
app.Flags = []cli.Flag{
cli.GenericFlag{
Name: "backend",
Value: &backend,
Usage: "impl:[address] of Coordinate backend",
},
cli.StringFlag{
Name: "namespace",
Usage: "Coordinate namespace name",
},
cli.IntFlag{
Name: "concurrency",
Value: runtime.NumCPU(),
Usage: "run this many jobs in parallel",
},
}
app.Commands = []cli.Command{
addUnits,
doWork,
clear,
}
app.Before = func(c *cli.Context) (err error) {
bench.Coordinate, err = backend.Coordinate()
if err != nil {
return
}
bench.Namespace, err = bench.Coordinate.Namespace(c.String("namespace"))
if err != nil {
return
}
bench.WorkSpec, err = bench.Namespace.SetWorkSpec(map[string]interface{}{
"name": "spec",
})
if err != nil {
return
}
bench.Concurrency = c.Int("concurrency")
return
}
app.RunAndExitOnError()
}