-
Notifications
You must be signed in to change notification settings - Fork 879
[WIP] NetworkDB performance improvements #2046
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
Open
fcrisciani
wants to merge
1
commit into
moby:master
Choose a base branch
from
fcrisciani:performance
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
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 hidden or 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,115 @@ | ||
package networkdb | ||
|
||
import ( | ||
"strconv" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestAddNetworkNode(t *testing.T) { | ||
n := &NetworkDB{config: &Config{NodeID: "node-0"}, networkNodes: make(map[string]map[string]struct{})} | ||
for i := 0; i < 2000; i++ { | ||
n.addNetworkNode("network", "node-"+strconv.Itoa(i%1000)) | ||
} | ||
assert.Equal(t, 1000, len(n.networkNodes["network"])) | ||
for i := 0; i < 2000; i++ { | ||
n.addNetworkNode("network"+strconv.Itoa(i%1000), "node-"+strconv.Itoa(i)) | ||
} | ||
for i := 0; i < 1000; i++ { | ||
assert.Equal(t, 2, len(n.networkNodes["network"+strconv.Itoa(i%1000)])) | ||
} | ||
} | ||
|
||
func TestDeleteNetworkNode(t *testing.T) { | ||
n := &NetworkDB{config: &Config{NodeID: "node-0"}, networkNodes: make(map[string]map[string]struct{})} | ||
for i := 0; i < 1000; i++ { | ||
n.addNetworkNode("network", "node-"+strconv.Itoa(i%1000)) | ||
} | ||
assert.Equal(t, 1000, len(n.networkNodes["network"])) | ||
for i := 0; i < 2000; i++ { | ||
n.deleteNetworkNode("network", "node-"+strconv.Itoa(i%1000)) | ||
} | ||
assert.Equal(t, 0, len(n.networkNodes["network"])) | ||
for i := 0; i < 2000; i++ { | ||
n.addNetworkNode("network"+strconv.Itoa(i%1000), "node-"+strconv.Itoa(i)) | ||
} | ||
for i := 0; i < 1000; i++ { | ||
assert.Equal(t, 2, len(n.networkNodes["network"+strconv.Itoa(i%1000)])) | ||
n.deleteNetworkNode("network"+strconv.Itoa(i%1000), "node-"+strconv.Itoa(i)) | ||
assert.Equal(t, 1, len(n.networkNodes["network"+strconv.Itoa(i%1000)])) | ||
} | ||
for i := 1000; i < 2000; i++ { | ||
n.deleteNetworkNode("network"+strconv.Itoa(i%1000), "node-"+strconv.Itoa(i)) | ||
assert.Equal(t, 0, len(n.networkNodes["network"+strconv.Itoa(i%1000)])) | ||
} | ||
} | ||
|
||
func TestRandomNodes(t *testing.T) { | ||
n := &NetworkDB{config: &Config{NodeID: "node-0"}} | ||
nodes := make(map[string]struct{}) | ||
for i := 0; i < 1000; i++ { | ||
nodes["node-"+strconv.Itoa(i)] = struct{}{} | ||
} | ||
nodeHit := make(map[string]int) | ||
for i := 0; i < 5000; i++ { | ||
chosen := n.mRandomNodes(3, nodes) | ||
for _, c := range chosen { | ||
if c == "node-0" { | ||
t.Fatal("should never hit itself") | ||
} | ||
nodeHit[c]++ | ||
} | ||
} | ||
|
||
// check results | ||
var min, max int | ||
for node, hit := range nodeHit { | ||
if min == 0 { | ||
min = hit | ||
} | ||
if hit == 0 && node != "node-0" { | ||
t.Fatal("node never hit") | ||
} | ||
if hit > max { | ||
max = hit | ||
} | ||
if hit < min { | ||
min = hit | ||
} | ||
} | ||
assert.NotEqual(t, 0, min) | ||
} | ||
|
||
func BenchmarkAddNetworkNode(b *testing.B) { | ||
n := &NetworkDB{config: &Config{NodeID: "node-0"}, networkNodes: make(map[string]map[string]struct{})} | ||
for i := 0; i < b.N; i++ { | ||
n.addNetworkNode("network", "node-"+strconv.Itoa(i%1000)) | ||
} | ||
} | ||
|
||
func BenchmarkDeleteNetworkNode(b *testing.B) { | ||
n := &NetworkDB{config: &Config{NodeID: "node-0"}, networkNodes: make(map[string]map[string]struct{})} | ||
nodes := make([]string, 0, 1000) | ||
for i := 0; i < 1000; i++ { | ||
name := "node-" + strconv.Itoa(i) | ||
n.addNetworkNode("network", name) | ||
nodes = append(nodes, name) | ||
} | ||
b.ResetTimer() | ||
for i := 0; i < b.N; i++ { | ||
n.deleteNetworkNode("network", nodes[i%1000]) | ||
} | ||
} | ||
|
||
func BenchmarkRandomNodes(b *testing.B) { | ||
n := &NetworkDB{config: &Config{NodeID: "node-0"}} | ||
nodes := make(map[string]struct{}) | ||
for i := 0; i < 1000; i++ { | ||
nodes["node-"+strconv.Itoa(i)] = struct{}{} | ||
} | ||
b.ResetTimer() | ||
for i := 0; i < b.N; i++ { | ||
n.mRandomNodes(3, nodes) | ||
} | ||
} |
This file contains hidden or 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 hidden or 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.
I think
go
does not guarantee therange
is random as a spec. Will there be any side effects if a certain implementation of Go returns predictable range? Also is the impact of calling randomOffet rather significant?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.
I have to check on that, I read that since go 1.0 the keys were randomized, but have to check is that is a common assumption for all the architectures, or at least the one that we support.
Yes the randomOffset is the bottlenek, that can be seen from the detailed flame graph, the rand.Int has 90% and 10% is the big.NewInt
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.
Ah I see. This thread has some details: https://groups.google.com/forum/#!topic/golang-nuts/zBcqMsDNt7Q . I guess it is safe to assume the randomness if there is a major perf gain and just have a comment about the assumption.
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.
It's not cryptographically random and also the test highlighted some unbalance in the results, but the test also makes sure that there is no node that never comes up in the selection (min == 0 condition)
Another approach that I was thinking about is to have the original string slice and an index that is saved with the network and loop on the slice in a circular buffer fashion. Considering that nodes not change every second, that should guarantee fairness in the selection and the randomness is guaranteed at insertion time (on the base of when the node join). The problem of the slice still remains the linear loop for each insertion and deletion that is pretty lame to pay.