Skip to content

Commit 2748652

Browse files
Added Instant query to downloaded blocks.
Signed-off-by: Kushal Shukla <[email protected]>
1 parent 6337ce7 commit 2748652

File tree

2 files changed

+59
-16
lines changed

2 files changed

+59
-16
lines changed

prombench/manifests/prombench/benchmark/6_loadgen.yaml

+7-5
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ data:
1010
- name: simple_range
1111
interval: 10s
1212
type: range
13-
start: 4h
13+
start: 2h
1414
end: 1h
1515
step: 15s
1616
queries:
@@ -31,7 +31,7 @@ data:
3131
- name: aggr_range
3232
interval: 30s
3333
type: range
34-
start: 4h
34+
start: 1h
3535
end: 0h
3636
step: 15s
3737
queries:
@@ -129,17 +129,19 @@ spec:
129129
- name: download-key
130130
image: kushalshukla/builder
131131
imagePullPolicy: Always
132-
command: [ "/download-key/key.sh" ]
132+
command: [ "/go/src/github.com/key.sh" ]
133133
env:
134134
- name: PR_NUMBER
135135
value: "{{ .PR_NUMBER }}"
136136
- name: GITHUB_ORG
137137
value: "{{ .GITHUB_ORG }}"
138138
- name: GITHUB_REPO
139-
value: "{{ .GITHUB_REPO }}"
139+
value: "{{ .GITHUB_REPO }}"
140+
- name: STORAGE
141+
value: "/storage-paths"
140142
volumeMounts:
141143
- name: key
142-
mountPath: /config
144+
mountPath: /storage-paths
143145
containers:
144146
- name: prom-load-generator
145147
image: kushalshukla/load-generator

tools/load-generator/main.go

+52-11
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import (
1717
"fmt"
1818
"io"
1919
"log"
20+
"math/rand"
2021
"net/http"
2122
"os"
2223
"sync"
@@ -98,6 +99,12 @@ type KeyConfig struct {
9899
MaxTime int64 `yaml:"maxTime"`
99100
}
100101

102+
type configState struct {
103+
keyConfig *KeyConfig
104+
Err error
105+
StaticValues []int64 // For storing the static absolute time for the downloaded blocks.
106+
}
107+
101108
func NewQuerier(groupID int, target, prNumber string, qg QueryGroup) *Querier {
102109
qtype := qg.Type
103110
if qtype == "" {
@@ -126,6 +133,18 @@ func NewQuerier(groupID int, target, prNumber string, qg QueryGroup) *Querier {
126133
}
127134
}
128135

136+
// This function returns an array of length 5 containing instant values between minTime and maxTime.
137+
// To calculate the values, the formula (maxTime - minTime) / int64(count+1) is used to determine the step size.
138+
// The step size is then added incrementally to minTime to generate the values.
139+
func generateStaticValues(minTime, maxTime int64, count int) []int64 {
140+
step := (maxTime - minTime) / int64(count+1)
141+
values := make([]int64, count)
142+
for i := 0; i < count; i++ {
143+
values[i] = minTime + step*int64(i+1)
144+
}
145+
return values
146+
}
147+
129148
// Function to load `minTime` and `maxTime` from key.yml
130149
func loadKeyConfig() (*KeyConfig, error) {
131150
filePath := "/config/key.yml"
@@ -148,20 +167,33 @@ func loadKeyConfig() (*KeyConfig, error) {
148167
return &keyConfig, nil
149168
}
150169

151-
func (q *Querier) run(wg *sync.WaitGroup) {
170+
func configstate(v *KeyConfig, err error) *configState {
171+
var staticValues []int64
172+
// if there is an error in extracting key.yml file then it means there is no way to query
173+
// data on downloaded blocks. so keep StateValues slices to be empty.
174+
if err == nil {
175+
staticValues = generateStaticValues(v.MinTime, v.MaxTime, 5)
176+
}
177+
return &configState{
178+
keyConfig: v,
179+
Err: err,
180+
StaticValues: staticValues,
181+
}
182+
}
183+
184+
func (q *Querier) run(wg *sync.WaitGroup, timeBound *configState) {
152185
defer wg.Done()
153186
fmt.Printf("Running querier %s %s for %s\n", q.target, q.name, q.url)
154187
time.Sleep(20 * time.Second)
155188

156-
keyConfig, err := loadKeyConfig()
157-
158189
for {
159190
start := time.Now()
160191

161192
for _, query := range q.queries {
162193
q.query(query.Expr, "current", nil)
163-
if err == nil {
164-
q.query(query.Expr, "absolute", keyConfig)
194+
// if there is an error we can avoid to go on absolute block.
195+
if timeBound.Err == nil {
196+
q.query(query.Expr, "absolute", timeBound)
165197
}
166198
}
167199

@@ -172,7 +204,7 @@ func (q *Querier) run(wg *sync.WaitGroup) {
172204
}
173205
}
174206

175-
func (q *Querier) query(expr string, timeMode string, keyConfig *KeyConfig) {
207+
func (q *Querier) query(expr string, timeMode string, timeBound *configState) {
176208
queryCount.WithLabelValues(q.target, q.name, expr, q.qtype).Inc()
177209
start := time.Now()
178210

@@ -187,17 +219,20 @@ func (q *Querier) query(expr string, timeMode string, keyConfig *KeyConfig) {
187219
qParams.Set("query", expr)
188220
if q.qtype == "range" {
189221
if timeMode == "current" {
190-
fmt.Println("range , current blocks")
191222
qParams.Set("start", fmt.Sprintf("%d", int64(time.Now().Add(-q.start).Unix())))
192223
qParams.Set("end", fmt.Sprintf("%d", int64(time.Now().Add(-q.end).Unix())))
193224
qParams.Set("step", q.step)
194225
} else {
195-
fmt.Println("range , absolute blocks")
196-
endTime := time.Unix(0, keyConfig.MaxTime*int64(time.Millisecond))
226+
endTime := time.Unix(0, timeBound.keyConfig.MaxTime*int64(time.Millisecond))
197227
qParams.Set("start", fmt.Sprintf("%d", int64(endTime.Add(-q.start).Unix())))
198228
qParams.Set("end", fmt.Sprintf("%d", int64(endTime.Add(-q.end).Unix())))
199229
qParams.Set("step", q.step)
200230
}
231+
} else if timeMode == "absolute" {
232+
x := timeBound.StaticValues
233+
randomIndex := rand.Intn(len(x)) // calculating random index between StaticValues slice.
234+
instantTime := time.Unix(0, x[randomIndex]*int64(time.Millisecond))
235+
qParams.Set("time", fmt.Sprintf("%d", int64(instantTime.Unix())))
201236
}
202237
req.URL.RawQuery = qParams.Encode()
203238

@@ -263,11 +298,17 @@ func main() {
263298

264299
var wg sync.WaitGroup
265300

301+
keyConfig, err := loadKeyConfig()
302+
if err != nil {
303+
fmt.Printf("key.yml file is not present: %v\n", err)
304+
}
305+
timeBound := configstate(keyConfig, err)
306+
266307
for i, group := range config.Querier.Groups {
267308
wg.Add(1)
268-
go NewQuerier(i, "pr", prNumber, group).run(&wg)
309+
go NewQuerier(i, "pr", prNumber, group).run(&wg, timeBound)
269310
wg.Add(1)
270-
go NewQuerier(i, "release", prNumber, group).run(&wg)
311+
go NewQuerier(i, "release", prNumber, group).run(&wg, timeBound)
271312
}
272313

273314
prometheus.MustRegister(queryDuration, queryCount, queryFailCount)

0 commit comments

Comments
 (0)