forked from Redundancy/go-sync
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gosync_test.go
111 lines (88 loc) · 2.66 KB
/
gosync_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
110
111
package gosync
import (
"fmt"
"testing"
"bytes"
"github.com/Redundancy/go-sync/blocksources"
"github.com/Redundancy/go-sync/comparer"
"github.com/Redundancy/go-sync/filechecksum"
"github.com/Redundancy/go-sync/indexbuilder"
"github.com/Redundancy/go-sync/util/readers"
)
func Example() {
// due to short example strings, use a very small block size
// using one this small in practice would increase your file transfer!
const blockSize = 4
// This is the "file" as described by the authoritive version
const reference = "The quick brown fox jumped over the lazy dog"
// This is what we have locally. Not too far off, but not correct.
const localVersion = "The qwik brown fox jumped 0v3r the lazy"
generator := filechecksum.NewFileChecksumGenerator(blockSize)
_, referenceFileIndex, _, err := indexbuilder.BuildIndexFromString(
generator,
reference,
)
if err != nil {
return
}
referenceAsBytes := []byte(reference)
localVersionAsBytes := []byte(localVersion)
blockCount := len(referenceAsBytes) / blockSize
if len(referenceAsBytes)%blockSize != 0 {
blockCount++
}
inputFile := bytes.NewReader(localVersionAsBytes)
patchedFile := bytes.NewBuffer(nil)
// This is more complicated than usual, because we're using in-memory
// "files" and sources. Normally you would use MakeRSync
summary := &BasicSummary{
ChecksumIndex: referenceFileIndex,
ChecksumLookup: nil,
BlockCount: uint(blockCount),
BlockSize: blockSize,
FileSize: int64(len(referenceAsBytes)),
}
rsync := &RSync{
Input: inputFile,
Output: patchedFile,
Source: blocksources.NewReadSeekerBlockSource(
bytes.NewReader(referenceAsBytes),
blocksources.MakeNullFixedSizeResolver(uint64(blockSize)),
),
Summary: summary,
OnClose: nil,
}
if err := rsync.Patch(); err != nil {
fmt.Printf("Error: %v", err)
return
}
fmt.Printf("Patched result: \"%s\"\n", patchedFile.Bytes())
// Output:
// Patched result: "The quick brown fox jumped over the lazy dog"
}
const (
BYTE = 1
KB = 1024 * BYTE
MB = 1024 * KB
)
func BenchmarkIndexComparisons(b *testing.B) {
b.ReportAllocs()
const SIZE = 200 * KB
b.SetBytes(SIZE)
file := readers.NewSizedNonRepeatingSequence(6, SIZE)
generator := filechecksum.NewFileChecksumGenerator(8 * KB)
_, index, _, err := indexbuilder.BuildChecksumIndex(generator, file)
if err != nil {
b.Fatal(err)
}
b.StartTimer()
for i := 0; i < b.N; i++ {
// must reinitialize the file for each comparison
otherFile := readers.NewSizedNonRepeatingSequence(745656, SIZE)
compare := &comparer.Comparer{}
m := compare.StartFindMatchingBlocks(otherFile, 0, generator, index)
for _, ok := <-m; ok; {
}
}
b.StopTimer()
}