-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathchunking_test.go
130 lines (110 loc) · 2.5 KB
/
chunking_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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
package chunking
import (
"bytes"
"context"
"fmt"
"io"
"testing"
"github.com/jotfs/fastcdc-go"
resticchunker "github.com/restic/chunker"
"github.com/skerkour/go-benchmarks/utils"
tigerwill90fastcdc "github.com/tigerwill90/fastcdc"
)
type Chunker interface {
Chunk(input []byte) (err error)
}
func BenchmarkChunking(b *testing.B) {
benchmarks := []int64{
64,
1024,
16 * 1024,
64 * 1024,
1024 * 1024,
10 * 1024 * 1024,
100 * 1024 * 1024,
1024 * 1024 * 1024,
}
for _, size := range benchmarks {
benchmarkChunker(size, "jotfs_fastcdc", jotfsFastCDCChunker{}, b)
benchmarkChunker(size, "tigerwill90_fastcdc", tigerwill90FastCDCChunker{}, b)
benchmarkChunker(size, "restic_chunker", resticChunker{}, b)
}
}
func benchmarkChunker[C Chunker](size int64, algorithm string, chunker C, b *testing.B) {
b.Run(fmt.Sprintf("%s-%s", utils.BytesCount(size), algorithm), func(b *testing.B) {
b.ReportAllocs()
b.SetBytes(size)
buf := utils.RandBytes(b, size)
b.ResetTimer()
for i := 0; i < b.N; i++ {
chunker.Chunk(buf)
}
})
}
type jotfsFastCDCChunker struct{}
func (jotfsFastCDCChunker) Chunk(input []byte) (err error) {
data := bytes.NewReader(input)
opts := fastcdc.Options{
// MinSize: 32 * 1024,
AverageSize: 64 * 1024,
// MaxSize: 128 * 1024,
}
chunker, err := fastcdc.NewChunker(data, opts)
if err != nil {
return
}
for {
_, err = chunker.Next()
if err != nil {
if err == io.EOF {
err = nil
}
return
}
}
}
type tigerwill90FastCDCChunker struct{}
func (tigerwill90FastCDCChunker) Chunk(input []byte) (err error) {
data := bytes.NewReader(input)
chunker, err := tigerwill90fastcdc.NewChunker(context.Background(), tigerwill90fastcdc.WithStreamMode(), tigerwill90fastcdc.With64kChunks())
if err != nil {
return
}
buf := make([]byte, 3*64*1024)
for {
var n int
n, err = data.Read(buf)
if err != nil {
if err == io.EOF {
err = nil
break
}
return
}
err = chunker.Split(bytes.NewReader(buf[:n]), func(offset, length uint, chunk []byte) error {
return nil
})
if err != nil {
return
}
}
err = chunker.Finalize(func(offset, length uint, chunk []byte) error {
return nil
})
return
}
type resticChunker struct{}
func (resticChunker) Chunk(input []byte) (err error) {
data := bytes.NewReader(input)
chnkr := resticchunker.New(data, resticchunker.Pol(0x3DA3358B4DC173))
buf := make([]byte, 8*1024*1024)
for {
_, err = chnkr.Next(buf)
if err != nil {
if err == io.EOF {
err = nil
}
return
}
}
}