Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

path.Depth speed + memory optimization #2

Merged
merged 1 commit into from
Nov 2, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ Crawls web pages and prints any link it can find.
- scan depth (limited by starting host and path, by default - 0) can be configured
- can crawl `robots.txt` rules and sitemaps
- `brute` mode - scan html comments for urls (this can lead to bogus results)
- make use of `HTTP_PROXY` / `HTTPS_PROXY` environment values

# installation

Expand Down
12 changes: 11 additions & 1 deletion pkg/crawler/crawl.go
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,17 @@ func canCrawl(a, b *url.URL, d int) (yes bool) {
return
}

depth, found := path.Depth(a.EscapedPath(), b.EscapedPath())
var apath, bpath string

if apath = a.Path; apath == "" {
apath = "/"
}

if bpath = b.Path; bpath == "" {
bpath = "/"
}

depth, found := path.Depth(apath, bpath)
if !found {
return
}
Expand Down
3 changes: 3 additions & 0 deletions pkg/crawler/crawl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ func Test_canCrawl(t *testing.T) {
url0, _ := url.Parse("http://test/some")
url1, _ := url.Parse("http://test/some/path/even")
url2, _ := url.Parse("http://test/some/path/even/more")
url3, _ := url.Parse("http://test")

tests := []struct {
name string
Expand All @@ -47,6 +48,7 @@ func Test_canCrawl(t *testing.T) {
{"url2-0-1", args{b: base, u: url0, d: -1}, false},
{"url2-1-1", args{b: base, u: url1, d: -1}, true},
{"url2-2-1", args{b: base, u: url2, d: -1}, true},
{"url3-3", args{b: base, u: url3, d: 0}, false},
}

for _, tt := range tests {
Expand Down Expand Up @@ -276,6 +278,7 @@ sitemap: http://other.host/sitemap.xml`
}

if len(resA) != 5 {
t.Log(resA)
t.Fatal("unexpected len for A")
}

Expand Down
37 changes: 17 additions & 20 deletions pkg/path/depth.go
Original file line number Diff line number Diff line change
@@ -1,41 +1,38 @@
package path

import (
"path"
"strings"
)

const pathSep = '/'

func isPathSep(r rune) (yes bool) {
return r == pathSep
}

// Depth calculates relative depth for `sub` to `base` resorces path.
func Depth(base, sub string) (n int, ok bool) {
var (
bp = splitPath(base)
sp = splitPath(sub)
bn = path.Clean(base)
sn = path.Clean(sub)
)

if len(sp) < len(bp) {
if len(sn) <= len(bn) {
return
}

for i := 0; i < len(bp); i++ {
if bp[i] != sp[i] {
return
}
if !strings.HasPrefix(sn, bn) {
return
}

return len(sp) - len(bp), true
}

func splitPath(p string) (o []string) {
return dropSpaces(strings.Split(p, "/"))
}

func dropSpaces(s []string) (o []string) {
o = make([]string, 0, len(s))
fields := strings.FieldsFunc(sn[len(bn):], isPathSep)

for _, v := range s {
if v != "" {
o = append(o, v)
for i := 0; i < len(fields); i++ {
if fields[i] != "" {
n++
}
}

return o
return n, true
}
77 changes: 14 additions & 63 deletions pkg/path/depth_test.go
Original file line number Diff line number Diff line change
@@ -1,72 +1,9 @@
package path

import (
"reflect"
"testing"
)

func Test_dropSpaces(t *testing.T) {
type args struct {
s []string
}

tests := []struct {
name string
args args
wantO []string
}{
{"ones", args{s: []string{"", "1", ""}}, []string{"1"}},
{"twos", args{s: []string{"2", "", "2"}}, []string{"2", "2"}},
{"threes", args{s: []string{"", "", "3"}}, []string{"3"}},
{"empty", args{s: []string{"", "", ""}}, []string{}},
}

t.Parallel()

for _, tt := range tests {
tc := tt

t.Run(tc.name, func(t *testing.T) {
t.Parallel()

if gotO := dropSpaces(tc.args.s); !reflect.DeepEqual(gotO, tc.wantO) {
t.Errorf("dropSpaces() = %v, want %v", gotO, tc.wantO)
}
})
}
}

func Test_splitPath(t *testing.T) {
type args struct {
p string
}

tests := []struct {
name string
args args
wantO []string
}{
{"empty", args{p: "/"}, []string{}},
{"foo", args{p: "/foo"}, []string{"foo"}},
{"foo-bar", args{p: "/foo/bar"}, []string{"foo", "bar"}},
{"foo-bar-baz", args{p: "/foo/bar//baz"}, []string{"foo", "bar", "baz"}},
}

t.Parallel()

for _, tt := range tests {
tc := tt

t.Run(tc.name, func(t *testing.T) {
t.Parallel()

if gotO := splitPath(tc.args.p); !reflect.DeepEqual(gotO, tc.wantO) {
t.Errorf("splitPath() = %v, want %v", gotO, tc.wantO)
}
})
}
}

func Test_Depth(t *testing.T) {
type args struct {
base string
Expand All @@ -84,6 +21,7 @@ func Test_Depth(t *testing.T) {
{"c-bad", args{base: "/a/b", sub: "/c"}, 0, false},
{"b-ok", args{base: "/a", sub: "/a/b"}, 1, true},
{"c-ok", args{base: "/a", sub: "/a/b/c"}, 2, true},
{"d-bad", args{base: "/a/b/c", sub: "/d/b/c/a"}, 0, false},
}

t.Parallel()
Expand All @@ -104,3 +42,16 @@ func Test_Depth(t *testing.T) {
})
}
}

func Benchmark_Depth(b *testing.B) {
const (
x = "/some/rather/long/path"
y = "/some/rather/long/path/but/longer"
)

b.ResetTimer()

for i := 0; i < b.N; i++ {
_, _ = Depth(x, y)
}
}