Skip to content

Instantly share code, notes, and snippets.

@trozet
Created August 22, 2025 14:16
Show Gist options
  • Select an option

  • Save trozet/da60dee8e170583b92e1ecbd28bdbe7d to your computer and use it in GitHub Desktop.

Select an option

Save trozet/da60dee8e170583b92e1ecbd28bdbe7d to your computer and use it in GitHub Desktop.
benchmark annotation vs label parsing
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