-
Notifications
You must be signed in to change notification settings - Fork 18
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fanout plugin: Improve except option #17
Merged
haiodo
merged 1 commit into
networkservicemesh:master
from
denis-tingaikin:improve_except
Apr 9, 2020
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
// Copyright (c) 2020 Doc.ai and/or its affiliates. | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at: | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package fanout | ||
|
||
import ( | ||
"strings" | ||
) | ||
|
||
// Domain represents DNS domain name | ||
type Domain interface { | ||
Get(string) Domain | ||
AddString(string) | ||
Add(string, Domain) | ||
Contains(string) bool | ||
IsFinal() bool | ||
Finish() | ||
} | ||
|
||
type domain struct { | ||
children map[string]Domain | ||
end bool | ||
} | ||
|
||
// Finish marks current domain as last in the chain | ||
func (l *domain) Finish() { | ||
l.end = true | ||
} | ||
|
||
// Add adds domain by name | ||
func (l *domain) Add(n string, d Domain) { | ||
l.children[n] = d | ||
} | ||
|
||
// IsFinal returns true if this domain is last in the chain | ||
func (l *domain) IsFinal() bool { | ||
return l.end | ||
} | ||
|
||
// Contains parses string and check is domains contains | ||
func (l *domain) Contains(s string) bool { | ||
end := len(s) | ||
var curr Domain = l | ||
for start := strings.LastIndex(s, "."); start != -1; start = strings.LastIndex(s[:start], ".") { | ||
var k string | ||
if start == end-1 { | ||
k = "." | ||
} else { | ||
k = s[start+1 : end] | ||
} | ||
end = start | ||
curr = curr.Get(k) | ||
if curr == nil { | ||
return false | ||
} | ||
if curr.IsFinal() { | ||
return true | ||
} | ||
} | ||
curr = curr.Get(s[:end]) | ||
if curr == nil { | ||
return false | ||
} | ||
return curr.IsFinal() | ||
} | ||
|
||
// AddString parses string and adds child domains | ||
func (l *domain) AddString(s string) { | ||
end := len(s) | ||
var curr = Domain(l) | ||
for start := strings.LastIndex(s, "."); start != -1; start = strings.LastIndex(s[:start], ".") { | ||
var k string | ||
if start == end-1 { | ||
k = "." | ||
} else { | ||
k = s[start+1 : end] | ||
} | ||
end = start | ||
if v := curr.Get(k); v != nil { | ||
if v.IsFinal() { | ||
return | ||
} | ||
curr = v | ||
} else { | ||
next := &domain{children: map[string]Domain{}} | ||
curr.Add(k, next) | ||
curr = next | ||
} | ||
} | ||
if s != "." { | ||
next := &domain{children: map[string]Domain{}, end: true} | ||
curr.Add(s[:end], next) | ||
} else { | ||
curr.Finish() | ||
} | ||
} | ||
|
||
// Get returns child domain by name | ||
func (l *domain) Get(s string) Domain { | ||
return l.children[s] | ||
} | ||
|
||
// NewDomain creates new domain instance | ||
func NewDomain() Domain { | ||
return &domain{children: map[string]Domain{}} | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,157 @@ | ||
// Copyright (c) 2020 Doc.ai and/or its affiliates. | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at: | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package fanout | ||
|
||
import ( | ||
"crypto/rand" | ||
"math/big" | ||
"strings" | ||
"testing" | ||
"time" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestDomainBasic(t *testing.T) { | ||
samples := []struct { | ||
child string | ||
parent string | ||
expected bool | ||
}{ | ||
{".", ".", true}, | ||
{"example.org.", ".", true}, | ||
{"example.org.", "example.org.", true}, | ||
{"example.org", "example.org", true}, | ||
{"example.org.", "org.", true}, | ||
{"org.", "example.org.", false}, | ||
} | ||
|
||
for i, s := range samples { | ||
l := NewDomain() | ||
l.AddString(s.parent) | ||
require.Equal(t, s.expected, l.Contains(s.child), i) | ||
} | ||
} | ||
|
||
func TestDomainGet(t *testing.T) { | ||
d := NewDomain() | ||
d.AddString("google.com.") | ||
d.AddString("example.com.") | ||
require.True(t, d.Get(".").Get("com").Get("google").IsFinal()) | ||
} | ||
|
||
func TestDomain_ContainsShouldWorkFast(t *testing.T) { | ||
var samples []string | ||
d := NewDomain() | ||
for i := 0; i < 100; i++ { | ||
for j := 0; j < 100; j++ { | ||
samples = append(samples, genSample(i+1)) | ||
d.AddString(samples[len(samples)-1]) | ||
} | ||
} | ||
start := time.Now() | ||
for i := 0; i < 10000; i++ { | ||
require.True(t, d.Contains(samples[i])) | ||
} | ||
require.True(t, time.Since(start) < time.Second/5) | ||
} | ||
|
||
func TestDomainFewEntries(t *testing.T) { | ||
d := NewDomain() | ||
d.AddString("google.com.") | ||
d.AddString("example.com.") | ||
require.True(t, d.Contains("google.com.")) | ||
require.True(t, d.Contains("example.com.")) | ||
require.False(t, d.Contains("com.")) | ||
} | ||
|
||
func TestDomain_DoNotStoreExtraEntries(t *testing.T) { | ||
d := NewDomain() | ||
d.AddString("example.com.") | ||
d.AddString("advanced.example.com.") | ||
require.Nil(t, d.Get(".").Get("com").Get("example").Get("advanced")) | ||
} | ||
|
||
func BenchmarkDomain_ContainsMatch(b *testing.B) { | ||
d := NewDomain() | ||
var samples []string | ||
for i := 0; i < 100; i++ { | ||
for j := 0; j < 100; j++ { | ||
samples = append(samples, genSample(i+1)) | ||
d.AddString(samples[len(samples)-1]) | ||
} | ||
} | ||
b.ResetTimer() | ||
for i := 0; i < b.N; i++ { | ||
for j := 0; j < 10000; j++ { | ||
d.Contains(samples[j]) | ||
} | ||
} | ||
} | ||
|
||
func BenchmarkDomain_AddString(b *testing.B) { | ||
d := NewDomain() | ||
var samples []string | ||
for i := 0; i < 100; i++ { | ||
for j := 0; j < 100; j++ { | ||
samples = append(samples, genSample(i+1)) | ||
} | ||
} | ||
for i := 0; i < b.N; i++ { | ||
for j := 0; j < len(samples); j++ { | ||
d.AddString(samples[j]) | ||
} | ||
} | ||
} | ||
|
||
func BenchmarkDomain_ContainsAny(b *testing.B) { | ||
d := NewDomain() | ||
var samples []string | ||
for i := 0; i < 100; i++ { | ||
for j := 0; j < 100; j++ { | ||
d.AddString(genSample(i + 1)) | ||
samples = append(samples, genSample(i+1)) | ||
} | ||
} | ||
b.ResetTimer() | ||
for i := 0; i < b.N; i++ { | ||
for j := 0; j < len(samples); j++ { | ||
d.Contains(samples[j]) | ||
} | ||
} | ||
} | ||
|
||
func genSample(n int) string { | ||
randInt := func() int { | ||
r, err := rand.Int(rand.Reader, big.NewInt(100)) | ||
if err != nil { | ||
panic(err.Error()) | ||
} | ||
return int(r.Int64()) | ||
} | ||
|
||
var sb strings.Builder | ||
for segment := 0; segment < n; segment++ { | ||
l := randInt()%9 + 1 | ||
for i := 0; i < l; i++ { | ||
v := (randInt() % 26) + 97 | ||
_, _ = sb.WriteRune(rune(v)) | ||
} | ||
_, _ = sb.WriteRune('.') | ||
} | ||
return sb.String() | ||
} |
This file contains 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This test prints
~20 ms
for10 000
entries.And the test prints
~103 s
for the previous realization.NOTE: Max segment count of the domain in the test is
100
.The current complexity of this solution is O(n) where n is the number of segments. This means it should work faster in the real world than in these bench tests.