Skip to content

Commit

Permalink
changed query in load-generator such that it will query from download…
Browse files Browse the repository at this point in the history
…ed blocks.

Signed-off-by: Kushal Shukla <kushalshukla110@gmail.com>
  • Loading branch information
kushalShukla-web committed Nov 11, 2024
1 parent 9e660d4 commit fb07a8f
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 9 deletions.
17 changes: 17 additions & 0 deletions prombench/manifests/prombench/benchmark/6_loadgen.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,21 @@ spec:
app: loadgen-scaler
spec:
serviceAccountName: loadgen-scaler
initContainers:
- name: download-key
image: kushalshukla/builder
imagePullPolicy: Always
command: [ "/download-key/key.sh" ]
env:
- name: PR_NUMBER
value: "{{ .PR_NUMBER }}"
- name: GITHUB_ORG
value: "{{ .GITHUB_ORG }}"
- name: GITHUB_REPO
value: "{{ .GITHUB_REPO }}"
volumeMounts:
- name: key
mountPath: /config
containers:
- name: prom-load-generator
image: docker.io/prominfra/scaler:master
Expand All @@ -105,6 +120,8 @@ spec:
- name: webserver-config-volume
configMap:
name: fake-webserver-config-for-scaler
- name: key
emptyDir: {}
nodeSelector:
node-name: nodes-{{ .PR_NUMBER }}
isolation: none
Expand Down
58 changes: 49 additions & 9 deletions tools/load-generator/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,12 @@ type QueryGroup struct {
Step string `yaml:"step,omitempty"`
}

type KeyConfig struct {
Key string `yaml:"key"`
MinTime int64 `yaml:"minTime"`
MaxTime int64 `yaml:"maxTime"`
}

func NewQuerier(groupID int, target, prNumber string, qg QueryGroup) *Querier {
qtype := qg.Type
if qtype == "" {
Expand All @@ -101,9 +107,10 @@ func NewQuerier(groupID int, target, prNumber string, qg QueryGroup) *Querier {
start := durationSeconds(qg.Start)
end := durationSeconds(qg.End)

url := fmt.Sprintf("http://%s/%s/prometheus-%s/api/v1/query", domainName, prNumber, target)
nodePort := 30198
url := fmt.Sprintf("http://%s:%d/%s/prometheus-%s/api/v1/query", domainName, nodePort, prNumber, target)
if qtype == "range" {
url = fmt.Sprintf("http://%s/%s/prometheus-%s/api/v1/query_range", domainName, prNumber, target)
url = fmt.Sprintf("http://%s:%d/%s/prometheus-%s/api/v1/query_range", domainName, nodePort, prNumber, target)
}

return &Querier{
Expand All @@ -120,6 +127,28 @@ func NewQuerier(groupID int, target, prNumber string, qg QueryGroup) *Querier {
}
}

// Function to load `minTime` and `maxTime` from key.yml
func loadKeyConfig() (*KeyConfig, error) {
filePath := "/config/key.yml"
_, err := os.Stat(filePath)
if os.IsNotExist(err) {
return nil, fmt.Errorf("file not found: %s", filePath)
}

data, err := os.ReadFile(filePath)
if err != nil {
return nil, fmt.Errorf("error reading file: %v", err)

Check failure on line 140 in tools/load-generator/main.go

View workflow job for this annotation

GitHub Actions / lint

non-wrapping format verb for fmt.Errorf. Use `%w` to format errors (errorlint)
}

var keyConfig KeyConfig
err = yaml.Unmarshal(data, &keyConfig)
if err != nil {
return nil, fmt.Errorf("error parsing YAML: %v", err)

Check failure on line 146 in tools/load-generator/main.go

View workflow job for this annotation

GitHub Actions / lint

non-wrapping format verb for fmt.Errorf. Use `%w` to format errors (errorlint)
}

return &keyConfig, nil
}

func (q *Querier) run(wg *sync.WaitGroup) {
defer wg.Done()
fmt.Printf("Running querier %s %s for %s\n", q.target, q.name, q.url)
Expand All @@ -129,7 +158,8 @@ func (q *Querier) run(wg *sync.WaitGroup) {
start := time.Now()

for _, query := range q.queries {
q.query(query.Expr)
q.query(query.Expr, "current")
q.query(query.Expr, "absolute")
}

wait := q.interval - time.Since(start)
Expand All @@ -139,23 +169,33 @@ func (q *Querier) run(wg *sync.WaitGroup) {
}
}

func (q *Querier) query(expr string) {
func (q *Querier) query(expr string, timeMode string) {
queryCount.WithLabelValues(q.target, q.name, expr, q.qtype).Inc()
start := time.Now()

req, err := http.NewRequest("GET", q.url, nil)
if err != nil {
log.Printf("Error creating request: %v", err)
queryFailCount.WithLabelValues(q.target, q.name, expr, q.qtype).Inc()
return
}

keyConfig, err := loadKeyConfig()
if err != nil {
timeMode = "current"
}
qParams := req.URL.Query()
qParams.Set("query", expr)
if q.qtype == "range" {
qParams.Set("start", fmt.Sprintf("%d", int64(time.Now().Add(-q.start).Unix())))
qParams.Set("end", fmt.Sprintf("%d", int64(time.Now().Add(-q.end).Unix())))
qParams.Set("step", q.step)
if timeMode == "current" {
qParams.Set("start", fmt.Sprintf("%d", int64(time.Now().Add(-q.start).Unix())))
qParams.Set("end", fmt.Sprintf("%d", int64(time.Now().Add(-q.end).Unix())))
qParams.Set("step", q.step)
} else {
startTime := time.Unix(0, keyConfig.MinTime*int64(time.Millisecond))
endTime := time.Unix(0, keyConfig.MaxTime*int64(time.Millisecond))
qParams.Set("start", fmt.Sprintf("%d", int64(startTime.Add(-q.start).Unix())))
qParams.Set("end", fmt.Sprintf("%d", int64(endTime.Add(-q.end).Unix())))
qParams.Set("step", q.step)
}
}
req.URL.RawQuery = qParams.Encode()

Expand Down

0 comments on commit fb07a8f

Please sign in to comment.