catalog: add benchmarks for overridden path comparison
Signed-off-by: Stephen J Day <stephen.day@docker.com>
This commit is contained in:
parent
bba5a0d05c
commit
308faf00f1
2 changed files with 85 additions and 0 deletions
|
@ -3,6 +3,7 @@ package storage
|
|||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"math/rand"
|
||||
"testing"
|
||||
|
||||
"github.com/docker/distribution"
|
||||
|
@ -220,3 +221,83 @@ func TestCatalogWalkError(t *testing.T) {
|
|||
t.Errorf("Expected catalog driver list error")
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkPathCompareEqual(B *testing.B) {
|
||||
B.StopTimer()
|
||||
pp := randomPath(100)
|
||||
// make a real copy
|
||||
ppb := append([]byte{}, []byte(pp)...)
|
||||
a, b := pp, string(ppb)
|
||||
|
||||
B.StartTimer()
|
||||
for i := 0; i < B.N; i++ {
|
||||
lessPath(a, b)
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkPathCompareNotEqual(B *testing.B) {
|
||||
B.StopTimer()
|
||||
a, b := randomPath(100), randomPath(100)
|
||||
B.StartTimer()
|
||||
|
||||
for i := 0; i < B.N; i++ {
|
||||
lessPath(a, b)
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkPathCompareNative(B *testing.B) {
|
||||
B.StopTimer()
|
||||
a, b := randomPath(100), randomPath(100)
|
||||
B.StartTimer()
|
||||
|
||||
for i := 0; i < B.N; i++ {
|
||||
c := a < b
|
||||
c = c && false
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkPathCompareNativeEqual(B *testing.B) {
|
||||
B.StopTimer()
|
||||
pp := randomPath(100)
|
||||
a, b := pp, pp
|
||||
B.StartTimer()
|
||||
|
||||
for i := 0; i < B.N; i++ {
|
||||
c := a < b
|
||||
c = c && false
|
||||
}
|
||||
}
|
||||
|
||||
var filenameChars = []byte("abcdefghijklmnopqrstuvwxyz0123456789")
|
||||
var separatorChars = []byte("._-")
|
||||
|
||||
func randomPath(length int64) string {
|
||||
path := "/"
|
||||
for int64(len(path)) < length {
|
||||
chunkLength := rand.Int63n(length-int64(len(path))) + 1
|
||||
chunk := randomFilename(chunkLength)
|
||||
path += chunk
|
||||
remaining := length - int64(len(path))
|
||||
if remaining == 1 {
|
||||
path += randomFilename(1)
|
||||
} else if remaining > 1 {
|
||||
path += "/"
|
||||
}
|
||||
}
|
||||
return path
|
||||
}
|
||||
|
||||
func randomFilename(length int64) string {
|
||||
b := make([]byte, length)
|
||||
wasSeparator := true
|
||||
for i := range b {
|
||||
if !wasSeparator && i < len(b)-1 && rand.Intn(4) == 0 {
|
||||
b[i] = separatorChars[rand.Intn(len(separatorChars))]
|
||||
wasSeparator = true
|
||||
} else {
|
||||
b[i] = filenameChars[rand.Intn(len(filenameChars))]
|
||||
wasSeparator = false
|
||||
}
|
||||
}
|
||||
return string(b)
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue