istio.io/istio@v0.0.0-20240520182934-d79c90f27776/pilot/pkg/config/memory/store_test.go (about)

     1  // Copyright Istio Authors
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package memory
    16  
    17  import (
    18  	"strconv"
    19  	"testing"
    20  
    21  	"istio.io/api/networking/v1alpha3"
    22  	"istio.io/istio/pilot/pkg/model"
    23  	"istio.io/istio/pilot/test/mock"
    24  	"istio.io/istio/pkg/config"
    25  	"istio.io/istio/pkg/config/schema/collections"
    26  )
    27  
    28  func TestStoreInvariant(t *testing.T) {
    29  	store := Make(collections.Mocks)
    30  	mock.CheckMapInvariant(store, t, "some-namespace", 10)
    31  }
    32  
    33  func TestIstioConfig(t *testing.T) {
    34  	store := Make(collections.Pilot)
    35  	mock.CheckIstioConfigTypes(store, "some-namespace", t)
    36  }
    37  
    38  func BenchmarkStoreGet(b *testing.B) {
    39  	s := initStore(b)
    40  	gvk := config.GroupVersionKind{
    41  		Group:   "networking.istio.io",
    42  		Version: "v1alpha3",
    43  		Kind:    "ServiceEntry",
    44  	}
    45  	b.ResetTimer()
    46  	for n := 0; n < b.N; n++ {
    47  		// get one thousand times
    48  		for i := 0; i < 1000; i++ {
    49  			if s.Get(gvk, strconv.Itoa(i), "ns") == nil {
    50  				b.Fatal("get failed")
    51  			}
    52  		}
    53  	}
    54  }
    55  
    56  func BenchmarkStoreList(b *testing.B) {
    57  	s := initStore(b)
    58  	gvk := config.GroupVersionKind{
    59  		Group:   "networking.istio.io",
    60  		Version: "v1alpha3",
    61  		Kind:    "ServiceEntry",
    62  	}
    63  	b.ResetTimer()
    64  	for n := 0; n < b.N; n++ {
    65  		s.List(gvk, "")
    66  	}
    67  }
    68  
    69  func BenchmarkStoreCreate(b *testing.B) {
    70  	for n := 0; n < b.N; n++ {
    71  		initStore(b)
    72  	}
    73  }
    74  
    75  func BenchmarkStoreUpdate(b *testing.B) {
    76  	cfg := config.Config{
    77  		Meta: config.Meta{
    78  			GroupVersionKind: config.GroupVersionKind{
    79  				Group:   "networking.istio.io",
    80  				Version: "v1alpha3",
    81  				Kind:    "ServiceEntry",
    82  			},
    83  			Namespace: "ns",
    84  		},
    85  		Spec: &v1alpha3.ServiceEntry{
    86  			Hosts: []string{"www.foo.com"},
    87  		},
    88  	}
    89  	for n := 0; n < b.N; n++ {
    90  		b.StopTimer()
    91  		s := initStore(b)
    92  		b.StartTimer()
    93  		// update one thousand times
    94  		for i := 0; i < 1000; i++ {
    95  			cfg.Name = strconv.Itoa(i)
    96  			cfg.Spec.(*v1alpha3.ServiceEntry).Hosts[0] = cfg.Name
    97  			if _, err := s.Update(cfg); err != nil {
    98  				b.Fatalf("update failed: %v", err)
    99  			}
   100  		}
   101  	}
   102  }
   103  
   104  func BenchmarkStoreDelete(b *testing.B) {
   105  	gvk := config.GroupVersionKind{
   106  		Group:   "networking.istio.io",
   107  		Version: "v1alpha3",
   108  		Kind:    "ServiceEntry",
   109  	}
   110  	for n := 0; n < b.N; n++ {
   111  		b.StopTimer()
   112  		s := initStore(b)
   113  		b.StartTimer()
   114  		// delete one thousand times
   115  		for i := 0; i < 1000; i++ {
   116  			if err := s.Delete(gvk, strconv.Itoa(i), "ns", nil); err != nil {
   117  				b.Fatalf("delete failed: %v", err)
   118  			}
   119  		}
   120  	}
   121  }
   122  
   123  // make a new store and add 1000 configs to it
   124  func initStore(b *testing.B) model.ConfigStore {
   125  	s := MakeSkipValidation(collections.Pilot)
   126  	cfg := config.Config{
   127  		Meta: config.Meta{
   128  			GroupVersionKind: config.GroupVersionKind{
   129  				Group:   "networking.istio.io",
   130  				Version: "v1alpha3",
   131  				Kind:    "ServiceEntry",
   132  			},
   133  			Namespace: "ns",
   134  		},
   135  		Spec: &v1alpha3.ServiceEntry{
   136  			Hosts: []string{"www.foo.com"},
   137  		},
   138  	}
   139  	for i := 0; i < 1000; i++ {
   140  		cfg.Name = strconv.Itoa(i)
   141  		if _, err := s.Create(cfg); err != nil {
   142  			b.Fatalf("create failed: %v", err)
   143  		}
   144  	}
   145  	return s
   146  }