catalog: add benchmarks for overridden path comparison

Signed-off-by: Stephen J Day <stephen.day@docker.com>
This commit is contained in:
Stephen J Day 2016-08-08 17:09:49 -07:00
parent bba5a0d05c
commit 308faf00f1
No known key found for this signature in database
GPG key ID: FB5F6B2905D7ECF3
2 changed files with 85 additions and 0 deletions

View file

@ -108,6 +108,10 @@ func lessPath(a, b string) bool {
// compareReplaceInline modifies runtime.cmpstring to replace old with new // compareReplaceInline modifies runtime.cmpstring to replace old with new
// during a byte-wise comparison. // during a byte-wise comparison.
func compareReplaceInline(s1, s2 string, old, new byte) int { func compareReplaceInline(s1, s2 string, old, new byte) int {
// TODO(stevvooe): We are missing an optimization when the s1 and s2 have
// the exact same slice header. It will make the code unsafe but can
// provide some extra performance.
l := len(s1) l := len(s1)
if len(s2) < l { if len(s2) < l {
l = len(s2) l = len(s2)

View file

@ -3,6 +3,7 @@ package storage
import ( import (
"fmt" "fmt"
"io" "io"
"math/rand"
"testing" "testing"
"github.com/docker/distribution" "github.com/docker/distribution"
@ -220,3 +221,83 @@ func TestCatalogWalkError(t *testing.T) {
t.Errorf("Expected catalog driver list error") 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)
}