Skip to content

Commit

Permalink
OTG Tests Update - Corrected loss percentage calculation (#1952)
Browse files Browse the repository at this point in the history
* Corrected lossPct calculation for a number of tests

* Printing flow metrics to 13.1

* 1s delay to traffic stats for Rx packets accuracy on kne

* comment added for the sleep
  • Loading branch information
octpetre authored Aug 19, 2023
1 parent d38beba commit 55b9642
Show file tree
Hide file tree
Showing 16 changed files with 69 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -514,13 +514,13 @@ func verifyTraffic(t *testing.T, ate *ondatra.ATEDevice, c gosnappi.Config, want
for _, f := range c.Flows().Items() {
t.Logf("Verifying flow metrics for flow %s\n", f.Name())
recvMetric := gnmi.Get(t, otg, gnmi.OTG().Flow(f.Name()).State())
txPackets := recvMetric.GetCounters().GetOutPkts()
rxPackets := recvMetric.GetCounters().GetInPkts()
txPackets := float32(recvMetric.GetCounters().GetOutPkts())
rxPackets := float32(recvMetric.GetCounters().GetInPkts())
lostPackets := txPackets - rxPackets
lossPct := lostPackets * 100 / txPackets
if !wantLoss {
if lostPackets > tolerance {
t.Logf("Packets received not matching packets sent. Sent: %v, Received: %d", txPackets, rxPackets)
t.Logf("Packets received not matching packets sent. Sent: %v, Received: %v", txPackets, rxPackets)
}
if lossPct > tolerancePct && txPackets > 0 {
t.Errorf("Traffic Loss Pct for Flow: %s\n got %v, want max %v pct failure", f.Name(), lossPct, tolerancePct)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -453,21 +453,21 @@ func validateTrafficFlows(t *testing.T, ate *ondatra.ATEDevice, good []gosnappi.
otgutils.LogFlowMetrics(t, ate.OTG(), top)

for _, flow := range good {
outPkts := gnmi.Get(t, ate.OTG(), gnmi.OTG().Flow(flow.Name()).Counters().OutPkts().State())
inPkts := gnmi.Get(t, ate.OTG(), gnmi.OTG().Flow(flow.Name()).Counters().InPkts().State())
outPkts := float32(gnmi.Get(t, ate.OTG(), gnmi.OTG().Flow(flow.Name()).Counters().OutPkts().State()))
inPkts := float32(gnmi.Get(t, ate.OTG(), gnmi.OTG().Flow(flow.Name()).Counters().InPkts().State()))
if outPkts == 0 {
t.Fatalf("OutPkts for flow %s is 0, want >0.", flow)
t.Fatalf("OutPkts for flow %s is 0, want > 0", flow)
}
if got := ((outPkts - inPkts) * 100) / outPkts; got > 0 {
t.Fatalf("LossPct for flow %s: got %v, want 0", flow.Name(), got)
}
}

for _, flow := range bad {
outPkts := gnmi.Get(t, ate.OTG(), gnmi.OTG().Flow(flow.Name()).Counters().OutPkts().State())
inPkts := gnmi.Get(t, ate.OTG(), gnmi.OTG().Flow(flow.Name()).Counters().InPkts().State())
outPkts := float32(gnmi.Get(t, ate.OTG(), gnmi.OTG().Flow(flow.Name()).Counters().OutPkts().State()))
inPkts := float32(gnmi.Get(t, ate.OTG(), gnmi.OTG().Flow(flow.Name()).Counters().InPkts().State()))
if outPkts == 0 {
t.Fatalf("OutPkts for flow %s is 0, want >0.", flow)
t.Fatalf("OutPkts for flow %s is 0, want > 0", flow)
}
if got := ((outPkts - inPkts) * 100) / outPkts; got < 100 {
t.Fatalf("LossPct for flow %s: got %v, want 100", flow.Name(), got)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -170,8 +170,12 @@ func stopAndVerifyTraffic(t *testing.T, ate *ondatra.ATEDevice, top gosnappi.Con

time.Sleep(time.Minute)

txPkts := gnmi.Get(t, ate.OTG(), gnmi.OTG().Flow(flowName).Counters().OutPkts().State())
rxPkts := gnmi.Get(t, ate.OTG(), gnmi.OTG().Flow(flowName).Counters().InPkts().State())
txPkts := float32(gnmi.Get(t, ate.OTG(), gnmi.OTG().Flow(flowName).Counters().OutPkts().State()))
rxPkts := float32(gnmi.Get(t, ate.OTG(), gnmi.OTG().Flow(flowName).Counters().InPkts().State()))

if txPkts == 0 {
t.Fatalf("TxPkts == 0, want > 0")
}

if got := (txPkts - rxPkts) * 100 / txPkts; got != 0 {
t.Errorf("FAIL: LossPct for flow named %s got %v, want 0", flowName, got)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -452,6 +452,7 @@ func testTraffic(t *testing.T, args testArgs, flow gosnappi.Flow) {
time.Sleep(2 * time.Minute)
args.ate.OTG().StopTraffic(t)

otgutils.LogFlowMetrics(t, args.ate.OTG(), args.top)
txPkts := float32(gnmi.Get(t, args.ate.OTG(), gnmi.OTG().Flow("Flow").Counters().OutPkts().State()))
rxPkts := float32(gnmi.Get(t, args.ate.OTG(), gnmi.OTG().Flow("Flow").Counters().InPkts().State()))
if txPkts == 0 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -350,12 +350,12 @@ func (s *TestSession) MustATEInterface(t testing.TB, portID string) gosnappi.Dev
}

// GetPacketLoss returns the packet loss for a given flow
func (s *TestSession) GetPacketLoss(t testing.TB, flow gosnappi.Flow) int64 {
func (s *TestSession) GetPacketLoss(t testing.TB, flow gosnappi.Flow) float32 {
t.Helper()
flowMetric := gnmi.Get(t, s.ATE.OTG(), gnmi.OTG().Flow(flow.Name()).State())
txPackets := flowMetric.GetCounters().GetOutPkts()
rxPackets := flowMetric.GetCounters().GetInPkts()
lossPct := int64((txPackets - rxPackets) * 100 / txPackets)
txPackets := float32(flowMetric.GetCounters().GetOutPkts())
rxPackets := float32(flowMetric.GetCounters().GetInPkts())
lossPct := (txPackets - rxPackets) * 100 / txPackets

if txPackets == 0 {
return -1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -302,10 +302,10 @@ func TestP4RTDaemonFailure(t *testing.T) {
ate.OTG().StopTraffic(t)

recvMetric := gnmi.Get(t, ate.OTG(), gnmi.OTG().Flow(flow.Name()).State())
txPackets := recvMetric.GetCounters().GetOutPkts()
rxPackets := recvMetric.GetCounters().GetInPkts()
txPackets := float32(recvMetric.GetCounters().GetOutPkts())
rxPackets := float32(recvMetric.GetCounters().GetInPkts())
lostPackets := txPackets - rxPackets
lossPct := float32(lostPackets * 100 / txPackets)
lossPct := lostPackets * 100 / txPackets

if lossPct > lossTolerance {
t.Errorf("FAIL: LossPct for %s got: %f, want: 0", flow.Name(), lossPct)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -353,14 +353,14 @@ func testTrafficFlows(t *testing.T, args *testArgs, expectPass bool, flows ...go
for _, flow := range flows {
t.Run(flow.Name(), func(t *testing.T) {
t.Logf("*** Verifying %v traffic on OTG ... ", flow.Name())
outPkts := gnmi.Get(t, args.ate.OTG(), gnmi.OTG().Flow(flow.Name()).Counters().OutPkts().State())
inPkts := gnmi.Get(t, args.ate.OTG(), gnmi.OTG().Flow(flow.Name()).Counters().InPkts().State())
outPkts := float32(gnmi.Get(t, args.ate.OTG(), gnmi.OTG().Flow(flow.Name()).Counters().OutPkts().State()))
inPkts := float32(gnmi.Get(t, args.ate.OTG(), gnmi.OTG().Flow(flow.Name()).Counters().InPkts().State()))

if outPkts == 0 {
t.Fatalf("OutPkts == 0, want >0.")
}

lossPct := ((outPkts - inPkts) * 100) / outPkts
lossPct := (outPkts - inPkts) * 100 / outPkts

// log stats
t.Log("Flow LossPct: ", lossPct)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -215,8 +215,8 @@ func testTraffic(t *testing.T, ate *ondatra.ATEDevice, top gosnappi.Config, srcE
otg.StopTraffic(t)

otgutils.LogFlowMetrics(t, otg, top)
txPkts := gnmi.Get(t, otg, gnmi.OTG().Flow("Flow").Counters().OutPkts().State())
rxPkts := gnmi.Get(t, otg, gnmi.OTG().Flow("Flow").Counters().InPkts().State())
txPkts := float32(gnmi.Get(t, otg, gnmi.OTG().Flow("Flow").Counters().OutPkts().State()))
rxPkts := float32(gnmi.Get(t, otg, gnmi.OTG().Flow("Flow").Counters().InPkts().State()))
if got := (txPkts - rxPkts) * 100 / txPkts; got > 0 {
t.Errorf("LossPct for flow %s got %v, want 0", flowipv4.Name(), got)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ func ValidateTraffic(t *testing.T, ate *ondatra.ATEDevice, flow gosnappi.Flow, f

txPkts := gnmi.Get(t, ate.OTG(), gnmi.OTG().Flow(flow.Name()).Counters().OutPkts().State())
rxPkts := gnmi.Get(t, ate.OTG(), gnmi.OTG().Flow(flow.Name()).Counters().InPkts().State())
lossPct := (txPkts - rxPkts) * 100 / txPkts
lossPct := float32(txPkts-rxPkts) * 100 / float32(txPkts)
if int(lossPct) == 0 && flowFilter != "" {
etPath := gnmi.OTG().Flow(flow.Name()).TaggedMetricAny()
ets := gnmi.GetAll(t, ate.OTG(), etPath.State())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -224,8 +224,13 @@ func testTraffic(t *testing.T, ate *ondatra.ATEDevice, config gosnappi.Config, s
otgutils.LogFlowMetrics(t, otg, config)
for _, f := range config.Flows().Items() {
recvMetric := gnmi.Get(t, otg, gnmi.OTG().Flow(f.Name()).State())
lostPackets := recvMetric.GetCounters().GetOutPkts() - recvMetric.GetCounters().GetInPkts()
lossPct := lostPackets * 100 / recvMetric.GetCounters().GetOutPkts()
txPackets := float32(recvMetric.GetCounters().GetOutPkts())
rxPackets := float32(recvMetric.GetCounters().GetInPkts())
lostPackets := txPackets - rxPackets
if txPackets == 0 {
t.Fatalf("TxPkts == 0, want > 0")
}
lossPct := lostPackets * 100 / txPackets
if lossPct > 0 && recvMetric.GetCounters().GetOutPkts() > 0 {
t.Errorf("Loss Pct for %s got %v, want 0", f.Name(), lossPct)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -209,8 +209,11 @@ func testTraffic(
otg.StopTraffic(t)

otgutils.LogFlowMetrics(t, otg, top)
txPkts := gnmi.Get(t, otg, gnmi.OTG().Flow("Flow").Counters().OutPkts().State())
rxPkts := gnmi.Get(t, otg, gnmi.OTG().Flow("Flow").Counters().InPkts().State())
txPkts := float32(gnmi.Get(t, otg, gnmi.OTG().Flow("Flow").Counters().OutPkts().State()))
rxPkts := float32(gnmi.Get(t, otg, gnmi.OTG().Flow("Flow").Counters().InPkts().State()))
if txPkts == 0 {
t.Fatalf("TxPkts == 0, want > 0")
}

if got := (txPkts - rxPkts) * 100 / txPkts; got > 0 {
t.Errorf("LossPct for flow %s got %v, want 0", flowName, got)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -182,8 +182,12 @@ func testTraffic(t *testing.T, ate *ondatra.ATEDevice, top gosnappi.Config, srcE
otgutils.LogFlowMetrics(t, otg, top)

time.Sleep(time.Minute)
txPkts := int(gnmi.Get(t, otg, gnmi.OTG().Flow(flowipv4.Name()).Counters().OutPkts().State()))
rxPkts := int(gnmi.Get(t, otg, gnmi.OTG().Flow(flowipv4.Name()).Counters().InPkts().State()))
txPkts := float32(gnmi.Get(t, otg, gnmi.OTG().Flow(flowipv4.Name()).Counters().OutPkts().State()))
rxPkts := float32(gnmi.Get(t, otg, gnmi.OTG().Flow(flowipv4.Name()).Counters().InPkts().State()))

if txPkts == 0 {
t.Fatalf("TxPkts == 0, want > 0")
}

if !wantLoss {
if got := (txPkts - rxPkts) * 100 / txPkts; got != 0 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -382,15 +382,15 @@ func sendTraffic(t *testing.T, ate *ondatra.ATEDevice, config gosnappi.Config) {
}

// computeLossPct checks for traffic packet loss.
func computeLossPct(t *testing.T, ate *ondatra.ATEDevice, config gosnappi.Config) int64 {
func computeLossPct(t *testing.T, ate *ondatra.ATEDevice, config gosnappi.Config) float32 {
t.Helper()
flowMetric := gnmi.Get(t, ate.OTG(), gnmi.OTG().Flow("Flow").State())
txPackets := flowMetric.GetCounters().GetOutPkts()
txPackets := float32(flowMetric.GetCounters().GetOutPkts())
if txPackets == 0 {
t.Fatal("No tx packets")
}
rxPackets := flowMetric.GetCounters().GetInPkts()
lossPct := int64((txPackets - rxPackets) * 100 / txPackets)
rxPackets := float32(flowMetric.GetCounters().GetInPkts())
lossPct := (txPackets - rxPackets) * 100 / txPackets
return lossPct
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,7 @@ func injectEntry(ctx context.Context, t *testing.T, client *fluent.GRIBIClient,
// testTraffic generates traffic flow from source network to
// destination network via srcEndPoint to dstEndPoint and checks for
// packet loss.
func testTraffic(t *testing.T, ate *ondatra.ATEDevice, top gosnappi.Config) int {
func testTraffic(t *testing.T, ate *ondatra.ATEDevice, top gosnappi.Config) float32 {
// Ensure that traffic can be forwarded between ATE port-1 and ATE port-2.
t.Helper()
otg := ate.OTG()
Expand Down Expand Up @@ -343,8 +343,11 @@ func testTraffic(t *testing.T, ate *ondatra.ATEDevice, top gosnappi.Config) int

otgutils.LogFlowMetrics(t, otg, top)
time.Sleep(time.Minute)
txPkts := int(gnmi.Get(t, otg, gnmi.OTG().Flow("Flow").Counters().OutPkts().State()))
rxPkts := int(gnmi.Get(t, otg, gnmi.OTG().Flow("Flow").Counters().InPkts().State()))
txPkts := float32(gnmi.Get(t, otg, gnmi.OTG().Flow("Flow").Counters().OutPkts().State()))
rxPkts := float32(gnmi.Get(t, otg, gnmi.OTG().Flow("Flow").Counters().InPkts().State()))
if txPkts == 0 {
t.Fatalf("TxPkts == 0, want > 0")
}
lossPct := (txPkts - rxPkts) * 100 / txPkts
return lossPct
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -189,8 +189,11 @@ func TestDefaultAddressFamilies(t *testing.T) {
// Check that we did not lose any packets for the IPv4 and IPv6 flows.
for _, flow := range []string{"ipv4", "ipv6"} {
m := gnmi.Get(t, ate.OTG(), gnmi.OTG().Flow(flow).State())
tx := m.GetCounters().GetOutPkts()
rx := m.GetCounters().GetInPkts()
tx := float32(m.GetCounters().GetOutPkts())
rx := float32(m.GetCounters().GetInPkts())
if tx == 0 {
t.Fatalf("TxPkts == 0, want > 0")
}
loss := tx - rx
lossPct := loss * 100 / tx
if got := lossPct; got > 0 {
Expand Down
7 changes: 7 additions & 0 deletions internal/otgutils/print.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"fmt"
"strings"
"testing"
"time"

"github.com/open-traffic-generator/snappi/gosnappi"
"github.com/openconfig/ondatra/gnmi"
Expand All @@ -30,6 +31,8 @@ import (
// LogFlowMetrics displays the otg flow statistics.
func LogFlowMetrics(t testing.TB, otg *otg.OTG, c gosnappi.Config) {
t.Helper()
// This short delay before printing will ensure that all the packets will be counted during the polling
time.Sleep(1 * time.Second)
var out strings.Builder
out.WriteString("\nOTG Flow Metrics\n")
fmt.Fprintln(&out, strings.Repeat("-", 80))
Expand All @@ -51,6 +54,8 @@ func LogFlowMetrics(t testing.TB, otg *otg.OTG, c gosnappi.Config) {
// LogPortMetrics displays otg port stats.
func LogPortMetrics(t testing.TB, otg *otg.OTG, c gosnappi.Config) {
t.Helper()
// This short delay before printing will ensure that all the packets will be counted during the polling
time.Sleep(1 * time.Second)
var link string
var out strings.Builder
out.WriteString("\nOTG Port Metrics\n")
Expand Down Expand Up @@ -84,6 +89,7 @@ func LogPortMetrics(t testing.TB, otg *otg.OTG, c gosnappi.Config) {
// LogLAGMetrics is displaying otg lag stats.
func LogLAGMetrics(t testing.TB, otg *otg.OTG, c gosnappi.Config) {
t.Helper()
time.Sleep(1 * time.Second)
var out strings.Builder
out.WriteString("\nOTG LAG Metrics\n")
fmt.Fprintln(&out, strings.Repeat("-", 120))
Expand Down Expand Up @@ -148,6 +154,7 @@ func LogLACPMetrics(t testing.TB, otg *otg.OTG, c gosnappi.Config) {
// LogLLDPMetrics is displaying otg lldp stats.
func LogLLDPMetrics(t testing.TB, otg *otg.OTG, c gosnappi.Config) {
t.Helper()
time.Sleep(1 * time.Second)
var out strings.Builder
out.WriteString("\nOTG LLDP Metrics\n")
fmt.Fprintln(&out, strings.Repeat("-", 120))
Expand Down

0 comments on commit 55b9642

Please sign in to comment.