|
| 1 | +package riskanalysis |
| 2 | + |
| 3 | +import ( |
| 4 | + "net/http" |
| 5 | + "net/url" |
| 6 | + "os" |
| 7 | + "testing" |
| 8 | + |
| 9 | + "github.com/stretchr/testify/assert" |
| 10 | +) |
| 11 | + |
| 12 | +// Run this test case with the env var set appropriately and sippy's cache disabled to benchmark RA. |
| 13 | +// An uncached call can take many seconds so run go test with e.g. -test.benchtime=10x |
| 14 | +func BenchmarkRequestRiskAnalysis(b *testing.B) { |
| 15 | + const raApiUrl = "api/jobs/runs/risk_analysis" |
| 16 | + sippyUrl := os.Getenv("TEST_RA_SIPPY_URL") |
| 17 | + if sippyUrl == "" { |
| 18 | + // not intended to run in CI, human should set env var to run manually |
| 19 | + b.Skip("set TEST_RA_SIPPY_URL to run this test; e.g. http://localhost:8080/ or https://sippy.dptools.openshift.org/") |
| 20 | + } |
| 21 | + // The JSON here can come from any job run that builds a RA summary in the junit directory. |
| 22 | + // Typically you'll want one with some test failures to exercise the RA code. |
| 23 | + // From the job artifacts, descend to the junit directory which should be under: |
| 24 | + // artifacts/*e2e*/*e2e*/artifacts/junit/ |
| 25 | + // ... and look for the test-failures-summary_*.json files. You can combine them by picking the one with |
| 26 | + // more test failures, and changing TestCount to the one with a higher TestCount (otherwise RA bails early). |
| 27 | + // |
| 28 | + // You can also download the junit directory and simply run the actual command that runs in tests, e.g.: |
| 29 | + // openshift-tests risk-analysis --sippy-url http://localhost:8080/api/jobs/runs/risk_analysis --junit-dir ./junit/ |
| 30 | + // Of course, this creates a lot of chaff about disruption that obscures RA output. And it only runs once. |
| 31 | + const summaryJson = ` |
| 32 | + { |
| 33 | + "ID": 2041023444769837056, |
| 34 | + "ProwJob": { |
| 35 | + "Name": "periodic-ci-openshift-release-main-nightly-4.22-e2e-aws-ovn-single-node-serial" |
| 36 | + }, |
| 37 | + "ClusterData": { |
| 38 | + "Release": "4.22", |
| 39 | + "FromRelease": "", |
| 40 | + "Platform": "aws", |
| 41 | + "Architecture": "amd64", |
| 42 | + "Network": "ovn", |
| 43 | + "Topology": "single", |
| 44 | + "os": { |
| 45 | + "Default": "", |
| 46 | + "ControlPlaneMachineConfigPool": "", |
| 47 | + "WorkerMachineConfigPool": "" |
| 48 | + }, |
| 49 | + "NetworkStack": "IPv4", |
| 50 | + "CloudRegion": "us-east-1", |
| 51 | + "CloudZone": "us-east-1d", |
| 52 | + "ClusterVersionHistory": [ |
| 53 | + "4.22.0-0.nightly-2026-04-06-051707" |
| 54 | + ], |
| 55 | + "MasterNodesUpdated": "Y" |
| 56 | + }, |
| 57 | + "Tests": [ |
| 58 | + { |
| 59 | + "Test": { |
| 60 | + "Name": "[sig-network-edge][OCPFeatureGate:GatewayAPIController][Feature:Router][apigroup:gateway.networking.k8s.io] Ensure gateway loadbalancer service and dnsrecords could be deleted and then get recreated [Serial] [Suite:openshift/conformance/serial]" |
| 61 | + }, |
| 62 | + "Suite": { |
| 63 | + "Name": "openshift-tests" |
| 64 | + }, |
| 65 | + "Status": 12 |
| 66 | + } |
| 67 | + ], |
| 68 | + "TestCount": 2149 |
| 69 | + } |
| 70 | + ` |
| 71 | + |
| 72 | + client := &http.Client{} |
| 73 | + tmp := b.TempDir() |
| 74 | + uri, err := url.JoinPath(sippyUrl, raApiUrl) |
| 75 | + if err != nil { |
| 76 | + b.Fatalf("could not join %q and %q: %v", sippyUrl, raApiUrl, err) |
| 77 | + } |
| 78 | + opt := &Options{SippyURL: uri, JUnitDir: tmp} |
| 79 | + for b.Loop() { |
| 80 | + _, err := opt.requestRiskAnalysis([]byte(summaryJson), client, &mockSleeper{}) |
| 81 | + assert.NoError(b, err) |
| 82 | + } |
| 83 | + b.ReportMetric(b.Elapsed().Seconds()/float64(b.N), "s/op") |
| 84 | +} |
0 commit comments