Skip to content

Commit

Permalink
loadtest: add memory metrics
Browse files Browse the repository at this point in the history
  • Loading branch information
GeorgeTsagk committed Jul 19, 2024
1 parent 48fdbe8 commit f14c531
Showing 1 changed file with 89 additions and 0 deletions.
89 changes: 89 additions & 0 deletions itest/loadtest/load_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ package loadtest

import (
"context"
"runtime"
"testing"
"time"

Expand All @@ -20,11 +21,30 @@ var (
},
[]string{"test_name"},
)

memAlloc = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "memory_alloc",
Help: "Memory usage of the test execution, in bytes",
},
[]string{"test_name"},
)

memTotalAlloc = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "memory_total_alloc",
Help: "Total memory allocated by the test execution, " +
"in bytes",
},
[]string{"test_name"},
)
)

func init() {
// Register the metric with Prometheus's default registry.
prometheus.MustRegister(testDuration)
prometheus.MustRegister(memAlloc)
prometheus.MustRegister(memTotalAlloc)
}

type testCase struct {
Expand All @@ -47,6 +67,48 @@ var loadTestCases = []testCase{
},
}

type memResults struct {
Alloc uint64
TotalAlloc uint64
}

// monitorMemory is a goroutine which monitors the memory usage of the test
// execution. It will send the results to the results channel when the quit
// signal is received.
func monitorMemory(interval time.Duration, testName string,
quit chan bool, results chan memResults) {

ticker := time.NewTicker(interval)
defer ticker.Stop()

topAlloc := uint64(0)
topTotalAlloc := uint64(0)

for {
select {
case <-ticker.C:
var m runtime.MemStats
runtime.ReadMemStats(&m)

if m.Alloc > topAlloc {
topAlloc = m.Alloc
}

if m.TotalAlloc > topTotalAlloc {
topTotalAlloc = m.TotalAlloc
}

case <-quit:
results <- memResults{
Alloc: topAlloc,
TotalAlloc: topTotalAlloc,
}

return
}
}
}

// TestPerformance executes the configured performance tests.
func TestPerformance(t *testing.T) {
cfg, err := LoadConfig()
Expand All @@ -69,6 +131,20 @@ func TestPerformance(t *testing.T) {
// Record the start time of the test case.
startTime := time.Now()

// Create a channel to receive the memory usage results.
memResultsChan := make(chan memResults)

// Create a channel to signal the memory monitor to quit.
memQuitChan := make(chan bool)

if cfg.PrometheusGateway.Enabled {
// Start the memory monitor.
go monitorMemory(
1*time.Second, tc.name, memQuitChan,
memResultsChan,
)
}

success := t.Run(tc.name, func(tt *testing.T) {
ctxt, cancel := context.WithTimeout(
ctxt, cfg.TestTimeout,
Expand All @@ -88,6 +164,19 @@ func TestPerformance(t *testing.T) {
// Update the metric with the test duration.
testDuration.WithLabelValues(tc.name).Set(duration)

// Send the quit signal to the memory monitor.
memQuitChan <- true

// Receive the memory usage results.
memResults := <-memResultsChan

// Update the metrics with the memory usage results.
memAlloc.WithLabelValues(tc.name).Set(float64(memResults.Alloc))

memTotalAlloc.WithLabelValues(tc.name).Set(
float64(memResults.TotalAlloc),
)

// Create a new pusher to push the metrics.
pusher := push.New(cfg.PrometheusGateway.PushURL, "load_test").
Collector(testDuration).
Expand Down

0 comments on commit f14c531

Please sign in to comment.