-
Notifications
You must be signed in to change notification settings - Fork 387
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add performance unit test for Flow Exporter
Signed-off-by: zyiou <[email protected]>
- Loading branch information
Showing
5 changed files
with
296 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,264 @@ | ||
// +build !race | ||
|
||
// Copyright 2021 Antrea Authors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package exporter | ||
|
||
import ( | ||
"crypto/rand" | ||
"flag" | ||
"fmt" | ||
"math/big" | ||
"net" | ||
goruntime "runtime" | ||
"testing" | ||
"time" | ||
|
||
"github.com/golang/mock/gomock" | ||
"k8s.io/klog/v2" | ||
|
||
"antrea.io/antrea/pkg/agent/flowexporter" | ||
"antrea.io/antrea/pkg/agent/flowexporter/connections" | ||
connectionstest "antrea.io/antrea/pkg/agent/flowexporter/connections/testing" | ||
"antrea.io/antrea/pkg/agent/flowexporter/flowrecords" | ||
interfacestoretest "antrea.io/antrea/pkg/agent/interfacestore/testing" | ||
proxytest "antrea.io/antrea/pkg/agent/proxy/testing" | ||
"antrea.io/antrea/pkg/ipfix" | ||
queriertest "antrea.io/antrea/pkg/querier/testing" | ||
) | ||
|
||
const ( | ||
testPollInterval = 5 * time.Second | ||
testConnectionsCount = 30000 | ||
testNewConnectionsCount = 10000 | ||
testDyingConnectionsCount = 5000 | ||
testZoneFilter = uint16(65520) | ||
testDuration = 1 * time.Minute | ||
testIdleTimeOut = 5 * time.Second | ||
testPerfActiveFlowTimeout = 10 * time.Second | ||
testPerfIdleFlowTimeout = 5 * time.Second | ||
testPollCycle = testDuration / testPollInterval | ||
) | ||
|
||
var count = 0 | ||
var exp *flowExporter | ||
|
||
func BenchmarkExport(b *testing.B) { | ||
disableLogToStderr() | ||
ctrl := gomock.NewController(b) | ||
defer ctrl.Finish() | ||
|
||
registry := ipfix.NewIPFIXRegistry() | ||
registry.LoadRegistry() | ||
stopChan1 := make(chan struct{}) | ||
stopChan2 := make(chan struct{}) | ||
listener := startLocalServer(b, stopChan1, stopChan2) | ||
|
||
conns := getConnections(b, testConnectionsCount) | ||
records := flowrecords.NewFlowRecords() | ||
mockIfaceStore := interfacestoretest.NewMockInterfaceStore(ctrl) | ||
mockConnDumper := connectionstest.NewMockConnTrackDumper(ctrl) | ||
mockProxier := proxytest.NewMockProxier(ctrl) | ||
npQuerier := queriertest.NewMockAgentNetworkPolicyInfoQuerier(ctrl) | ||
conntrackStore := connections.NewConntrackConnectionStore(mockConnDumper, records, mockIfaceStore, true, false, mockProxier, npQuerier, testPollInterval) | ||
denyConnStore := connections.NewDenyConnectionStore(mockIfaceStore, mockProxier) | ||
var err error | ||
exp, err = NewFlowExporter(conntrackStore, records, denyConnStore, listener.String(), listener.Network(), testPerfActiveFlowTimeout, testPerfIdleFlowTimeout, true, false, nil, nil, false) | ||
if err != nil { | ||
b.Errorf("error in starting flow exporter: %v", err) | ||
} | ||
// Generate connections for Dumpflows, each round with update connections, new connections and dying connections | ||
mockConnDumper.EXPECT().DumpFlows(testZoneFilter).Return(conns, testConnectionsCount, nil).Times(1) | ||
for i := 0; i < int(testPollCycle); i++ { | ||
conns = getUpdateConnections(b, conns, testNewConnectionsCount, testDyingConnectionsCount, i) | ||
mockConnDumper.EXPECT().DumpFlows(testZoneFilter).Return(conns, len(conns), nil).Times(1) | ||
} | ||
conns = make([]*flowexporter.Connection, 0) | ||
mockConnDumper.EXPECT().DumpFlows(testZoneFilter).Return(conns, len(conns), nil).AnyTimes() | ||
mockConnDumper.EXPECT().GetMaxConnections().Return(testConnectionsCount, nil).AnyTimes() | ||
mockIfaceStore.EXPECT().GetInterfaceByIP(gomock.Any()).Return(nil, false).AnyTimes() | ||
|
||
var maxAlloc uint64 | ||
go statMaxMemAlloc(&maxAlloc, 500*time.Millisecond, stopChan2) | ||
go conntrackStore.Run(stopChan1) | ||
go exp.Run(stopChan2) | ||
<-stopChan2 | ||
b.Logf("\nSummary metrics:\nTOTAL_CONNECTIONS: %d\nINIT_CONNECTIONS: %d\nNEW_CONNECTIONS/poll: %d\nDYING_CONNECTIONS/poll: %d\nPOLL_INTERVAL(s):%d\nMEMORY(M): %d\n", count, testConnectionsCount, testNewConnectionsCount, testDyingConnectionsCount, testPollInterval/1000000000, maxAlloc/1024/1024) | ||
} | ||
|
||
func startLocalServer(b *testing.B, stopChan1, stopChan2 chan struct{}) (address net.Addr) { | ||
udpAddr, err := net.ResolveUDPAddr("udp", "127.0.0.1:0") | ||
if err != nil { | ||
b.Fatalf("Got error when resolving UDP address: %v", err) | ||
} | ||
conn, err := net.ListenUDP("udp", udpAddr) | ||
if err != nil { | ||
b.Fatalf("Got error when creating a local server: %v", err) | ||
} | ||
if err != nil { | ||
b.Fatalf("Got error when creating a local server: %v", err) | ||
} | ||
prevCount := 0 | ||
ticker := time.NewTicker(testIdleTimeOut) | ||
go func() { | ||
defer conn.Close() | ||
for { | ||
buff := make([]byte, 272) | ||
_, _, err := conn.ReadFromUDP(buff) | ||
if err != nil { | ||
b.Error(err) | ||
return | ||
} | ||
count++ | ||
} | ||
}() | ||
go func() { | ||
time.Sleep(testDuration) | ||
close(stopChan1) | ||
for { | ||
select { | ||
case <-ticker.C: | ||
if count > prevCount { | ||
prevCount = count | ||
continue | ||
} else { | ||
exp.process.CloseConnToCollector() | ||
close(stopChan2) | ||
} | ||
} | ||
} | ||
}() | ||
return conn.LocalAddr() | ||
} | ||
|
||
func getConnections(b *testing.B, numberOfConn int) []*flowexporter.Connection { | ||
connections := make([]*flowexporter.Connection, numberOfConn) | ||
for i := 0; i < numberOfConn; i++ { | ||
randomNum := getRandomNum(b, 255) | ||
src := net.ParseIP(fmt.Sprintf("192.168.0.%d", randomNum)) | ||
dst := net.ParseIP(fmt.Sprintf("192.169.0.%d", randomNum)) | ||
flowKey := flowexporter.Tuple{SourceAddress: src, DestinationAddress: dst, Protocol: 6, SourcePort: uint16(i), DestinationPort: uint16(i)} | ||
conn := &flowexporter.Connection{ | ||
StartTime: time.Now().Add(-time.Duration(randomNum) * time.Millisecond), | ||
StopTime: time.Now().Add(-time.Duration(randomNum) * time.Millisecond), | ||
LastExportTime: time.Now().Add(-time.Duration(randomNum) * time.Millisecond), | ||
IsPresent: true, | ||
DoneExport: false, | ||
FlowKey: flowKey, | ||
OriginalPackets: 100, | ||
OriginalBytes: 10, | ||
ReversePackets: 50, | ||
ReverseBytes: 5, | ||
SourcePodNamespace: "ns1", | ||
SourcePodName: "pod1", | ||
DestinationPodNamespace: "ns2", | ||
DestinationPodName: "pod2", | ||
DestinationServicePortName: "service", | ||
DestinationServiceAddress: net.ParseIP("0.0.0.0"), | ||
TCPState: "SYN_SENT", | ||
} | ||
connections[i] = conn | ||
} | ||
return connections | ||
} | ||
|
||
func getUpdateConnections(b *testing.B, conns []*flowexporter.Connection, numOfNewConn int, numOfDyingConn int, index int) []*flowexporter.Connection { | ||
currentTime := time.Now().Add(time.Duration(index) * time.Second) | ||
connections := make([]*flowexporter.Connection, 0) | ||
|
||
// update connections and delete connections in dying state | ||
for i := 0; i < len(conns); i++ { | ||
if conns[i].TCPState == "TIME_WAIT" { | ||
continue | ||
} | ||
conn := *conns[i] | ||
conn.OriginalPackets += 10 | ||
conn.OriginalBytes += 10 | ||
conn.ReversePackets += 5 | ||
conn.ReverseBytes += 5 | ||
randomNum := getRandomNum(b, 1000) | ||
conn.StopTime = currentTime.Add(-time.Duration(randomNum) * time.Millisecond) | ||
connections = append(connections, &conn) | ||
} | ||
|
||
// add new connections | ||
for i := 0; i < numOfNewConn; i++ { | ||
randomNum1 := getRandomNum(b, 255) | ||
randomNum2 := getRandomNum(b, 255) | ||
randomNum3 := getRandomNum(b, 255) | ||
src := net.ParseIP(fmt.Sprintf("192.%d.%d.%d", randomNum1, randomNum2, randomNum3)) | ||
dst := net.ParseIP(fmt.Sprintf("192.%d.%d.%d", randomNum1, randomNum2, randomNum3)) | ||
randomPort := getRandomNum(b, 90000) | ||
flowKey := flowexporter.Tuple{SourceAddress: src, DestinationAddress: dst, Protocol: 6, SourcePort: uint16(randomPort), DestinationPort: uint16(randomPort)} | ||
conn := &flowexporter.Connection{ | ||
StartTime: currentTime.Add(-time.Duration(randomNum1) * time.Millisecond), | ||
StopTime: currentTime.Add(-time.Duration(randomNum1) * time.Millisecond), | ||
LastExportTime: currentTime.Add(-time.Duration(randomNum1) * time.Millisecond), | ||
IsPresent: true, | ||
DoneExport: false, | ||
FlowKey: flowKey, | ||
OriginalPackets: 100, | ||
OriginalBytes: 10, | ||
ReversePackets: 50, | ||
ReverseBytes: 5, | ||
SourcePodNamespace: "ns1", | ||
SourcePodName: "pod1", | ||
DestinationPodNamespace: "ns2", | ||
DestinationPodName: "pod2", | ||
DestinationServicePortName: "service", | ||
DestinationServiceAddress: net.ParseIP("0.0.0.0"), | ||
TCPState: "SYN_SENT", | ||
} | ||
connections = append(connections, conn) | ||
} | ||
|
||
// change connections to dying state, which will be deleted in next round | ||
randomNum := int(getRandomNum(b, int64(len(connections)-numOfDyingConn))) | ||
for i := randomNum; i < numOfDyingConn+randomNum; i++ { | ||
connections[i].TCPState = "TIME_WAIT" | ||
} | ||
return connections | ||
} | ||
|
||
func statMaxMemAlloc(maxAlloc *uint64, interval time.Duration, stopCh chan struct{}) { | ||
var memStats goruntime.MemStats | ||
ticker := time.NewTicker(interval) | ||
defer ticker.Stop() | ||
for { | ||
select { | ||
case <-ticker.C: | ||
goruntime.ReadMemStats(&memStats) | ||
if memStats.Alloc > *maxAlloc { | ||
*maxAlloc = memStats.Alloc | ||
} | ||
case <-stopCh: | ||
return | ||
} | ||
} | ||
} | ||
|
||
func getRandomNum(b *testing.B, value int64) uint64 { | ||
number, err := rand.Int(rand.Reader, big.NewInt(value)) | ||
if err != nil { | ||
b.Errorf("error when generating random number: %v", err) | ||
} | ||
return number.Uint64() | ||
} | ||
|
||
func disableLogToStderr() { | ||
klogFlagSet := flag.NewFlagSet("klog", flag.ContinueOnError) | ||
klog.InitFlags(klogFlagSet) | ||
klogFlagSet.Parse([]string{"-logtostderr=false"}) | ||
} |
Oops, something went wrong.