forked from open-telemetry/opentelemetry-collector
-
Notifications
You must be signed in to change notification settings - Fork 0
/
load_generator.go
187 lines (149 loc) · 4.29 KB
/
load_generator.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
// Copyright 2019, OpenTelemetry Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package testbed
import (
"encoding/binary"
"fmt"
"log"
"sync"
"sync/atomic"
"time"
"contrib.go.opencensus.io/exporter/jaeger"
"go.opencensus.io/trace"
)
// LoadGenerator is a simple load generator.
type LoadGenerator struct {
exporter *jaeger.Exporter
tracesSent uint64
spansSent uint64
stopOnce sync.Once
stopWait sync.WaitGroup
stopSignal chan struct{}
options LoadOptions
}
// LoadOptions defines the options to use for generating the load.
type LoadOptions struct {
// SpansPerSecond specifies how many spans to generate each second.
SpansPerSecond uint
// SpansPerTrace specifies how many spans per trace to generate. Should be equal or greater than zero.
// The number of traces generated per second will be SpansPerSecond/SpansPerTrace.
SpansPerTrace uint
// Attributes to add to each generated span. Can be empty.
Attributes map[string]interface{}
}
// NewLoadGenerator creates a load generator.
func NewLoadGenerator() (*LoadGenerator, error) {
lg := &LoadGenerator{}
lg.stopSignal = make(chan struct{})
opts := jaeger.Options{
CollectorEndpoint: "http://localhost:14268/api/traces",
Process: jaeger.Process{
ServiceName: "load-generator",
},
}
var err error
lg.exporter, err = jaeger.NewExporter(opts)
if err != nil {
return nil, err
}
return lg, nil
}
// Start the load.
func (lg *LoadGenerator) Start(options LoadOptions) {
lg.options = options
if lg.options.SpansPerTrace == 0 {
// 10 spans per trace by default.
lg.options.SpansPerTrace = 10
}
log.Printf("Starting load generator at %d spans/sec.", lg.options.SpansPerSecond)
// Indicate that generation is in progress.
lg.stopWait.Add(1)
// Begin generation
go lg.generate()
}
// Stop the load.
func (lg *LoadGenerator) Stop() {
lg.stopOnce.Do(func() {
// Signal generate() to stop.
close(lg.stopSignal)
// Wait for it to stop.
lg.stopWait.Wait()
// Print stats.
log.Printf("Stopped generator. %s", lg.GetStats())
})
}
// GetStats returns the stats as a printable string.
func (lg *LoadGenerator) GetStats() string {
return fmt.Sprintf("Sent:%5d spans", atomic.LoadUint64(&lg.spansSent))
}
func (lg *LoadGenerator) SpansSent() uint64 {
return atomic.LoadUint64(&lg.spansSent)
}
func (lg *LoadGenerator) generate() {
// Indicate that generation is done at the end
defer lg.stopWait.Done()
if lg.options.SpansPerSecond <= 0 {
return
}
t := time.NewTicker(time.Second / time.Duration(lg.options.SpansPerSecond/lg.options.SpansPerTrace))
defer t.Stop()
done := false
for !done {
select {
case <-t.C:
lg.generateTrace()
case <-lg.stopSignal:
done = true
}
}
// Send all pending generated spans
lg.exporter.Flush()
}
func (lg *LoadGenerator) generateTrace() {
traceID := atomic.AddUint64(&lg.tracesSent, 1)
for i := uint(0); i < lg.options.SpansPerTrace; i++ {
startTime := time.Now()
spanID := atomic.AddUint64(&lg.spansSent, 1)
// Create a span.
span := &trace.SpanData{
SpanContext: trace.SpanContext{
TraceID: generateTraceID(traceID),
SpanID: generateSpanID(spanID),
},
Name: "load-generator-span",
SpanKind: trace.SpanKindClient,
Attributes: map[string]interface{}{
"load_generator.span_seq_num": int64(spanID),
"load_generator.trace_seq_num": int64(traceID),
},
StartTime: startTime,
EndTime: startTime.Add(time.Duration(time.Millisecond)),
}
// Append attributes.
for k, v := range lg.options.Attributes {
span.Attributes[k] = v
}
lg.exporter.ExportSpan(span)
}
}
func generateTraceID(id uint64) trace.TraceID {
var traceID trace.TraceID
binary.PutUvarint(traceID[:], id)
return traceID
}
func generateSpanID(id uint64) trace.SpanID {
var spanID trace.SpanID
binary.PutUvarint(spanID[:], id)
return spanID
}