Skip to content

Commit

Permalink
chore: sevral small refinements
Browse files Browse the repository at this point in the history
  • Loading branch information
Jia He committed Jul 9, 2022
1 parent c6030a7 commit 4f06579
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 30 deletions.
3 changes: 3 additions & 0 deletions fsearch.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ type FSearch struct {
resultLimit int
}

// New creates a new Fsearch
// pathSeparator is the path separator in the path
// limit is the upper bound of matched results size, 0 means unlimited (not recommended).
func New(pathSeparator string, limit int) *FSearch {
fs := &FSearch{
on: true,
Expand Down
62 changes: 33 additions & 29 deletions fsearch_bench_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,45 +5,49 @@ import (
"testing"
)

func genTreePaths(sampleSize, factor int) map[string]bool {
paths := map[string]bool{"root": true}
nextLevelPaths := map[string]bool{}

for len(paths) <= sampleSize {
for pathname := range paths {
for i := 0; i < factor; i++ {
subPath := fmt.Sprintf("%s/%s", pathname, randStr.Alphabets())
nextLevelPaths[subPath] = true
}

if len(nextLevelPaths) > sampleSize {
break
}
}

paths = nextLevelPaths
nextLevelPaths = map[string]bool{}
}

return paths
}

func BenchmarkFSearch(b *testing.B) {
const (
segmentLen = 4
factor = 10
sampleSize = 10000 * 100
)

genTreePaths := func(sampleSize int) map[string]bool {
paths := map[string]bool{"root": true}
nextLevelPaths := map[string]bool{}

for len(paths) <= sampleSize {
for pathname := range paths {
for i := 0; i < factor; i++ {
subPath := fmt.Sprintf("%s/%s", pathname, randStr.Alphabets())
nextLevelPaths[subPath] = true
}

if len(nextLevelPaths) > sampleSize {
break
}
}

paths = nextLevelPaths
nextLevelPaths = map[string]bool{}
var pathnames = genTreePaths(sampleSize, factor)
fmt.Printf("bench sample size (%d)\n", len(pathnames))
fs := New("/", 0)
for pathname := range pathnames {
err := fs.AddPath(pathname)
if err != nil {
b.Fatal(err)
}

return paths
}

pathnames := genTreePaths(sampleSize)
fmt.Printf("bench sample size (%d)\n", len(pathnames))
b.ResetTimer()

for i := 0; i < b.N; i++ {
fs := New("/", 0)
for pathname := range pathnames {
err := fs.AddPath(pathname)
if err != nil {
b.Fatal(err)
}
}
fs.Search("ab")
}
}
2 changes: 1 addition & 1 deletion fsearch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ func TestFSearch(t *testing.T) {
if len(fs.idsToDelete) == 0 {
break
}
fmt.Printf("wating for drain: %d\n", len(fs.idsToDelete))
fmt.Printf("waiting for drain: %d\n", len(fs.idsToDelete))
time.Sleep(500 * time.Millisecond)
}

Expand Down

0 comments on commit 4f06579

Please sign in to comment.