-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlevenshtein_test.go
109 lines (90 loc) · 3.73 KB
/
levenshtein_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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
package sequencing
import (
"github.com/stretchr/testify/assert"
"testing"
)
type seq struct {
chars []byte
}
func (s *seq) Val(i int) interface{} {
return s.chars[i]
}
func (s *seq) Len() int {
return len(s.chars)
}
func toSeq(s string) Sequence {
sb := []byte(s)
return &seq{sb}
}
func TestDistanceI(t *testing.T) {
d := LevenshteinDistanceInterface(toSeq("kitten"), toSeq("sitting"), nil, nil)
assert.Equal(t, 3, d)
}
func TestDistanceK(t *testing.T) {
d := LevenshteinDistanceK([]byte("kitten"), []byte("sitting"), nil, nil, 3)
assert.Equal(t, 3, d)
d = LevenshteinDistanceK([]byte("kitten"), []byte("sitting"), nil, nil, 2)
assert.Equal(t, -1, d)
d = LevenshteinDistanceK([]byte("elephant"), []byte("hippo"), nil, nil, 8)
assert.Equal(t, 7, d)
d = LevenshteinDistanceK([]byte("elephant"), []byte(""), nil, nil, 100)
assert.Equal(t, 8, d)
}
func TestDistance(t *testing.T) {
d := LevenshteinDistance([]byte("kitten"), []byte("sitting"), nil, nil)
assert.Equal(t, 3, d)
}
func BenchmarkLevenshteinDistance(b *testing.B) {
b.Run("6 char string", func(b *testing.B) {
x := []byte("ABCDEF")
y := []byte("ABCCDEF")
b.ReportAllocs()
for i := 0; i < b.N; i++ {
LevenshteinDistance(x, y, nil, nil)
}
})
b.Run("25-30 char string", func(b *testing.B) {
x := []byte("This is a longer string")
y := []byte("This is a much longer string")
b.ReportAllocs()
for i := 0; i < b.N; i++ {
LevenshteinDistance(x, y, nil, nil)
}
})
b.Run("25-30 char string interface", func(b *testing.B) {
x := toSeq("This is a longer string")
y := toSeq("This is a much longer string")
b.ResetTimer()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
LevenshteinDistanceInterface(x, y, nil, nil)
}
})
b.Run("Long log line", func(b *testing.B) {
x := []byte("10__8__0__146 kernel process Google Chrome Ca[3955] caught causing excessive wakeups. Observed wakeups rate (per sec): 392; Maximum permitted wakeups rate (per sec): 150; Observation period: 300 seconds; Task lifetime number of wakeups: 317314")
y := []byte("10__8__0__146 kernel process Sublime Text[802] caught causing excessive wakeups. Observed wakeups rate (per sec): 233; Maximum permitted wakeups rate (per sec): 150; Observation period: 300 seconds; Task lifetime number of wakeups: 95333")
b.ReportAllocs()
for i := 0; i < b.N; i++ {
LevenshteinDistance(x, y, nil, nil)
}
})
b.Run("Long log line K bound", func(b *testing.B) {
x := []byte("10__8__0__146 kernel process Google Chrome Ca[3955] caught causing excessive wakeups. Observed wakeups rate (per sec): 392; Maximum permitted wakeups rate (per sec): 150; Observation period: 300 seconds; Task lifetime number of wakeups: 317314")
y := []byte("10__8__0__146 kernel process Sublime Text[802] caught causing excessive wakeups. Observed wakeups rate (per sec): 233; Maximum permitted wakeups rate (per sec): 150; Observation period: 300 seconds; Task lifetime number of wakeups: 95333")
b.ReportAllocs()
for i := 0; i < b.N; i++ {
LevenshteinDistanceK(x, y, nil, nil, len(x)/4)
}
})
b.Run("Long log line buffer reuse", func(b *testing.B) {
x := []byte("10__8__0__146 kernel process Google Chrome Ca[3955] caught causing excessive wakeups. Observed wakeups rate (per sec): 392; Maximum permitted wakeups rate (per sec): 150; Observation period: 300 seconds; Task lifetime number of wakeups: 317314")
y := []byte("10__8__0__146 kernel process Sublime Text[802] caught causing excessive wakeups. Observed wakeups rate (per sec): 233; Maximum permitted wakeups rate (per sec): 150; Observation period: 300 seconds; Task lifetime number of wakeups: 95333")
n := len(y)
v0 := make([]int, n+1, n+1)
v1 := make([]int, n+1, n+1)
b.ReportAllocs()
for i := 0; i < b.N; i++ {
LevenshteinDistance(x, y, v0, v1)
}
})
}