chore: publish IntegraCI build modules 732ec6c83925
This commit is contained in:
1 parent
7a84930dea
commit
5f8a24d014
133 files changed
+18990
-2
No files matched your search
@@ -0,0 +1,279 @@
|
||||
// Package main implements the IntegraCI perf-test Dagger module.
|
||||
//
|
||||
// Runs a performance/load test against a target URL using one of several
|
||||
// engines — Locust, Gatling, or k6 (grafana/k6) — captures the run summary,
|
||||
// and POSTs results to the IntegraCI API. Engine-agnostic: callable from any
|
||||
// CI engine via `dagger call`.
|
||||
//
|
||||
// Usage:
|
||||
//
|
||||
// dagger call run --target-url https://staging.example.com --tool k6 --vus 50 --duration 30s
|
||||
// dagger call run --target-url https://staging.example.com --tool locust
|
||||
// dagger call run --target-url https://staging.example.com --tool gatling --source .
|
||||
// dagger call run --target-url https://staging.example.com --tool none
|
||||
// dagger call run --target-url https://staging.example.com --api-url http://localhost:8000 --token env:TOKEN --service-id $SVC_ID
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"dagger/perf-test/internal/dagger"
|
||||
"fmt"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// PerfTest is the IntegraCI performance/load testing module.
|
||||
type PerfTest struct{}
|
||||
|
||||
// PerfResult contains the output of a performance/load test run.
|
||||
type PerfResult struct {
|
||||
Tool string `json:"tool"`
|
||||
TargetURL string `json:"target_url"`
|
||||
VUs int `json:"vus"`
|
||||
Duration string `json:"duration"`
|
||||
SummaryReport string `json:"summary_report"`
|
||||
Skipped bool `json:"skipped"`
|
||||
Posted bool `json:"posted"`
|
||||
Success bool `json:"success"`
|
||||
}
|
||||
|
||||
// Run executes a performance/load test against targetURL. The tool flag
|
||||
// selects the load-testing engine:
|
||||
//
|
||||
// - "locust" — locustio/locust, headless (-u 50 -r 10 -t 1m --host)
|
||||
// - "gatling" — maven image, ./mvnw gatling:test
|
||||
// - "k6" — grafana/k6, k6 run (default)
|
||||
// - "none" — no-op skip (mirrors governance.perf_test=none)
|
||||
//
|
||||
// When apiURL/token/serviceID are supplied the run summary is POSTed to the
|
||||
// IntegraCI API.
|
||||
func (m *PerfTest) Run(
|
||||
ctx context.Context,
|
||||
// Target URL to load test
|
||||
targetURL string,
|
||||
// Load testing tool to use: locust | gatling | k6 | none
|
||||
// +optional
|
||||
// +default="k6"
|
||||
tool string,
|
||||
// Number of virtual users / concurrent clients
|
||||
// +optional
|
||||
// +default=50
|
||||
vus int,
|
||||
// Test duration / run time (e.g. "1m", "30s")
|
||||
// +optional
|
||||
// +default="1m"
|
||||
duration string,
|
||||
// Optional source directory containing a custom k6 script
|
||||
// (script.js), Locust file (locustfile.py), or a Maven/Gatling
|
||||
// project (pom.xml + src/test). When omitted, k6/locust use a
|
||||
// generated default script targeting targetURL.
|
||||
// +optional
|
||||
source *dagger.Directory,
|
||||
// IntegraCI API URL for posting results
|
||||
// +optional
|
||||
apiURL string,
|
||||
// JWT token for API authentication
|
||||
// +optional
|
||||
token *dagger.Secret,
|
||||
// Service ID to link perf-test results
|
||||
// +optional
|
||||
serviceID string,
|
||||
) (*PerfResult, error) {
|
||||
result := &PerfResult{
|
||||
Tool: strings.ToLower(strings.TrimSpace(tool)),
|
||||
TargetURL: targetURL,
|
||||
VUs: vus,
|
||||
Duration: duration,
|
||||
}
|
||||
if result.Tool == "" {
|
||||
result.Tool = "k6"
|
||||
}
|
||||
|
||||
// "none" is a no-op skip, mirroring the adapter's _comment_skip intent.
|
||||
if result.Tool == "none" {
|
||||
result.Skipped = true
|
||||
result.Success = true
|
||||
return result, nil
|
||||
}
|
||||
|
||||
if targetURL == "" {
|
||||
return nil, fmt.Errorf("targetURL is required")
|
||||
}
|
||||
|
||||
var summary string
|
||||
var err error
|
||||
switch result.Tool {
|
||||
case "k6":
|
||||
summary, err = m.runK6(ctx, targetURL, vus, duration, source)
|
||||
case "locust":
|
||||
summary, err = m.runLocust(ctx, targetURL, vus, duration, source)
|
||||
case "gatling":
|
||||
summary, err = m.runGatling(ctx, targetURL, source)
|
||||
default:
|
||||
return nil, fmt.Errorf("unsupported tool %q: use locust | gatling | k6 | none", tool)
|
||||
}
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("%s run failed: %w", result.Tool, err)
|
||||
}
|
||||
result.SummaryReport = summary
|
||||
|
||||
if apiURL != "" && token != nil && serviceID != "" {
|
||||
if err := m.postResults(ctx, apiURL, token, serviceID, summary); err != nil {
|
||||
return nil, fmt.Errorf("post results failed: %w", err)
|
||||
}
|
||||
result.Posted = true
|
||||
}
|
||||
|
||||
result.Success = true
|
||||
return result, nil
|
||||
}
|
||||
|
||||
// runK6 runs grafana/k6 against targetURL. If source contains a custom
|
||||
// script.js it is used; otherwise a minimal generated script is written
|
||||
// into the container.
|
||||
func (m *PerfTest) runK6(ctx context.Context, targetURL string, vus int, duration string, source *dagger.Directory) (string, error) {
|
||||
script := fmt.Sprintf(`import http from 'k6/http';
|
||||
import { sleep } from 'k6';
|
||||
export const options = { vus: %d, duration: '%s' };
|
||||
export default function () {
|
||||
http.get(__ENV.TARGET_URL);
|
||||
sleep(1);
|
||||
}
|
||||
`, vus, duration)
|
||||
|
||||
k6 := dag.Container().
|
||||
From("grafana/k6:0.55.0").
|
||||
WithEnvVariable("TARGET_URL", targetURL).
|
||||
WithNewFile("/scripts/script.js", script)
|
||||
|
||||
scriptPath := "/scripts/script.js"
|
||||
if source != nil {
|
||||
k6 = k6.WithMountedDirectory("/src", source).WithWorkdir("/src")
|
||||
}
|
||||
|
||||
k6 = k6.WithExec([]string{
|
||||
"run",
|
||||
"--vus", fmt.Sprintf("%d", vus),
|
||||
"--duration", duration,
|
||||
"--summary-export", "/tmp/k6-summary.json",
|
||||
scriptPath,
|
||||
})
|
||||
|
||||
if _, err := k6.Stdout(ctx); err != nil {
|
||||
// k6 exits non-zero when thresholds fail — still try to read summary.
|
||||
summary, rerr := k6.File("/tmp/k6-summary.json").Contents(ctx)
|
||||
if rerr != nil {
|
||||
return "", fmt.Errorf("k6 run failed and no summary produced: %w", err)
|
||||
}
|
||||
return summary, nil
|
||||
}
|
||||
|
||||
summary, err := k6.File("/tmp/k6-summary.json").Contents(ctx)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("read k6 summary: %w", err)
|
||||
}
|
||||
return summary, nil
|
||||
}
|
||||
|
||||
// runLocust runs Locust in headless mode against targetURL, mirroring the
|
||||
// adapter recipe: pip install locust && locust --headless -u 50 -r 10 -t 1m
|
||||
// --host <targetURL>. If source contains a locustfile.py it is used;
|
||||
// otherwise a minimal generated locustfile is written into the container.
|
||||
func (m *PerfTest) runLocust(ctx context.Context, targetURL string, vus int, duration string, source *dagger.Directory) (string, error) {
|
||||
locustfile := `from locust import HttpUser, task, between
|
||||
|
||||
class DefaultUser(HttpUser):
|
||||
wait_time = between(1, 2)
|
||||
|
||||
@task
|
||||
def index(self):
|
||||
self.client.get("/")
|
||||
`
|
||||
|
||||
locust := dag.Container().
|
||||
From("python:3.12-slim").
|
||||
WithExec([]string{"pip", "install", "locust"}).
|
||||
WithNewFile("/mnt/locustfile.py", locustfile).
|
||||
WithWorkdir("/mnt")
|
||||
|
||||
if source != nil {
|
||||
locust = locust.WithMountedDirectory("/src", source).WithWorkdir("/src")
|
||||
}
|
||||
|
||||
locust = locust.WithExec([]string{
|
||||
"locust",
|
||||
"--headless",
|
||||
"-u", fmt.Sprintf("%d", vus),
|
||||
"-r", "10",
|
||||
"-t", duration,
|
||||
"--host", targetURL,
|
||||
"--csv", "/tmp/locust_result",
|
||||
"--only-summary",
|
||||
"--exit-code-on-error", "1",
|
||||
})
|
||||
|
||||
if _, err := locust.Stdout(ctx); err != nil {
|
||||
summary, rerr := locust.File("/tmp/locust_result_stats.csv").Contents(ctx)
|
||||
if rerr != nil {
|
||||
return "", fmt.Errorf("locust run failed and no summary produced: %w", err)
|
||||
}
|
||||
return summary, nil
|
||||
}
|
||||
|
||||
summary, err := locust.File("/tmp/locust_result_stats.csv").Contents(ctx)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("read locust summary: %w", err)
|
||||
}
|
||||
return summary, nil
|
||||
}
|
||||
|
||||
// runGatling runs a Maven/Gatling project against targetURL, mirroring the
|
||||
// adapter recipe: ./mvnw gatling:test. A source directory containing the
|
||||
// Maven project (mvnw wrapper + pom.xml + Gatling simulation) is required;
|
||||
// targetURL is exposed as PERF_TARGET_URL for the simulation to consume.
|
||||
func (m *PerfTest) runGatling(ctx context.Context, targetURL string, source *dagger.Directory) (string, error) {
|
||||
if source == nil {
|
||||
return "", fmt.Errorf("gatling requires --source pointing at a Maven/Gatling project (mvnw + pom.xml)")
|
||||
}
|
||||
|
||||
gatling := dag.Container().
|
||||
From("maven:3.9-eclipse-temurin-21").
|
||||
WithMountedDirectory("/src", source).
|
||||
WithWorkdir("/src").
|
||||
WithEnvVariable("PERF_TARGET_URL", targetURL).
|
||||
WithExec([]string{
|
||||
"./mvnw", "gatling:test",
|
||||
"-Dgatling.simulationClass=LoadSimulation",
|
||||
})
|
||||
|
||||
out, err := gatling.Stdout(ctx)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("gatling run failed: %w", err)
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
// postResults POSTs the run summary to the IntegraCI API. We avoid pulling
|
||||
// in extra dependencies and shell out to curl from a small image, mirroring
|
||||
// the POST-results pattern used by the other IntegraCI Dagger modules. The
|
||||
// token is injected as a secret env var, never interpolated into args.
|
||||
func (m *PerfTest) postResults(ctx context.Context, apiURL string, token *dagger.Secret, serviceID, summary string) error {
|
||||
endpoint := strings.TrimRight(apiURL, "/") + "/api/v1/services/" + serviceID + "/perf-results"
|
||||
|
||||
poster := dag.Container().
|
||||
From("curlimages/curl:8.10.1").
|
||||
WithSecretVariable("API_TOKEN", token).
|
||||
WithNewFile("/tmp/summary.json", summary).
|
||||
WithExec([]string{
|
||||
"sh", "-c",
|
||||
"curl -sS -f -X POST " +
|
||||
"-H \"Authorization: Bearer ${API_TOKEN}\" " +
|
||||
"-H 'Content-Type: application/json' " +
|
||||
"--data-binary @/tmp/summary.json " +
|
||||
"'" + endpoint + "'",
|
||||
})
|
||||
|
||||
if _, err := poster.Stdout(ctx); err != nil {
|
||||
return fmt.Errorf("posting to %s: %w", endpoint, err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
Reference in new issue
Block a user