forked from TrueCloudLab/distribution
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
|
@ -108,6 +108,10 @@ func lessPath(a, b string) bool {
|
|||
// compareReplaceInline modifies runtime.cmpstring to replace old with new
|
||||
// during a byte-wise comparison.
|
||||
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)
|
||||
if len(s2) < l {
|
||||
l = len(s2)
|
||||
|
|
|
@ -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…
Reference in a new issue