Skip to content
This repository was archived by the owner on Jun 3, 2025. It is now read-only.

Commit 4647b3e

Browse files
committed
test: add BenchmarkHasFilepathPrefix
1 parent ab04b8a commit 4647b3e

1 file changed

Lines changed: 80 additions & 0 deletions

File tree

pkg/util/fs_util_test.go

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,13 @@ package util
1919
import (
2020
"archive/tar"
2121
"bytes"
22+
"fmt"
2223
"io/ioutil"
2324
"os"
2425
"path/filepath"
2526
"reflect"
2627
"sort"
28+
"strings"
2729
"testing"
2830

2931
"github.com/GoogleContainerTools/kaniko/testutil"
@@ -310,6 +312,84 @@ func TestHasFilepathPrefix(t *testing.T) {
310312
}
311313
}
312314

315+
func BenchmarkHasFilepathPrefix(b *testing.B) {
316+
tests := []struct {
317+
path string
318+
prefix string
319+
prefixMatchOnly bool
320+
}{
321+
{
322+
path: "/foo/bar",
323+
prefix: "/foo",
324+
prefixMatchOnly: true,
325+
},
326+
{
327+
path: "/foo/bar/baz",
328+
prefix: "/foo",
329+
prefixMatchOnly: true,
330+
},
331+
{
332+
path: "/foo/bar/baz/foo",
333+
prefix: "/foo",
334+
prefixMatchOnly: true,
335+
},
336+
{
337+
path: "/foo/bar/baz/foo/foobar",
338+
prefix: "/foo",
339+
prefixMatchOnly: true,
340+
},
341+
{
342+
path: "/foo/bar",
343+
prefix: "/foo/bar",
344+
prefixMatchOnly: true,
345+
},
346+
{
347+
path: "/foo/bar/baz",
348+
prefix: "/foo/bar",
349+
prefixMatchOnly: true,
350+
},
351+
{
352+
path: "/foo/bar/baz/foo",
353+
prefix: "/foo/bar",
354+
prefixMatchOnly: true,
355+
},
356+
{
357+
path: "/foo/bar/baz/foo/foobar",
358+
prefix: "/foo/bar",
359+
prefixMatchOnly: true,
360+
},
361+
{
362+
path: "/foo/bar",
363+
prefix: "/foo/bar/baz",
364+
prefixMatchOnly: true,
365+
},
366+
{
367+
path: "/foo/bar/baz",
368+
prefix: "/foo/bar/baz",
369+
prefixMatchOnly: true,
370+
},
371+
{
372+
path: "/foo/bar/baz/foo",
373+
prefix: "/foo/bar/baz",
374+
prefixMatchOnly: true,
375+
},
376+
{
377+
path: "/foo/bar/baz/foo/foobar",
378+
prefix: "/foo/bar/baz",
379+
prefixMatchOnly: true,
380+
},
381+
}
382+
for _, ts := range tests {
383+
name := fmt.Sprint("PathDepth=", strings.Count(ts.path, "/"), ",PrefixDepth=", strings.Count(ts.prefix, "/"))
384+
b.Run(name, func(b *testing.B) {
385+
b.ReportAllocs()
386+
for i := 0; i < b.N; i++ {
387+
HasFilepathPrefix(ts.path, ts.prefix, ts.prefixMatchOnly)
388+
}
389+
})
390+
}
391+
}
392+
313393
type checker func(root string, t *testing.T)
314394

315395
func fileExists(p string) checker {

0 commit comments

Comments
 (0)