-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathdedupe_test.go
92 lines (73 loc) · 1.82 KB
/
dedupe_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
package ant
import (
"context"
"net/url"
"testing"
"github.com/stretchr/testify/require"
)
func TestDeduper(t *testing.T) {
t.Run("map", func(t *testing.T) {
var ctx = context.Background()
var assert = require.New(t)
var urls = parseURLs(t, "https://a", "https://b")
var d = DedupeMap()
ret, err := d.Dedupe(ctx, urls)
assert.NoError(err)
assert.Equal(urls, ret)
urls = parseURLs(t, "https://a", "https://b", "https://c")
ret, err = d.Dedupe(ctx, urls)
assert.NoError(err)
assert.Equal(urls[2:], ret)
})
t.Run("bf", func(t *testing.T) {
var ctx = context.Background()
var assert = require.New(t)
var urls = parseURLs(t, "https://a", "https://b")
var d = DedupeBF(2000000, 5)
ret, err := d.Dedupe(ctx, urls)
assert.NoError(err)
assert.Equal(urls, ret)
urls = parseURLs(t, "https://a", "https://b", "https://c")
ret, err = d.Dedupe(ctx, urls)
assert.NoError(err)
assert.Equal(urls[2:], ret)
})
}
func BenchmarkDedupe(b *testing.B) {
b.Run("map", func(b *testing.B) {
var ctx = context.Background()
var urls = parseURLs(b, "https://a", "https://b")
var d = DedupeMap()
for i := 0; i < b.N; i++ {
d.Dedupe(ctx, urls)
}
})
b.Run("bf", func(b *testing.B) {
var ctx = context.Background()
var urls = parseURLs(b, "https://a", "https://b")
var d = DedupeBF(200000, 5)
for i := 0; i < b.N; i++ {
d.Dedupe(ctx, urls)
}
})
}
func parseURLs(t testing.TB, rawurls ...string) URLs {
var ret = make(URLs, 0, len(rawurls))
t.Helper()
for _, rawurl := range rawurls {
u, err := url.Parse(rawurl)
if err != nil {
t.Fatalf("parse url %q - %s", rawurl, err)
}
ret = append(ret, u)
}
return ret
}
func parseURL(t testing.TB, rawurl string) *URL {
t.Helper()
u, err := url.Parse(rawurl)
if err != nil {
t.Fatalf("parse url %q - %s", rawurl, err)
}
return u
}