-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathzsync_bench_test.go
100 lines (77 loc) · 2.47 KB
/
zsync_bench_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
package zsync
import (
"io"
"io/ioutil"
"os"
"testing"
"github.com/AppImageCrafters/libzsync-go/chunksmapper"
"github.com/AppImageCrafters/libzsync-go/control"
"github.com/AppImageCrafters/libzsync-go/sources"
"github.com/stretchr/testify/assert"
)
func BenchmarkZSync2_Sync(t *testing.B) {
tests := []string{
"/file_displaced",
"/1st_chunk_changed",
"/2nd_chunk_changed",
"/3rd_chunk_changed",
"/all_changed",
"/large_file",
}
for _, tt := range tests {
t.Run(tt, func(t *testing.B) {
zsyncControl, _ := getControl("file.zsync")
zsyncControl.URL = serverUrl + "file"
zsync := NewZSyncFromControl(zsyncControl)
outputPath := dataDir + "/file_copy"
output, err := os.Create(outputPath)
assert.Equal(t, err, nil)
defer output.Close()
err = zsync.Sync(dataDir+tt, output)
if err != nil {
t.Fatal(err)
}
expected, _ := ioutil.ReadFile(dataDir + "/file")
result, _ := ioutil.ReadFile(outputPath)
assert.Equal(t, expected, result)
_ = os.Remove(outputPath)
})
}
}
func BenchmarkZSync2_SyncAppImageTool(t *testing.B) {
data, err := os.Open("/tmp/appimagetool-x86_64.AppImage.zsync")
assert.Nil(t, err)
zsyncControl, err := control.ReadControl(data)
assert.Nil(t, err)
zsync := ZSync{
BlockSize: int64(zsyncControl.BlockSize),
ChecksumsIndex: zsyncControl.ChecksumIndex,
RemoteFileSize: zsyncControl.FileLength,
RemoteFileUrl: "https://github.com/AppImage/AppImageKit/releases/download/continuous/appimagetool-x86_64.AppImage",
}
output, err := os.Create("/tmp/appimagetool-new-x86_64.AppImage")
assert.Nil(t, err)
filePath := "/tmp/appimagetool-x86_64.AppImage"
reusableChunks, err := zsync.SearchReusableChunks(filePath)
assert.Nil(t, err)
input, err := os.Open(filePath)
assert.Nil(t, err)
chunkMapper := chunksmapper.NewFileChunksMapper(zsync.RemoteFileSize)
for chunk := range reusableChunks {
err = zsync.WriteChunk(input, output, chunk)
assert.Nil(t, err)
chunkMapper.Add(chunk)
}
missingChunksSource := sources.HttpFileSource{URL: zsync.RemoteFileUrl, Size: zsync.RemoteFileSize}
missingChunks := chunkMapper.GetMissingChunks()
for _, chunk := range missingChunks {
// fetch whole chunk to reduce the number of request
_, err = missingChunksSource.Seek(chunk.SourceOffset, io.SeekStart)
assert.Nil(t, err)
err = missingChunksSource.Request(chunk.Size)
assert.Nil(t, err)
err = zsync.WriteChunk(&missingChunksSource, output, chunk)
assert.Nil(t, err)
}
assert.Nil(t, err)
}