Created
August 22, 2025 14:16
-
-
Save trozet/da60dee8e170583b92e1ecbd28bdbe7d to your computer and use it in GitHub Desktop.
benchmark annotation vs label parsing
This file contains hidden or 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
| package trustzone | |
| import ( | |
| "encoding/json" | |
| "testing" | |
| metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | |
| "k8s.io/apimachinery/pkg/labels" | |
| ) | |
| // Example annotation we might store on a Node | |
| var annotationData = []byte(`{ | |
| "ovn.k8s.org/trust-zones": { | |
| "ovnkube-net1": "tz1", | |
| "ovnkube-net2": "tz2" | |
| } | |
| }`) | |
| type TrustZoneAnnotation struct { | |
| Zones map[string]string `json:"zones"` | |
| } | |
| // Example labels that a Node might carry | |
| var peerNodeLabels = map[string]string{ | |
| "trustZoneFor": "CoreDNSAccess", | |
| } | |
| func init() { | |
| } | |
| const myTrustZone = "tz1" | |
| func BenchmarkJSONUnmarshal(b *testing.B) { | |
| for i := 0; i < b.N; i++ { | |
| var tz TrustZoneAnnotation | |
| if err := json.Unmarshal(annotationData, &tz); err != nil { | |
| b.Fatal(err) | |
| } | |
| // Check if zone matches on a remote node update | |
| for remotePeer, zone := range tz.Zones { | |
| if myTrustZone == zone { | |
| _ = remotePeer | |
| } | |
| } | |
| } | |
| } | |
| func BenchmarkLabelSelectorTrustZoneCRDSelectorChange(b *testing.B) { | |
| for i := 0; i < b.N; i++ { | |
| sel, err := metav1.LabelSelectorAsSelector(&metav1.LabelSelector{ | |
| MatchLabels: map[string]string{ | |
| "trustZoneFor": "CoreDNSAccess", | |
| }, | |
| }) | |
| if err != nil { | |
| panic(err) | |
| } | |
| ls := labels.Set(peerNodeLabels) | |
| if sel.Matches(ls) { | |
| _ = ls | |
| } | |
| } | |
| } | |
| func BenchmarkLabelSelectorNodeChange(b *testing.B) { | |
| sel, err := metav1.LabelSelectorAsSelector(&metav1.LabelSelector{ | |
| MatchLabels: map[string]string{ | |
| "trustZoneFor": "CoreDNSAccess", | |
| }, | |
| }) | |
| if err != nil { | |
| panic(err) | |
| } | |
| b.ResetTimer() | |
| for i := 0; i < b.N; i++ { | |
| ls := labels.Set(peerNodeLabels) | |
| if sel.Matches(ls) { | |
| _ = ls | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment