- Notifications
You must be signed in to change notification settings - Fork 3.9k
/
Copy pathbench_test.go
154 lines (139 loc) · 4.15 KB
/
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
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
// Copyright 2019 The Cockroach Authors.
//
// Use of this software is governed by the CockroachDB Software License
// included in the /LICENSE file.
package blobs
import (
"bytes"
"context"
"io"
"os"
"path/filepath"
"testing"
"github.com/cockroachdb/cockroach/pkg/roachpb"
"github.com/cockroachdb/cockroach/pkg/rpc"
"github.com/cockroachdb/cockroach/pkg/util/hlc"
"github.com/cockroachdb/cockroach/pkg/util/ioctx"
"github.com/cockroachdb/errors"
)
// filesize should be at least 1 GB when running these benchmarks.
// Reduced to 129 K for CI.
constfilesize=129*1<<10
typebenchmarkTestCasestruct {
localNodeID roachpb.NodeID
remoteNodeID roachpb.NodeID
localExternalDirstring
remoteExternalDirstring
blobClientBlobClient
fileSizeint64
fileNamestring
}
funcwriteLargeFile(t testing.TB, filestring, sizeint64) {
err:=os.MkdirAll(filepath.Dir(file), 0755)
iferr!=nil {
t.Fatal(err)
}
content:=make([]byte, size)
err=os.WriteFile(file, content, 0600)
iferr!=nil {
t.Fatal(err)
}
}
funcBenchmarkStreamingReadFile(b*testing.B) {
localNodeID:=roachpb.NodeID(1)
remoteNodeID:=roachpb.NodeID(2)
localExternalDir, remoteExternalDir, stopper, cleanUpFn:=createTestResources(b)
defercleanUpFn()
ctx:=context.Background()
clock:=hlc.NewClockForTesting(nil)
rpcContext:=rpc.NewInsecureTestingContext(ctx, clock, stopper)
rpcContext.TestingAllowNamedRPCToAnonymousServer=true
factory:=setUpService(b, rpcContext, localNodeID, remoteNodeID, localExternalDir, remoteExternalDir)
blobClient, err:=factory(ctx, remoteNodeID)
iferr!=nil {
b.Fatal(err)
}
params:=&benchmarkTestCase{
localNodeID: localNodeID,
remoteNodeID: remoteNodeID,
localExternalDir: localExternalDir,
remoteExternalDir: remoteExternalDir,
blobClient: blobClient,
fileSize: filesize,
fileName: "test/largefile.csv",
}
benchmarkStreamingReadFile(b, params)
}
funcbenchmarkStreamingReadFile(b*testing.B, tc*benchmarkTestCase) {
writeLargeFile(b, filepath.Join(tc.remoteExternalDir, tc.fileName), tc.fileSize)
writeTo:=LocalStorage{externalIODir: tc.localExternalDir}
b.ResetTimer()
b.SetBytes(tc.fileSize)
ctx:=context.Background()
fori:=0; i<b.N; i++ {
reader, _, err:=tc.blobClient.ReadFile(ctx, tc.fileName, 0)
iferr!=nil {
b.Fatal(err)
}
w, err:=writeTo.Writer(ctx, tc.fileName)
iferr!=nil {
b.Fatal(err)
}
if_, err:=io.Copy(w, ioctx.ReaderCtxAdapter(ctx, reader)); err!=nil {
b.Fatal(errors.CombineErrors(err, w.Close()))
}
iferr:=w.Close(); err!=nil {
b.Fatal(err)
}
stat, err:=writeTo.Stat(tc.fileName)
iferr!=nil {
b.Fatal(err)
}
ifstat.Filesize!=tc.fileSize {
b.Fatal("incorrect number of bytes written")
}
}
}
funcBenchmarkStreamingWriteFile(b*testing.B) {
localNodeID:=roachpb.NodeID(1)
remoteNodeID:=roachpb.NodeID(2)
localExternalDir, remoteExternalDir, stopper, cleanUpFn:=createTestResources(b)
defercleanUpFn()
ctx:=context.Background()
clock:=hlc.NewClockForTesting(nil)
rpcContext:=rpc.NewInsecureTestingContext(ctx, clock, stopper)
rpcContext.TestingAllowNamedRPCToAnonymousServer=true
factory:=setUpService(b, rpcContext, localNodeID, remoteNodeID, localExternalDir, remoteExternalDir)
blobClient, err:=factory(ctx, remoteNodeID)
iferr!=nil {
b.Fatal(err)
}
params:=&benchmarkTestCase{
localNodeID: localNodeID,
remoteNodeID: remoteNodeID,
localExternalDir: localExternalDir,
remoteExternalDir: remoteExternalDir,
blobClient: blobClient,
fileSize: filesize,
fileName: "test/largefile.csv",
}
benchmarkStreamingWriteFile(b, params)
}
funcbenchmarkStreamingWriteFile(b*testing.B, tc*benchmarkTestCase) {
content:=make([]byte, tc.fileSize)
b.ResetTimer()
b.SetBytes(tc.fileSize)
ctx:=context.Background()
fori:=0; i<b.N; i++ {
w, err:=tc.blobClient.Writer(ctx, tc.fileName)
iferr!=nil {
b.Fatal(err)
}
if_, err:=io.Copy(w, bytes.NewReader(content)); err!=nil {
b.Fatal(errors.CombineErrors(w.Close(), err))
}
iferr:=w.Close(); err!=nil {
b.Fatal(err)
}
}
}