diff --git a/server/store/memory/ip_store_test.go b/server/store/memory/ip_store_test.go index 908fa88..552568d 100644 --- a/server/store/memory/ip_store_test.go +++ b/server/store/memory/ip_store_test.go @@ -18,7 +18,8 @@ var ( v4 = net.ParseIP("12.13.14.15") v4s = net.ParseIP("12.13.14.15").To4() - ipStoreTester = store.PrepareIPStoreTester(&ipStoreDriver{}) + ipStoreTester = store.PrepareIPStoreTester(&ipStoreDriver{}) + ipStoreBenchmarker = store.PrepareIPStoreBenchmarker(&ipStoreDriver{}) ) func TestKey(t *testing.T) { @@ -52,3 +53,147 @@ func TestNetworks(t *testing.T) { func TestHasAllHasAnyNetworks(t *testing.T) { ipStoreTester.TestHasAllHasAnyNetworks(t, &store.DriverConfig{}) } + +func BenchmarkIPStore_AddV4(b *testing.B) { + ipStoreBenchmarker.AddV4(b, &store.DriverConfig{}) +} + +func BenchmarkIPStore_AddV6(b *testing.B) { + ipStoreBenchmarker.AddV6(b, &store.DriverConfig{}) +} + +func BenchmarkIPStore_LookupV4(b *testing.B) { + ipStoreBenchmarker.LookupV4(b, &store.DriverConfig{}) +} + +func BenchmarkIPStore_LookupV6(b *testing.B) { + ipStoreBenchmarker.LookupV6(b, &store.DriverConfig{}) +} + +func BenchmarkIPStore_AddRemoveV4(b *testing.B) { + ipStoreBenchmarker.AddRemoveV4(b, &store.DriverConfig{}) +} + +func BenchmarkIPStore_AddRemoveV6(b *testing.B) { + ipStoreBenchmarker.AddRemoveV6(b, &store.DriverConfig{}) +} + +func BenchmarkIPStore_LookupNonExistV4(b *testing.B) { + ipStoreBenchmarker.LookupNonExistV4(b, &store.DriverConfig{}) +} + +func BenchmarkIPStore_LookupNonExistV6(b *testing.B) { + ipStoreBenchmarker.LookupNonExistV6(b, &store.DriverConfig{}) +} + +func BenchmarkIPStore_RemoveNonExistV4(b *testing.B) { + ipStoreBenchmarker.RemoveNonExistV4(b, &store.DriverConfig{}) +} + +func BenchmarkIPStore_RemoveNonExistV6(b *testing.B) { + ipStoreBenchmarker.RemoveNonExistV6(b, &store.DriverConfig{}) +} + +func BenchmarkIPStore_AddV4Network(b *testing.B) { + ipStoreBenchmarker.AddV4Network(b, &store.DriverConfig{}) +} + +func BenchmarkIPStore_AddV6Network(b *testing.B) { + ipStoreBenchmarker.AddV6Network(b, &store.DriverConfig{}) +} + +func BenchmarkIPStore_LookupV4Network(b *testing.B) { + ipStoreBenchmarker.LookupV4Network(b, &store.DriverConfig{}) +} + +func BenchmarkIPStore_LookupV6Network(b *testing.B) { + ipStoreBenchmarker.LookupV6Network(b, &store.DriverConfig{}) +} + +func BenchmarkIPStore_AddRemoveV4Network(b *testing.B) { + ipStoreBenchmarker.AddRemoveV4Network(b, &store.DriverConfig{}) +} + +func BenchmarkIPStore_AddRemoveV6Network(b *testing.B) { + ipStoreBenchmarker.AddRemoveV6Network(b, &store.DriverConfig{}) +} + +func BenchmarkIPStore_RemoveNonExistV4Network(b *testing.B) { + ipStoreBenchmarker.RemoveNonExistV4Network(b, &store.DriverConfig{}) +} + +func BenchmarkIPStore_RemoveNonExistV6Network(b *testing.B) { + ipStoreBenchmarker.RemoveNonExistV6Network(b, &store.DriverConfig{}) +} + +func BenchmarkIPStore_Add1KV4(b *testing.B) { + ipStoreBenchmarker.Add1KV4(b, &store.DriverConfig{}) +} + +func BenchmarkIPStore_Add1KV6(b *testing.B) { + ipStoreBenchmarker.Add1KV6(b, &store.DriverConfig{}) +} + +func BenchmarkIPStore_Lookup1KV4(b *testing.B) { + ipStoreBenchmarker.Lookup1KV4(b, &store.DriverConfig{}) +} + +func BenchmarkIPStore_Lookup1KV6(b *testing.B) { + ipStoreBenchmarker.Lookup1KV6(b, &store.DriverConfig{}) +} + +func BenchmarkIPStore_AddRemove1KV4(b *testing.B) { + ipStoreBenchmarker.AddRemove1KV4(b, &store.DriverConfig{}) +} + +func BenchmarkIPStore_AddRemove1KV6(b *testing.B) { + ipStoreBenchmarker.AddRemove1KV6(b, &store.DriverConfig{}) +} + +func BenchmarkIPStore_LookupNonExist1KV4(b *testing.B) { + ipStoreBenchmarker.LookupNonExist1KV4(b, &store.DriverConfig{}) +} + +func BenchmarkIPStore_LookupNonExist1KV6(b *testing.B) { + ipStoreBenchmarker.LookupNonExist1KV6(b, &store.DriverConfig{}) +} + +func BenchmarkIPStore_RemoveNonExist1KV4(b *testing.B) { + ipStoreBenchmarker.RemoveNonExist1KV4(b, &store.DriverConfig{}) +} + +func BenchmarkIPStore_RemoveNonExist1KV6(b *testing.B) { + ipStoreBenchmarker.RemoveNonExist1KV6(b, &store.DriverConfig{}) +} + +func BenchmarkIPStore_Add1KV4Network(b *testing.B) { + ipStoreBenchmarker.Add1KV4Network(b, &store.DriverConfig{}) +} + +func BenchmarkIPStore_Add1KV6Network(b *testing.B) { + ipStoreBenchmarker.Add1KV6Network(b, &store.DriverConfig{}) +} + +func BenchmarkIPStore_Lookup1KV4Network(b *testing.B) { + ipStoreBenchmarker.Lookup1KV4Network(b, &store.DriverConfig{}) +} + +func BenchmarkIPStore_Lookup1KV6Network(b *testing.B) { + ipStoreBenchmarker.Lookup1KV6Network(b, &store.DriverConfig{}) +} + +func BenchmarkIPStore_AddRemove1KV4Network(b *testing.B) { + ipStoreBenchmarker.AddRemove1KV4Network(b, &store.DriverConfig{}) +} + +func BenchmarkIPStore_AddRemove1KV6Network(b *testing.B) { + ipStoreBenchmarker.AddRemove1KV6Network(b, &store.DriverConfig{}) +} + +func BenchmarkIPStore_RemoveNonExist1KV4Network(b *testing.B) { + ipStoreBenchmarker.RemoveNonExist1KV4Network(b, &store.DriverConfig{}) +} + +func BenchmarkIPStore_RemoveNonExist1KV6Network(b *testing.B) { + ipStoreBenchmarker.RemoveNonExist1KV6Network(b, &store.DriverConfig{}) +} diff --git a/server/store/store_bench.go b/server/store/store_bench.go index ea52ef9..c1b56f3 100644 --- a/server/store/store_bench.go +++ b/server/store/store_bench.go @@ -6,11 +6,10 @@ package store import ( "fmt" + "net" "strings" "testing" - "net" - "github.com/stretchr/testify/require" ) @@ -298,3 +297,483 @@ func (sb stringStoreBench) RemoveNonExist1KLong(b *testing.B, cfg *DriverConfig) return nil }) } + +// IPStoreBenchmarker is a collection of benchmarks for IPStore drivers. +// Every benchmark expects a new, clean storage. Every benchmark should be +// called with a DriverConfig that ensures this. +type IPStoreBenchmarker interface { + AddV4(*testing.B, *DriverConfig) + AddV6(*testing.B, *DriverConfig) + LookupV4(*testing.B, *DriverConfig) + LookupV6(*testing.B, *DriverConfig) + AddRemoveV4(*testing.B, *DriverConfig) + AddRemoveV6(*testing.B, *DriverConfig) + LookupNonExistV4(*testing.B, *DriverConfig) + LookupNonExistV6(*testing.B, *DriverConfig) + RemoveNonExistV4(*testing.B, *DriverConfig) + RemoveNonExistV6(*testing.B, *DriverConfig) + + AddV4Network(*testing.B, *DriverConfig) + AddV6Network(*testing.B, *DriverConfig) + LookupV4Network(*testing.B, *DriverConfig) + LookupV6Network(*testing.B, *DriverConfig) + AddRemoveV4Network(*testing.B, *DriverConfig) + AddRemoveV6Network(*testing.B, *DriverConfig) + RemoveNonExistV4Network(*testing.B, *DriverConfig) + RemoveNonExistV6Network(*testing.B, *DriverConfig) + + Add1KV4(*testing.B, *DriverConfig) + Add1KV6(*testing.B, *DriverConfig) + Lookup1KV4(*testing.B, *DriverConfig) + Lookup1KV6(*testing.B, *DriverConfig) + AddRemove1KV4(*testing.B, *DriverConfig) + AddRemove1KV6(*testing.B, *DriverConfig) + LookupNonExist1KV4(*testing.B, *DriverConfig) + LookupNonExist1KV6(*testing.B, *DriverConfig) + RemoveNonExist1KV4(*testing.B, *DriverConfig) + RemoveNonExist1KV6(*testing.B, *DriverConfig) + + Add1KV4Network(*testing.B, *DriverConfig) + Add1KV6Network(*testing.B, *DriverConfig) + Lookup1KV4Network(*testing.B, *DriverConfig) + Lookup1KV6Network(*testing.B, *DriverConfig) + AddRemove1KV4Network(*testing.B, *DriverConfig) + AddRemove1KV6Network(*testing.B, *DriverConfig) + RemoveNonExist1KV4Network(*testing.B, *DriverConfig) + RemoveNonExist1KV6Network(*testing.B, *DriverConfig) +} + +func generateV4Networks() (a [num1KElements]string) { + b := make([]byte, 2) + for i := range a { + b[0] = byte(i) + b[1] = byte(i >> 8) + a[i] = fmt.Sprintf("64.%d.%d.255/24", b[0], b[1]) + } + + return +} + +func generateV6Networks() (a [num1KElements]string) { + b := make([]byte, 2) + for i := range a { + b[0] = byte(i) + b[1] = byte(i >> 8) + a[i] = fmt.Sprintf("6464:6464:6464:%02x%02x:ffff:ffff:ffff:ffff/64", b[0], b[1]) + } + + return +} + +func generateV4IPs() (a [num1KElements]net.IP) { + b := make([]byte, 2) + for i := range a { + b[0] = byte(i) + b[1] = byte(i >> 8) + a[i] = net.ParseIP(fmt.Sprintf("64.%d.%d.64", b[0], b[1])).To4() + } + + return +} + +func generateV6IPs() (a [num1KElements]net.IP) { + b := make([]byte, 2) + for i := range a { + b[0] = byte(i) + b[1] = byte(i >> 8) + a[i] = net.ParseIP(fmt.Sprintf("6464:6464:6464:%02x%02x:6464:6464:6464:6464", b[0], b[1])) + } + + return +} + +type ipStoreBench struct { + v4IPs [num1KElements]net.IP + v6IPs [num1KElements]net.IP + + v4Networks [num1KElements]string + v6Networks [num1KElements]string + + driver IPStoreDriver +} + +// PrepareIPStoreBenchmarker prepares a reusable suite for StringStore driver +// benchmarks. +func PrepareIPStoreBenchmarker(driver IPStoreDriver) IPStoreBenchmarker { + return ipStoreBench{ + v4IPs: generateV4IPs(), + v6IPs: generateV6IPs(), + v4Networks: generateV4Networks(), + v6Networks: generateV6Networks(), + driver: driver, + } +} + +type ipStoreSetupFunc func(IPStore) error + +func ipStoreSetupNOP(IPStore) error { return nil } + +type ipStoreBenchFunc func(IPStore, int) error + +func (ib ipStoreBench) runBenchmark(b *testing.B, cfg *DriverConfig, setup ipStoreSetupFunc, execute ipStoreBenchFunc) { + is, err := ib.driver.New(cfg) + require.Nil(b, err, "Constructor error must be nil") + require.NotNil(b, is, "IP store must not be nil") + + err = setup(is) + require.Nil(b, err, "Benchmark setup must not fail") + + b.ResetTimer() + for i := 0; i < b.N; i++ { + execute(is, i) + } + b.StopTimer() + + errChan := is.Stop() + err = <-errChan + require.Nil(b, err, "IPStore shutdown must not fail") +} + +func (ib ipStoreBench) AddV4(b *testing.B, cfg *DriverConfig) { + ib.runBenchmark(b, cfg, ipStoreSetupNOP, + func(is IPStore, i int) error { + is.AddIP(ib.v4IPs[0]) + return nil + }) +} + +func (ib ipStoreBench) AddV6(b *testing.B, cfg *DriverConfig) { + ib.runBenchmark(b, cfg, ipStoreSetupNOP, + func(is IPStore, i int) error { + is.AddIP(ib.v6IPs[0]) + return nil + }) +} + +func (ib ipStoreBench) LookupV4(b *testing.B, cfg *DriverConfig) { + ib.runBenchmark(b, cfg, + func(is IPStore) error { + return is.AddIP(ib.v4IPs[0]) + }, + func(is IPStore, i int) error { + is.HasIP(ib.v4IPs[0]) + return nil + }) +} + +func (ib ipStoreBench) LookupV6(b *testing.B, cfg *DriverConfig) { + ib.runBenchmark(b, cfg, + func(is IPStore) error { + return is.AddIP(ib.v6IPs[0]) + }, + func(is IPStore, i int) error { + is.HasIP(ib.v6IPs[0]) + return nil + }) +} + +func (ib ipStoreBench) AddRemoveV4(b *testing.B, cfg *DriverConfig) { + ib.runBenchmark(b, cfg, ipStoreSetupNOP, + func(is IPStore, i int) error { + is.AddIP(ib.v4IPs[0]) + is.RemoveIP(ib.v4IPs[0]) + return nil + }) +} + +func (ib ipStoreBench) AddRemoveV6(b *testing.B, cfg *DriverConfig) { + ib.runBenchmark(b, cfg, ipStoreSetupNOP, + func(is IPStore, i int) error { + is.AddIP(ib.v6IPs[0]) + is.RemoveIP(ib.v6IPs[0]) + return nil + }) +} + +func (ib ipStoreBench) LookupNonExistV4(b *testing.B, cfg *DriverConfig) { + ib.runBenchmark(b, cfg, ipStoreSetupNOP, + func(is IPStore, i int) error { + is.HasIP(ib.v4IPs[0]) + return nil + }) +} + +func (ib ipStoreBench) LookupNonExistV6(b *testing.B, cfg *DriverConfig) { + ib.runBenchmark(b, cfg, ipStoreSetupNOP, + func(is IPStore, i int) error { + is.HasIP(ib.v6IPs[0]) + return nil + }) +} + +func (ib ipStoreBench) RemoveNonExistV4(b *testing.B, cfg *DriverConfig) { + ib.runBenchmark(b, cfg, ipStoreSetupNOP, + func(is IPStore, i int) error { + is.RemoveIP(ib.v4IPs[0]) + return nil + }) +} + +func (ib ipStoreBench) RemoveNonExistV6(b *testing.B, cfg *DriverConfig) { + ib.runBenchmark(b, cfg, ipStoreSetupNOP, + func(is IPStore, i int) error { + is.RemoveIP(ib.v6IPs[0]) + return nil + }) +} + +func (ib ipStoreBench) AddV4Network(b *testing.B, cfg *DriverConfig) { + ib.runBenchmark(b, cfg, ipStoreSetupNOP, + func(is IPStore, i int) error { + is.AddNetwork(ib.v4Networks[0]) + return nil + }) +} + +func (ib ipStoreBench) AddV6Network(b *testing.B, cfg *DriverConfig) { + ib.runBenchmark(b, cfg, ipStoreSetupNOP, + func(is IPStore, i int) error { + is.AddNetwork(ib.v6Networks[0]) + return nil + }) +} + +func (ib ipStoreBench) LookupV4Network(b *testing.B, cfg *DriverConfig) { + ib.runBenchmark(b, cfg, + func(is IPStore) error { + return is.AddNetwork(ib.v4Networks[0]) + }, + func(is IPStore, i int) error { + is.HasIP(ib.v4IPs[0]) + return nil + }) +} + +func (ib ipStoreBench) LookupV6Network(b *testing.B, cfg *DriverConfig) { + ib.runBenchmark(b, cfg, + func(is IPStore) error { + return is.AddNetwork(ib.v6Networks[0]) + }, + func(is IPStore, i int) error { + is.HasIP(ib.v6IPs[0]) + return nil + }) +} + +func (ib ipStoreBench) AddRemoveV4Network(b *testing.B, cfg *DriverConfig) { + ib.runBenchmark(b, cfg, ipStoreSetupNOP, + func(is IPStore, i int) error { + is.AddNetwork(ib.v4Networks[0]) + is.RemoveNetwork(ib.v4Networks[0]) + return nil + }) +} + +func (ib ipStoreBench) AddRemoveV6Network(b *testing.B, cfg *DriverConfig) { + ib.runBenchmark(b, cfg, ipStoreSetupNOP, + func(is IPStore, i int) error { + is.AddNetwork(ib.v6Networks[0]) + is.RemoveNetwork(ib.v6Networks[0]) + return nil + }) +} + +func (ib ipStoreBench) RemoveNonExistV4Network(b *testing.B, cfg *DriverConfig) { + ib.runBenchmark(b, cfg, ipStoreSetupNOP, + func(is IPStore, i int) error { + is.RemoveNetwork(ib.v4Networks[0]) + return nil + }) +} + +func (ib ipStoreBench) RemoveNonExistV6Network(b *testing.B, cfg *DriverConfig) { + ib.runBenchmark(b, cfg, ipStoreSetupNOP, + func(is IPStore, i int) error { + is.RemoveNetwork(ib.v6Networks[0]) + return nil + }) +} + +func (ib ipStoreBench) Add1KV4(b *testing.B, cfg *DriverConfig) { + ib.runBenchmark(b, cfg, ipStoreSetupNOP, + func(is IPStore, i int) error { + is.AddIP(ib.v4IPs[i%num1KElements]) + return nil + }) +} + +func (ib ipStoreBench) Add1KV6(b *testing.B, cfg *DriverConfig) { + ib.runBenchmark(b, cfg, ipStoreSetupNOP, + func(is IPStore, i int) error { + is.AddIP(ib.v6IPs[i%num1KElements]) + return nil + }) +} + +func (ib ipStoreBench) Lookup1KV4(b *testing.B, cfg *DriverConfig) { + ib.runBenchmark(b, cfg, + func(is IPStore) error { + for i := 0; i < num1KElements; i++ { + err := is.AddIP(ib.v4IPs[i%num1KElements]) + if err != nil { + return err + } + } + return nil + }, + func(is IPStore, i int) error { + is.HasIP(ib.v4IPs[i%num1KElements]) + return nil + }) +} + +func (ib ipStoreBench) Lookup1KV6(b *testing.B, cfg *DriverConfig) { + ib.runBenchmark(b, cfg, + func(is IPStore) error { + for i := 0; i < num1KElements; i++ { + err := is.AddIP(ib.v6IPs[i%num1KElements]) + if err != nil { + return err + } + } + return nil + }, + func(is IPStore, i int) error { + is.HasIP(ib.v6IPs[i%num1KElements]) + return nil + }) +} + +func (ib ipStoreBench) AddRemove1KV4(b *testing.B, cfg *DriverConfig) { + ib.runBenchmark(b, cfg, ipStoreSetupNOP, + func(is IPStore, i int) error { + is.AddIP(ib.v4IPs[i%num1KElements]) + is.RemoveIP(ib.v4IPs[i%num1KElements]) + return nil + }) +} + +func (ib ipStoreBench) AddRemove1KV6(b *testing.B, cfg *DriverConfig) { + ib.runBenchmark(b, cfg, ipStoreSetupNOP, + func(is IPStore, i int) error { + is.AddIP(ib.v6IPs[i%num1KElements]) + is.RemoveIP(ib.v6IPs[i%num1KElements]) + return nil + }) +} + +func (ib ipStoreBench) LookupNonExist1KV4(b *testing.B, cfg *DriverConfig) { + ib.runBenchmark(b, cfg, ipStoreSetupNOP, + func(is IPStore, i int) error { + is.HasIP(ib.v4IPs[i%num1KElements]) + return nil + }) +} + +func (ib ipStoreBench) LookupNonExist1KV6(b *testing.B, cfg *DriverConfig) { + ib.runBenchmark(b, cfg, ipStoreSetupNOP, + func(is IPStore, i int) error { + is.HasIP(ib.v6IPs[i%num1KElements]) + return nil + }) +} + +func (ib ipStoreBench) RemoveNonExist1KV4(b *testing.B, cfg *DriverConfig) { + ib.runBenchmark(b, cfg, ipStoreSetupNOP, + func(is IPStore, i int) error { + is.RemoveIP(ib.v4IPs[i%num1KElements]) + return nil + }) +} + +func (ib ipStoreBench) RemoveNonExist1KV6(b *testing.B, cfg *DriverConfig) { + ib.runBenchmark(b, cfg, ipStoreSetupNOP, + func(is IPStore, i int) error { + is.RemoveIP(ib.v6IPs[i%num1KElements]) + return nil + }) +} + +func (ib ipStoreBench) Add1KV4Network(b *testing.B, cfg *DriverConfig) { + ib.runBenchmark(b, cfg, ipStoreSetupNOP, + func(is IPStore, i int) error { + is.AddNetwork(ib.v4Networks[i%num1KElements]) + return nil + }) +} + +func (ib ipStoreBench) Add1KV6Network(b *testing.B, cfg *DriverConfig) { + ib.runBenchmark(b, cfg, ipStoreSetupNOP, + func(is IPStore, i int) error { + is.AddNetwork(ib.v6Networks[i%num1KElements]) + return nil + }) +} + +func (ib ipStoreBench) Lookup1KV4Network(b *testing.B, cfg *DriverConfig) { + ib.runBenchmark(b, cfg, + func(is IPStore) error { + for i := 0; i < num1KElements; i++ { + err := is.AddNetwork(ib.v4Networks[i%num1KElements]) + if err != nil { + return err + } + } + return nil + }, + func(is IPStore, i int) error { + is.HasIP(ib.v4IPs[i%num1KElements]) + return nil + }) +} + +func (ib ipStoreBench) Lookup1KV6Network(b *testing.B, cfg *DriverConfig) { + ib.runBenchmark(b, cfg, + func(is IPStore) error { + for i := 0; i < num1KElements; i++ { + err := is.AddNetwork(ib.v6Networks[i%num1KElements]) + if err != nil { + return err + } + } + return nil + }, + func(is IPStore, i int) error { + is.HasIP(ib.v6IPs[i%num1KElements]) + return nil + }) +} + +func (ib ipStoreBench) AddRemove1KV4Network(b *testing.B, cfg *DriverConfig) { + ib.runBenchmark(b, cfg, ipStoreSetupNOP, + func(is IPStore, i int) error { + is.AddNetwork(ib.v4Networks[i%num1KElements]) + is.RemoveNetwork(ib.v4Networks[i%num1KElements]) + return nil + }) +} + +func (ib ipStoreBench) AddRemove1KV6Network(b *testing.B, cfg *DriverConfig) { + ib.runBenchmark(b, cfg, ipStoreSetupNOP, + func(is IPStore, i int) error { + is.AddNetwork(ib.v6Networks[i%num1KElements]) + is.RemoveNetwork(ib.v6Networks[i%num1KElements]) + return nil + }) +} + +func (ib ipStoreBench) RemoveNonExist1KV4Network(b *testing.B, cfg *DriverConfig) { + ib.runBenchmark(b, cfg, ipStoreSetupNOP, + func(is IPStore, i int) error { + is.RemoveNetwork(ib.v4Networks[i%num1KElements]) + return nil + }) +} + +func (ib ipStoreBench) RemoveNonExist1KV6Network(b *testing.B, cfg *DriverConfig) { + ib.runBenchmark(b, cfg, ipStoreSetupNOP, + func(is IPStore, i int) error { + is.RemoveNetwork(ib.v6Networks[i%num1KElements]) + return nil + }) +}