istio.io/istio@v0.0.0-20240520182934-d79c90f27776/pkg/test/framework/components/echo/util/traffic/result.go (about) 1 // Copyright Istio Authors 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package traffic 16 17 import ( 18 "bytes" 19 "fmt" 20 21 "github.com/hashicorp/go-multierror" 22 23 "istio.io/istio/pkg/test" 24 "istio.io/istio/pkg/test/framework/components/echo" 25 ) 26 27 // Result of a traffic generation operation. 28 type Result struct { 29 TotalRequests int 30 SuccessfulRequests int 31 Error error 32 } 33 34 func (r Result) String() string { 35 buf := &bytes.Buffer{} 36 37 _, _ = fmt.Fprintf(buf, "TotalRequests: %d\n", r.TotalRequests) 38 _, _ = fmt.Fprintf(buf, "SuccessfulRequests: %d\n", r.SuccessfulRequests) 39 _, _ = fmt.Fprintf(buf, "PercentSuccess: %f\n", r.PercentSuccess()) 40 _, _ = fmt.Fprintf(buf, "Errors: %v\n", r.Error) 41 42 return buf.String() 43 } 44 45 func (r *Result) add(result echo.CallResult, err error) { 46 count := result.Responses.Len() 47 if count == 0 { 48 count = 1 49 } 50 51 r.TotalRequests += count 52 if err != nil { 53 r.Error = multierror.Append(r.Error, fmt.Errorf("request %d: %v", r.TotalRequests, err)) 54 } else { 55 r.SuccessfulRequests += count 56 } 57 } 58 59 func (r Result) PercentSuccess() float64 { 60 return float64(r.SuccessfulRequests) / float64(r.TotalRequests) 61 } 62 63 // CheckSuccessRate asserts that a minimum success threshold was met. 64 func (r Result) CheckSuccessRate(t test.Failer, minimumPercent float64) { 65 if r.PercentSuccess() < minimumPercent { 66 t.Fatalf("Minimum success threshold, %f, was not met. %d/%d (%f) requests failed: %v", 67 minimumPercent, r.SuccessfulRequests, r.TotalRequests, r.PercentSuccess(), r.Error) 68 } 69 if r.SuccessfulRequests == r.TotalRequests { 70 t.Log("traffic checker succeeded with all successful requests") 71 } else { 72 t.Logf("traffic checker met minimum threshold, with %d/%d successes, but encountered some failures: %v", r.SuccessfulRequests, r.TotalRequests, r.Error) 73 } 74 }