test: add BenchmarkHasFilepathPrefix
@@ -19,11 +19,13 @@ package util
import (
"archive/tar"
"bytes"
+ "fmt"
"io/ioutil"
"os"
"path/filepath"
"reflect"
"sort"
+ "strings"
"testing"
"github.com/GoogleContainerTools/kaniko/testutil"
@@ -342,6 +344,84 @@ func TestHasFilepathPrefix(t *testing.T) {
}
}
+func BenchmarkHasFilepathPrefix(b *testing.B) {
+ tests := []struct {
+ path string
+ prefix string
+ prefixMatchOnly bool
+ }{
+ {
+ path: "/foo/bar",
+ prefix: "/foo",
+ prefixMatchOnly: true,
+ },
+ {
+ path: "/foo/bar/baz",
+ prefix: "/foo",
+ prefixMatchOnly: true,
+ },
+ {
+ path: "/foo/bar/baz/foo",
+ prefix: "/foo",
+ prefixMatchOnly: true,
+ },
+ {
+ path: "/foo/bar/baz/foo/foobar",
+ prefix: "/foo",
+ prefixMatchOnly: true,
+ },
+ {
+ path: "/foo/bar",
+ prefix: "/foo/bar",
+ prefixMatchOnly: true,
+ },
+ {
+ path: "/foo/bar/baz",
+ prefix: "/foo/bar",
+ prefixMatchOnly: true,
+ },
+ {
+ path: "/foo/bar/baz/foo",
+ prefix: "/foo/bar",
+ prefixMatchOnly: true,
+ },
+ {
+ path: "/foo/bar/baz/foo/foobar",
+ prefix: "/foo/bar",
+ prefixMatchOnly: true,
+ },
+ {
+ path: "/foo/bar",
+ prefix: "/foo/bar/baz",
+ prefixMatchOnly: true,
+ },
+ {
+ path: "/foo/bar/baz",
+ prefix: "/foo/bar/baz",
+ prefixMatchOnly: true,
+ },
+ {
+ path: "/foo/bar/baz/foo",
+ prefix: "/foo/bar/baz",
+ prefixMatchOnly: true,
+ },
+ {
+ path: "/foo/bar/baz/foo/foobar",
+ prefix: "/foo/bar/baz",
+ prefixMatchOnly: true,
+ },
+ }
+ for _, ts := range tests {
+ name := fmt.Sprint("PathDepth=", strings.Count(ts.path, "/"), ",PrefixDepth=", strings.Count(ts.prefix, "/"))
+ b.Run(name, func(b *testing.B) {
+ b.ReportAllocs()
+ for i := 0; i < b.N; i++ {
+ HasFilepathPrefix(ts.path, ts.prefix, ts.prefixMatchOnly)
+ }
+ })
+ }
+}
+
type checker func(root string, t *testing.T)
func fileExists(p string) checker {
