add tests

This commit is contained in:
bcmmbaga
2024-07-02 12:57:42 +03:00
parent 42be72a86c
commit 67cc8bd655
5 changed files with 119 additions and 42 deletions

View File

@@ -1,6 +1,7 @@
package server
import (
"github.com/mitchellh/hashstructure/v2"
"github.com/r3labs/diff"
log "github.com/sirupsen/logrus"
)
@@ -54,39 +55,64 @@ func updateAccountPeersWithHash(account *Account) {
// log.Tracef("peer %s doesn't have a channel, skipping network map update", peer.ID)
// continue
//}
//33006042459
// 8700718125
remotePeerNetworkMap := account.GetPeerNetworkMap(peer.ID, "netbird.io", approvedPeersMap)
//log.Println("firewall rules: ", len(remotePeerNetworkMap.FirewallRules))
//hashStr, err := hashstructure.Hash(remotePeerNetworkMap, hashstructure.FormatV2, &hashstructure.HashOptions{
// ZeroNil: true,
// IgnoreZeroValue: true,
// SlicesAsSets: true,
// UseStringer: true,
// //Hasher: xxhash.New(),
//})
//if err != nil {
// log.Errorf("failed to generate network map hash: %v", err)
//} else {
// if peer.NetworkMapHash == hashStr {
// //log.Debugf("not sending network map update to peer: %s as there is nothing new", peer.ID)
// skipUpdate++
// continue
// }
// peer.NetworkMapHash = hashStr
//}
if peer.NetworkMap == nil {
peer.NetworkMap = remotePeerNetworkMap
hashStr, err := hashstructure.Hash(remotePeerNetworkMap, hashstructure.FormatV2, &hashstructure.HashOptions{
ZeroNil: true,
IgnoreZeroValue: true,
SlicesAsSets: true,
UseStringer: true,
//Hasher: xxhash.New(),
})
if err != nil {
log.Errorf("failed to generate network map hash: %v", err)
} else {
changelog, err := diff.Diff(peer.NetworkMap, remotePeerNetworkMap)
if err != nil {
log.Errorf("failed to generate network map diff: %v", err)
} else {
if len(changelog) == 0 {
continue
}
if peer.NetworkMapHash == hashStr {
//log.Debugf("not sending network map update to peer: %s as there is nothing new", peer.ID)
//skipUpdate++
continue
}
peer.NetworkMapHash = hashStr
}
}
}
func updateAccountPeersWithDiff(account *Account) {
//start := time.Now()
//var skipUpdate int
//defer func() {
// duration := time.Since(start)
// log.Printf("Finished execution of updateAccountPeers, took %v\n", duration.Nanoseconds())
// log.Println("not updated peers: ", skipUpdate)
//}()
peers := account.GetPeers()
approvedPeersMap := make(map[string]struct{}, len(peers))
for _, peer := range peers {
approvedPeersMap[peer.ID] = struct{}{}
}
for _, peer := range peers {
//if !am.peersUpdateManager.HasChannel(peer.ID) {
// log.Tracef("peer %s doesn't have a channel, skipping network map update", peer.ID)
// continue
//}
//33006042459
// 8700718125
remotePeerNetworkMap := account.GetPeerNetworkMap(peer.ID, "netbird.io", approvedPeersMap)
peer.NetworkMap = remotePeerNetworkMap
changelog, err := diff.Diff(peer.NetworkMap, remotePeerNetworkMap)
if err != nil {
log.Errorf("failed to generate network map diff: %v", err)
} else {
if len(changelog) == 0 {
continue
}
}
}
}

View File

@@ -159,6 +159,15 @@ func BenchmarkTest_updateAccountPeersWithHash100(b *testing.B) {
}
}
func BenchmarkTest_updateAccountPeersWithDiff100(b *testing.B) {
account := initTestAccount(b, 100)
b.ResetTimer()
for i := 0; i < b.N; i++ {
log.Debug(i)
updateAccountPeersWithDiff(account)
}
}
// 1000 - 6717416375 ns/op
// 500 - 1732888875 ns/op
func BenchmarkTest_updateAccountPeers200(b *testing.B) {
@@ -180,6 +189,15 @@ func BenchmarkTest_updateAccountPeersWithHash200(b *testing.B) {
}
}
func BenchmarkTest_updateAccountPeersWithDiff200(b *testing.B) {
account := initTestAccount(b, 200)
b.ResetTimer()
for i := 0; i < b.N; i++ {
log.Debug(i)
updateAccountPeersWithDiff(account)
}
}
func BenchmarkTest_updateAccountPeers500(b *testing.B) {
account := initTestAccount(b, 500)
b.ResetTimer()
@@ -199,6 +217,15 @@ func BenchmarkTest_updateAccountPeersWithHash500(b *testing.B) {
}
}
func BenchmarkTest_updateAccountPeersWithDiff500(b *testing.B) {
account := initTestAccount(b, 500)
b.ResetTimer()
for i := 0; i < b.N; i++ {
log.Debug(i)
updateAccountPeersWithDiff(account)
}
}
func BenchmarkTest_updateAccountPeers1000(b *testing.B) {
account := initTestAccount(b, 1000)
b.ResetTimer()
@@ -218,6 +245,15 @@ func BenchmarkTest_updateAccountPeersWithHash1000(b *testing.B) {
}
}
func BenchmarkTest_updateAccountPeersWithDiff1000(b *testing.B) {
account := initTestAccount(b, 1000)
b.ResetTimer()
for i := 0; i < b.N; i++ {
log.Debug(i)
updateAccountPeersWithDiff(account)
}
}
func BenchmarkTest_updateAccountPeers1500(b *testing.B) {
account := initTestAccount(b, 1500)
b.ResetTimer()
@@ -237,6 +273,15 @@ func BenchmarkTest_updateAccountPeersWithHash1500(b *testing.B) {
}
}
func BenchmarkTest_updateAccountPeersWithDiff1500(b *testing.B) {
account := initTestAccount(b, 1500)
b.ResetTimer()
for i := 0; i < b.N; i++ {
log.Debug(i)
updateAccountPeersWithDiff(account)
}
}
func BenchmarkTest_updateAccountPeers2000(b *testing.B) {
account := initTestAccount(b, 2000)
b.ResetTimer()
@@ -255,3 +300,12 @@ func BenchmarkTest_updateAccountPeersWithHash2000(b *testing.B) {
updateAccountPeersWithHash(account)
}
}
func BenchmarkTest_updateAccountPeersWithDiff2000(b *testing.B) {
account := initTestAccount(b, 2000)
b.ResetTimer()
for i := 0; i < b.N; i++ {
log.Debug(i)
updateAccountPeersWithDiff(account)
}
}

View File

@@ -18,38 +18,38 @@ type Peer struct {
// WireGuard public key
Key string `gorm:"index"`
// A setup key this peer was registered with
SetupKey string `diff:"-"`
SetupKey string `diff:"-" hash:"ignore"`
// IP address of the Peer
IP net.IP `gorm:"serializer:json"`
// Meta is a Peer system meta data
Meta PeerSystemMeta `gorm:"embedded;embeddedPrefix:meta_" diff:"-"`
Meta PeerSystemMeta `gorm:"embedded;embeddedPrefix:meta_" diff:"-" hash:"ignore"`
// Name is peer's name (machine name)
Name string
// DNSLabel is the parsed peer name for domain resolution. It is used to form an FQDN by appending the account's
// domain to the peer label. e.g. peer-dns-label.netbird.cloud
DNSLabel string
// Status peer's management connection status
Status *PeerStatus `gorm:"embedded;embeddedPrefix:peer_status_" diff:"-"`
Status *PeerStatus `gorm:"embedded;embeddedPrefix:peer_status_" diff:"-" hash:"ignore"`
// The user ID that registered the peer
UserID string `diff:"-"`
UserID string `diff:"-" hash:"ignore"`
// SSHKey is a public SSH key of the peer
SSHKey string
// SSHEnabled indicates whether SSH server is enabled on the peer
SSHEnabled bool
// LoginExpirationEnabled indicates whether peer's login expiration is enabled and once expired the peer has to re-login.
// Works with LastLogin
LoginExpirationEnabled bool `diff:"-"`
LoginExpirationEnabled bool `diff:"-" hash:"ignore"`
// LastLogin the time when peer performed last login operation
LastLogin time.Time `diff:"-"`
LastLogin time.Time `diff:"-" hash:"ignore"`
// CreatedAt records the time the peer was created
CreatedAt time.Time `diff:"-"`
CreatedAt time.Time `diff:"-" hash:"ignore"`
// Indicate ephemeral peer attribute
Ephemeral bool `diff:"-"`
Ephemeral bool `diff:"-" hash:"ignore"`
// Geo location based on connection IP
Location Location `gorm:"embedded;embeddedPrefix:location_" diff:"-"`
Location Location `gorm:"embedded;embeddedPrefix:location_" diff:"-" hash:"ignore"`
NetworkMap any `diff:"-"`
//NetworkMapHash uint64 `hash:"ignore"`
NetworkMap any `diff:"-" hash:"ignore"`
NetworkMapHash uint64 ` diff:"-" hash:"ignore"`
}
type PeerStatus struct { //nolint:revive