-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
executable file
·212 lines (189 loc) · 4.25 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
package main
import (
"context"
"encoding/json"
"flag"
"fmt"
"io/ioutil"
"net/http"
"runtime"
"sync"
"time"
"github.com/splnkit/splunk-tracer-go"
"github.com/opentracing/opentracing-go"
)
const (
controlPath = "/control"
resultPath = "/result"
controllerPort = 8088
hecPort = 8088
controllerHost = "localhost"
controllerAccessToken = "ignored"
logsSizeMax = 1 << 20
)
var (
logPayloadStr string
)
func fatal(x ...interface{}) {
panic(fmt.Sprintln(x...))
}
func init() {
lps := make([]byte, logsSizeMax)
for i := 0; i < len(lps); i++ {
lps[i] = 'A' + byte(i%26)
}
logPayloadStr = string(lps)
}
type control struct {
Concurrent int // How many routines, threads, etc.
// How much work to perform under one span
Work int64
// How many repetitions
Repeat int64
// How many amortized nanoseconds to sleep after each span
Sleep time.Duration
// How many nanoseconds to sleep at once
SleepInterval time.Duration
// How many bytes per log statement
BytesPerLog int64
NumLogs int64
// Misc control bits
Trace bool // Trace the operation.
Exit bool // Terminate the test.
Profile bool // Profile this operation
}
type testClient struct {
baseURL string
tracer opentracing.Tracer
}
func work(n int64) int64 {
const primeWork = 982451653
x := int64(primeWork)
for n != 0 {
x *= primeWork
n--
}
return x
}
func (t *testClient) getURL(path string) []byte {
resp, err := http.Get(t.baseURL + path)
if err != nil {
fatal("Bench control request failed: ", err)
}
if resp.StatusCode != 200 {
fatal("Bench control status != 200: ", resp.Status, ": ", path)
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
fatal("Bench error reading body: ", err)
}
return body
}
func (t *testClient) loop() {
for {
body := t.getURL(controlPath)
ctrl := control{}
if err := json.Unmarshal(body, &ctrl); err != nil {
fatal("Bench control parse error: ", err)
}
if ctrl.Exit {
return
}
timing, flusht, sleeps, answer := t.run(&ctrl)
t.getURL(fmt.Sprintf(
"%s?timing=%.9f&flush=%.9f&s=%.9f&a=%d",
resultPath,
timing.Seconds(),
flusht.Seconds(),
sleeps.Seconds(),
answer))
}
}
func testBody(control *control) (time.Duration, int64) {
var sleepDebt time.Duration
var answer int64
var totalSleep time.Duration
for i := int64(0); i < control.Repeat; i++ {
span := opentracing.StartSpan("span/test")
answer = work(control.Work)
for i := int64(0); i < control.NumLogs; i++ {
span.LogEventWithPayload("testlog",
logPayloadStr[0:control.BytesPerLog])
}
span.Finish()
sleepDebt += control.Sleep
if sleepDebt <= control.SleepInterval {
continue
}
begin := time.Now()
time.Sleep(sleepDebt)
elapsed := time.Since(begin)
sleepDebt -= elapsed
totalSleep += elapsed
}
return totalSleep, answer
}
func (t *testClient) run(control *control) (time.Duration, time.Duration, time.Duration, int64) {
if control.Trace {
opentracing.InitGlobalTracer(t.tracer)
} else {
opentracing.InitGlobalTracer(opentracing.NoopTracer{})
}
conc := control.Concurrent
runtime.GOMAXPROCS(conc)
runtime.GC()
runtime.Gosched()
var sleeps time.Duration
var answer int64
beginTest := time.Now()
if conc == 1 {
s, a := testBody(control)
sleeps += s
answer += a
} else {
start := &sync.WaitGroup{}
finish := &sync.WaitGroup{}
start.Add(conc)
finish.Add(conc)
for c := 0; c < conc; c++ {
go func() {
start.Done()
start.Wait()
s, a := testBody(control)
sleeps += s
answer += a
finish.Done()
}()
}
finish.Wait()
}
endTime := time.Now()
flushDur := time.Duration(0)
if control.Trace {
recorder, ok := t.tracer.(splunktracing.Tracer)
if !ok {
panic("Tracer does not have a splunk recorder")
}
recorder.Flush(context.Background())
flushDur = time.Since(endTime)
}
return endTime.Sub(beginTest), flushDur, sleeps, answer
}
func main() {
flag.Parse()
tc := &testClient{
baseURL: fmt.Sprint("http://",
controllerHost, ":",
controllerPort),
tracer: splunktracing.NewTracer(splunktracing.Options{
AccessToken: controllerAccessToken,
Collector: splunktracing.Endpoint{
Host: controllerHost,
Port: hecPort,
Plaintext: true,
},
}),
}
tc.loop()
}