Skip to content

Commit

Permalink
feat: add workflow to automate test
Browse files Browse the repository at this point in the history
  • Loading branch information
SalmaElsoly committed Nov 20, 2024
1 parent 25235f8 commit 65ea3f3
Show file tree
Hide file tree
Showing 3 changed files with 121 additions and 0 deletions.
29 changes: 29 additions & 0 deletions .github/workflows/grid-proxy-load.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
name: Proxy load testing

defaults:
run:
working-directory: grid-proxy
on:
schedule:
- cron: 0 21 * * *
workflow_dispatch:

jobs:
load_test:
runs-on: ubuntu-latest

steps:
- name: Checkout code
uses: actions/checkout@v3

- name: Set up Go
uses: actions/setup-go@v4
with:
go-version: 1.21

- name: Run Load Testing
run: |
go mod tidy
cd tests/load_testing
go test -v
82 changes: 82 additions & 0 deletions grid-proxy/tests/load_testing/load.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package load_test

import (
"fmt"
"log"
"net/http"
"os"
"time"

prometheus_integration "github.com/threefoldtech/tfgrid-sdk-go/grid-proxy/tests/load_testing/prometheus"
vegeta "github.com/tsenart/vegeta/v12/lib"
"gopkg.in/yaml.v2"

"github.com/prometheus/client_golang/prometheus"
)

func PerformLoadTesting() error {

data, err := os.ReadFile("test.yml")
if err != nil {
return fmt.Errorf("Failed to read YAML file: %v", err)
}
var loadTest LoadTest
err = yaml.Unmarshal(data, &loadTest)
if err != nil {
return fmt.Errorf("Failed to unmarshal YAML: %v", err)
}

var targets []vegeta.Target
for _, endpoint := range loadTest.Endpoints {
targets = append(targets, vegeta.Target{
Method: "GET",
URL: loadTest.BaseUrl + endpoint,
})
}

attacker := vegeta.NewAttacker()

rate := vegeta.Rate{Freq: loadTest.Freq, Per: time.Second}
duration := loadTest.Duration * time.Second

reg := prometheus.NewRegistry()
pm := prometheus_integration.NewMetrics()

if err := pm.Register(reg); err != nil {
log.Fatal("error registering metrics", err)
}

go func() {
fmt.Println("Prometheus metrics server running on :9090/metrics")
log.Fatal(http.ListenAndServe(":9090", prometheus_integration.NewHandler(reg, time.Now().UTC())))
}()

var results vegeta.Metrics
for res := range attacker.Attack(vegeta.NewStaticTargeter(targets...), rate, duration, "load testing") {
results.Add(res)
pm.Observe(res)
}

results.Close()

successRate := results.Success * 100
errorRate := (1.0 - results.Success) * 100
pm.SuccessRate.Set(successRate)
pm.ErrorRate.Set(errorRate)
pm.AvgLatency.Set(results.Latencies.Mean.Seconds())
pm.MaxLatency.Set(results.Latencies.Max.Seconds())

fmt.Printf("Success Rate: %.2f%%\n", successRate)
fmt.Printf("Error Rate: %.2f%%\n", errorRate)
fmt.Printf("Average Latency: %v\n", results.Latencies.Mean.Seconds())
fmt.Printf("Maximum Latency: %v\n", results.Latencies.Max.Seconds())

return nil
}

type LoadTest struct {
BaseUrl string `yaml:"base_url"`
Endpoints []string `yaml:"endpoints"`
Freq int `yaml:"rate_second"`
Duration time.Duration `yaml:"duration"`
}
10 changes: 10 additions & 0 deletions grid-proxy/tests/load_testing/load_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package load_test

import "testing"

func TestLoad(t *testing.T) {
err := PerformLoadTesting()
if err != nil {
t.Fatalf("Load test failed: %v", err)
}
}

0 comments on commit 65ea3f3

Please # to comment.