github.com/blend/go-sdk@v1.20220411.3/selector/_bench/main.go (about) 1 /* 2 3 Copyright (c) 2022 - Present. Blend Labs, Inc. All rights reserved 4 Use of this source code is governed by a MIT license that can be found in the LICENSE file. 5 6 */ 7 8 package main 9 10 import ( 11 "fmt" 12 "time" 13 14 k8s "k8s.io/apimachinery/pkg/labels" 15 16 "github.com/blend/go-sdk/selector" 17 ) 18 19 func benchSelector(sel string, labels []map[string]string, binder func(string, map[string]string) (bool, error)) (d time.Duration, err error) { 20 start := time.Now() 21 defer func() { 22 d = time.Since(start) 23 }() 24 var result bool 25 for i := 0; i < 1024; i++ { 26 for _, labelSet := range labels { 27 result, err = binder(sel, labelSet) 28 if err != nil { 29 return 30 } 31 if !result { 32 err = fmt.Errorf("selector failed") 33 return 34 } 35 } 36 } 37 return 38 } 39 40 func kubeRunner(sel string, labels map[string]string) (bool, error) { 41 s, err := k8s.Parse(sel) 42 if err != nil { 43 return false, err 44 } 45 46 return s.Matches(k8s.Set(labels)), nil 47 } 48 49 func blendRunner(sel string, labels map[string]string) (bool, error) { 50 s, err := selector.Parse(sel) 51 if err != nil { 52 return false, err 53 } 54 return s.Matches(labels), nil 55 } 56 57 func main() { 58 sel := "foo==bar,foo!=baz,moo in (foo, bar, baz, buzz),!thing" 59 labels := []map[string]string{ 60 {"foo": "bar", "thing1": "", "moo": "foo"}, 61 {"foo": "bar", "thing1": "", "moo": "bar"}, 62 {"foo": "bar", "thing1": "", "moo": "baz"}, 63 {"foo": "bar", "thing1": "", "moo": "buzz"}, 64 } 65 66 fmt.Println("starting bench") 67 68 fmt.Println("k8s starting") 69 k8s, err := benchSelector(sel, labels, kubeRunner) 70 if err != nil { 71 fmt.Printf("k8s failed: %v\n", err) 72 } else { 73 fmt.Println("k8s complete") 74 fmt.Printf("%v\n", k8s) 75 } 76 fmt.Println("blend starting") 77 blend, err := benchSelector(sel, labels, blendRunner) 78 if err != nil { 79 fmt.Printf("blend failed: %v\n", err) 80 } else { 81 fmt.Println("blend complete") 82 fmt.Printf("%v\n", blend) 83 } 84 }