Skip to content

Commit fe19e99

Browse files
author
Arko Dasgupta
committed
benchmark: use custom duration in prom metric
* determine the time duration more accurately instead of using 30s, which may be reading values across tests Signed-off-by: Arko Dasgupta <[email protected]>
1 parent 2119c68 commit fe19e99

File tree

3 files changed

+19
-9
lines changed

3 files changed

+19
-9
lines changed

test/benchmark/suite/report.go

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,12 @@ import (
2424
)
2525

2626
const (
27+
DURATION_FORMATTER = "%DURATION"
2728
controlPlaneContainerMemQL = `process_resident_memory_bytes{namespace="envoy-gateway-system", control_plane="envoy-gateway"}/1024/1024`
2829
controlPlaneProcessMemQL = `go_memstats_heap_inuse_bytes{namespace="envoy-gateway-system", control_plane="envoy-gateway"}/1024/1024`
29-
controlPlaneCPUQL = `rate(process_cpu_seconds_total{namespace="envoy-gateway-system", control_plane="envoy-gateway"}[30s])*100`
30+
controlPlaneCPUQL = `rate(process_cpu_seconds_total{namespace="envoy-gateway-system", control_plane="envoy-gateway"}[%DURATIONs])*100`
3031
dataPlaneMemQL = `container_memory_working_set_bytes{namespace="envoy-gateway-system", container="envoy"}/1024/1024`
31-
dataPlaneCPUQL = `rate(container_cpu_usage_seconds_total{namespace="envoy-gateway-system", container="envoy"}[30s])*100`
32+
dataPlaneCPUQL = `rate(container_cpu_usage_seconds_total{namespace="envoy-gateway-system", container="envoy"}[%DURATIONs])*100`
3233
)
3334

3435
// BenchmarkMetricSample contains sampled metrics and profiles data.
@@ -63,10 +64,10 @@ func NewBenchmarkReport(name, profilesOutputDir string, kubeClient kube.CLIClien
6364
}
6465
}
6566

66-
func (r *BenchmarkReport) Sample(ctx context.Context) (err error) {
67+
func (r *BenchmarkReport) Sample(ctx context.Context, startTime time.Time) (err error) {
6768
sample := BenchmarkMetricSample{}
6869

69-
if mErr := r.sampleMetrics(ctx, &sample); mErr != nil {
70+
if mErr := r.sampleMetrics(ctx, &sample, startTime); mErr != nil {
7071
err = errors.Join(mErr)
7172
}
7273

@@ -122,11 +123,18 @@ func (r *BenchmarkReport) sampleMetrics(ctx context.Context, sample *BenchmarkMe
122123
err = errors.Join(err, fmt.Errorf("failed to query data plane memory: %w", qErr))
123124
}
124125
// Sample cpu
125-
cpCPU, qErr := r.promClient.QuerySum(ctx, controlPlaneCPUQL)
126+
127+
// Get duration
128+
durationSeconds := int(time.Now.Sub(startTime))
129+
cpCPUQL := strings.ReplaceAll(controlPlaneCPUQL, DURATION_FORMATTER, durationSeconds)
130+
131+
cpCPUQL, qErr := r.promClient.QuerySum(ctx, cpCPUQL)
126132
if qErr != nil {
127133
err = errors.Join(err, fmt.Errorf("failed to query control plane cpu: %w", qErr))
128134
}
129-
dpCPU, qErr := r.promClient.QueryAvg(ctx, dataPlaneCPUQL)
135+
136+
dpCPUQL := strings.ReplaceAll(dataPlaneCPUQL, DURATION_FORMATTER, durationSeconds)
137+
dpCPUQL, qErr := r.promClient.QueryAvg(ctx, dpCPUQL)
130138
if qErr != nil {
131139
err = errors.Join(err, fmt.Errorf("failed to query data plane cpu: %w", qErr))
132140
}

test/benchmark/suite/suite.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ func (b *BenchmarkTestSuite) Run(t *testing.T, tests []BenchmarkTest) {
183183
// TODO: currently running benchmark test via nighthawk_client,
184184
// consider switching to gRPC nighthawk-service for benchmark test.
185185
// ref: https://github.com/envoyproxy/nighthawk/blob/main/api/client/service.proto
186-
func (b *BenchmarkTestSuite) Benchmark(t *testing.T, ctx context.Context, jobName, resultTitle, gatewayHostPort, hostnamePattern string, host int) (*BenchmarkReport, error) {
186+
func (b *BenchmarkTestSuite) Benchmark(t *testing.T, ctx context.Context, jobName, resultTitle, gatewayHostPort, hostnamePattern string, host int, startTime time.Time) (*BenchmarkReport, error) {
187187
t.Logf("Running benchmark test: %s", resultTitle)
188188

189189
requestHeaders := make([]string, 0, host)
@@ -230,7 +230,7 @@ func (b *BenchmarkTestSuite) Benchmark(t *testing.T, ctx context.Context, jobNam
230230

231231
// Sample the metrics and profiles at runtime.
232232
// Do not consider it as an error, fail sampling should not affect test running.
233-
if err := report.Sample(ctx); err != nil {
233+
if err := report.Sample(ctx, startTime); err != nil {
234234
t.Logf("Error occurs while sampling metrics or profiles: %v", err)
235235
}
236236

test/benchmark/tests/scale_httproutes.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"context"
1212
"fmt"
1313
"testing"
14+
"time"
1415

1516
"github.com/stretchr/testify/require"
1617
"k8s.io/apimachinery/pkg/types"
@@ -56,6 +57,7 @@ var ScaleHTTPRoutes = suite.BenchmarkTest{
5657
testName := fmt.Sprintf("scaling up httproutes to %d with %d routes per hostname", scale, routePerHost)
5758

5859
t.Run(testName, func(t *testing.T) {
60+
startTime := time.Now()
5961
err = bSuite.ScaleUpHTTPRoutes(ctx, [2]uint16{start, scale}, routeNameFormat, routeHostnameFormat, gatewayNN.Name, routePerHost-batch, func(route *gwapiv1.HTTPRoute) {
6062
routeNN := types.NamespacedName{Name: route.Name, Namespace: route.Namespace}
6163
routeNNs = append(routeNNs, routeNN)
@@ -71,7 +73,7 @@ var ScaleHTTPRoutes = suite.BenchmarkTest{
7173

7274
// Run benchmark test at different scale.
7375
jobName := fmt.Sprintf("scale-up-httproutes-%d", scale)
74-
report, err := bSuite.Benchmark(t, ctx, jobName, testName, gatewayAddr, routeHostnameFormat, int(totalHosts))
76+
report, err := bSuite.Benchmark(t, ctx, jobName, testName, gatewayAddr, routeHostnameFormat, int(totalHosts), startTime)
7577
require.NoError(t, err)
7678

7779
reports = append(reports, report)

0 commit comments

Comments
 (0)