Skip to content

Commit 16b0804

Browse files
committed
Refactor benchmarks
1 parent 6ae06cb commit 16b0804

File tree

1 file changed

+58
-34
lines changed

1 file changed

+58
-34
lines changed

diffmatchpatch/diffmatchpatch_test.go

Lines changed: 58 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,22 @@ func diffRebuildtexts(diffs []Diff) []string {
8181
return text
8282
}
8383

84+
func readFile(b *testing.B, filename string) string {
85+
bytes, err := ioutil.ReadFile(filename)
86+
if err != nil {
87+
b.Fatal(err)
88+
}
89+
90+
return string(bytes)
91+
}
92+
93+
func speedtestTexts(b *testing.B) (s1 string, s2 string) {
94+
s1 = readFile(b, "../testdata/speedtest1.txt")
95+
s2 = readFile(b, "../testdata/speedtest2.txt")
96+
97+
return s1, s2
98+
}
99+
84100
func Test_diffCommonPrefix(t *testing.T) {
85101
dmp := New()
86102
// Detect any common suffix.
@@ -1498,73 +1514,89 @@ func TestLastIndexOf(t *testing.T) {
14981514
}
14991515
}
15001516

1501-
func Benchmark_DiffMain(bench *testing.B) {
1502-
dmp := New()
1503-
dmp.DiffTimeout = time.Second
1504-
a := "`Twas brillig, and the slithy toves\nDid gyre and gimble in the wabe:\nAll mimsy were the borogoves,\nAnd the mome raths outgrabe.\n"
1505-
b := "I am the very model of a modern major general,\nI've information vegetable, animal, and mineral,\nI know the kings of England, and I quote the fights historical,\nFrom Marathon to Waterloo, in order categorical.\n"
1517+
func BenchmarkDiffMain(bench *testing.B) {
1518+
s1 := "`Twas brillig, and the slithy toves\nDid gyre and gimble in the wabe:\nAll mimsy were the borogoves,\nAnd the mome raths outgrabe.\n"
1519+
s2 := "I am the very model of a modern major general,\nI've information vegetable, animal, and mineral,\nI know the kings of England, and I quote the fights historical,\nFrom Marathon to Waterloo, in order categorical.\n"
1520+
15061521
// Increase the text lengths by 1024 times to ensure a timeout.
15071522
for x := 0; x < 10; x++ {
1508-
a = a + a
1509-
b = b + b
1523+
s1 = s1 + s1
1524+
s2 = s2 + s2
15101525
}
1526+
1527+
dmp := New()
1528+
dmp.DiffTimeout = time.Second
1529+
15111530
bench.ResetTimer()
1531+
15121532
for i := 0; i < bench.N; i++ {
1513-
dmp.DiffMain(a, b, true)
1533+
dmp.DiffMain(s1, s2, true)
15141534
}
15151535
}
15161536

1517-
func Benchmark_DiffCommonPrefix(b *testing.B) {
1537+
func BenchmarkDiffCommonPrefix(b *testing.B) {
1538+
s := "ABCDEFGHIJKLMNOPQRSTUVWXYZÅÄÖ"
1539+
15181540
dmp := New()
1519-
a := "ABCDEFGHIJKLMNOPQRSTUVWXYZÅÄÖ"
1541+
15201542
for i := 0; i < b.N; i++ {
1521-
dmp.DiffCommonPrefix(a, a)
1543+
dmp.DiffCommonPrefix(s, s)
15221544
}
15231545
}
15241546

1525-
func Benchmark_DiffCommonSuffix(b *testing.B) {
1547+
func BenchmarkDiffCommonSuffix(b *testing.B) {
1548+
s := "ABCDEFGHIJKLMNOPQRSTUVWXYZÅÄÖ"
1549+
15261550
dmp := New()
1527-
a := "ABCDEFGHIJKLMNOPQRSTUVWXYZÅÄÖ"
1551+
1552+
b.ResetTimer()
1553+
15281554
for i := 0; i < b.N; i++ {
1529-
dmp.DiffCommonSuffix(a, a)
1555+
dmp.DiffCommonSuffix(s, s)
15301556
}
15311557
}
15321558

1533-
func Benchmark_DiffMainLarge(b *testing.B) {
1534-
s1 := readFile("../testdata/speedtest1.txt", b)
1535-
s2 := readFile("../testdata/speedtest2.txt", b)
1559+
func BenchmarkDiffMainLarge(b *testing.B) {
1560+
s1, s2 := speedtestTexts(b)
1561+
15361562
dmp := New()
1563+
15371564
b.ResetTimer()
1565+
15381566
for i := 0; i < b.N; i++ {
15391567
dmp.DiffMain(s1, s2, true)
15401568
}
15411569
}
15421570

1543-
func Benchmark_DiffMainLargeLines(b *testing.B) {
1544-
s1 := readFile("../testdata/speedtest1.txt", b)
1545-
s2 := readFile("../testdata/speedtest2.txt", b)
1571+
func BenchmarkDiffMainRunesLargeLines(b *testing.B) {
1572+
s1, s2 := speedtestTexts(b)
1573+
15461574
dmp := New()
1575+
15471576
b.ResetTimer()
1577+
15481578
for i := 0; i < b.N; i++ {
15491579
text1, text2, linearray := dmp.DiffLinesToRunes(s1, s2)
1580+
15501581
diffs := dmp.DiffMainRunes(text1, text2, false)
15511582
diffs = dmp.DiffCharsToLines(diffs, linearray)
15521583
}
15531584
}
15541585

1555-
func Benchmark_DiffHalfMatch(b *testing.B) {
1556-
s1 := readFile("../testdata/speedtest1.txt", b)
1557-
s2 := readFile("../testdata/speedtest2.txt", b)
1586+
func BenchmarkDiffHalfMatch(b *testing.B) {
1587+
s1, s2 := speedtestTexts(b)
1588+
15581589
dmp := New()
1590+
15591591
b.ResetTimer()
1592+
15601593
for i := 0; i < b.N; i++ {
15611594
dmp.DiffHalfMatch(s1, s2)
15621595
}
15631596
}
15641597

1565-
func Benchmark_DiffCleanupSemantic(b *testing.B) {
1566-
s1 := readFile("../testdata/speedtest1.txt", b)
1567-
s2 := readFile("../testdata/speedtest2.txt", b)
1598+
func BenchmarkDiffCleanupSemantic(b *testing.B) {
1599+
s1, s2 := speedtestTexts(b)
15681600

15691601
dmp := New()
15701602

@@ -1576,11 +1608,3 @@ func Benchmark_DiffCleanupSemantic(b *testing.B) {
15761608
dmp.DiffCleanupSemantic(diffs)
15771609
}
15781610
}
1579-
1580-
func readFile(filename string, b *testing.B) string {
1581-
bytes, err := ioutil.ReadFile(filename)
1582-
if err != nil {
1583-
b.Fatal(err)
1584-
}
1585-
return string(bytes)
1586-
}

0 commit comments

Comments
 (0)