2019-08-06 23:56:12 +01:00
|
|
|
// Copyright (C) 2019 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information
|
|
|
|
|
|
|
|
package overlay_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"storj.io/storj/pkg/storj"
|
2019-11-14 19:46:15 +00:00
|
|
|
"storj.io/storj/private/testrand"
|
|
|
|
"storj.io/storj/private/teststorj"
|
2019-08-06 23:56:12 +01:00
|
|
|
"storj.io/storj/satellite/overlay"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestKeyLock(t *testing.T) {
|
|
|
|
ml := overlay.NewKeyLock()
|
|
|
|
key := teststorj.NodeIDFromString("hi")
|
|
|
|
unlockFunc := ml.Lock(key)
|
|
|
|
unlockFunc()
|
|
|
|
unlockFunc = ml.RLock(key)
|
|
|
|
unlockFunc()
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkKeyLock(b *testing.B) {
|
|
|
|
b.ReportAllocs()
|
|
|
|
ml := overlay.NewKeyLock()
|
|
|
|
numNodes := 100
|
|
|
|
nodeIDs := make([]storj.NodeID, numNodes)
|
|
|
|
for i := 0; i < numNodes; i++ {
|
|
|
|
nodeIDs[i] = testrand.NodeID()
|
|
|
|
}
|
|
|
|
b.Run("lock all new nodes", func(b *testing.B) {
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
unlockFunc := ml.Lock(testrand.NodeID())
|
|
|
|
unlockFunc()
|
|
|
|
}
|
|
|
|
})
|
|
|
|
b.Run("lock existing nodes", func(b *testing.B) {
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
idx := i % numNodes
|
|
|
|
unlockFunc := ml.Lock(nodeIDs[idx])
|
|
|
|
unlockFunc()
|
|
|
|
}
|
|
|
|
})
|
|
|
|
b.Run("rlock existing nodes", func(b *testing.B) {
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
idx := i % numNodes
|
|
|
|
unlockFunc := ml.RLock(nodeIDs[idx])
|
|
|
|
unlockFunc()
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|