forked from pingcap/talent-plan
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcasegen.go
241 lines (220 loc) · 5.94 KB
/
casegen.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
package main
import (
"fmt"
"math/rand"
"path"
"sort"
)
type DataSize int
const (
KB = 1 << 10
MB = 1 << 20
GB = 1 << 30
)
func (d DataSize) String() string {
if d < KB {
return fmt.Sprintf("%dbyte", d)
} else if d < MB {
return fmt.Sprintf("%dKB", d/KB)
} else if d < GB {
return fmt.Sprintf("%dMB", d/MB)
}
return fmt.Sprintf("%dGB", d/GB)
}
// Case represents a test case.
type Case struct {
MapFiles []string // input files for map function
ResultFile string // expected result
}
// CaseGenF represents test case generate function
type CaseGenF func(dataFileDir string, totalDataSize, nMapFiles int) Case
// AllCaseGenFs returns all CaseGenFs used to test.
func AllCaseGenFs() []CaseGenF {
var gs []CaseGenF
gs = append(gs, genUniformCases()...)
gs = append(gs, genPercentCases()...)
gs = append(gs, CaseSingleURLPerFile)
return gs
}
func genUniformCases() []CaseGenF {
cardinalities := []int{1, 7, 200, 10000, 1000000}
gs := make([]CaseGenF, 0, len(cardinalities))
for i := range cardinalities {
card := cardinalities[i]
gs = append(gs, func(dataFileDir string, totalDataSize, nMapFiles int) Case {
if FileOrDirExist(dataFileDir) {
files := make([]string, 0, nMapFiles)
for i := 0; i < nMapFiles; i++ {
fpath := path.Join(dataFileDir, fmt.Sprintf("inputMapFile%d", i))
files = append(files, fpath)
}
rpath := path.Join(dataFileDir, "result")
return Case{
MapFiles: files,
ResultFile: rpath,
}
}
urls, avgLen := randomNURL(card)
eachRecords := (totalDataSize / nMapFiles) / avgLen
files := make([]string, 0, nMapFiles)
urlCount := make(map[string]int, len(urls))
for i := 0; i < nMapFiles; i++ {
fpath := path.Join(dataFileDir, fmt.Sprintf("inputMapFile%d", i))
files = append(files, fpath)
f, buf := CreateFileAndBuf(fpath)
for i := 0; i < eachRecords; i++ {
str := urls[rand.Int()%len(urls)]
urlCount[str]++
WriteToBuf(buf, str, "\n")
}
SafeClose(f, buf)
}
rpath := path.Join(dataFileDir, "result")
genResult(rpath, urlCount)
return Case{
MapFiles: files,
ResultFile: rpath,
}
})
}
return gs
}
func genPercentCases() []CaseGenF {
ps := []struct {
l int
p []float64
}{
{11, []float64{0.9, 0.09, 0.009, 0.0009, 0.00009, 0.000009}},
{10000, []float64{0.9, 0.09, 0.009, 0.0009, 0.00009, 0.000009}},
{100000, []float64{0.9, 0.09, 0.009, 0.0009, 0.00009, 0.000009}},
{10000, []float64{0.5, 0.4}},
{10000, []float64{0.3, 0.3, 0.3}},
}
gs := make([]CaseGenF, 0, len(ps))
for i := range ps {
p := ps[i]
gs = append(gs, func(dataFileDir string, totalDataSize, nMapFiles int) Case {
if FileOrDirExist(dataFileDir) {
files := make([]string, 0, nMapFiles)
for i := 0; i < nMapFiles; i++ {
fpath := path.Join(dataFileDir, fmt.Sprintf("inputMapFile%d", i))
files = append(files, fpath)
}
rpath := path.Join(dataFileDir, "result")
return Case{
MapFiles: files,
ResultFile: rpath,
}
}
// make up percents list
percents := make([]float64, 0, p.l)
percents = append(percents, p.p...)
var sum float64
for _, p := range p.p {
sum += p
}
if sum > 1 || len(p.p) > p.l {
panic("invalid prefix")
}
x := (1 - sum) / float64(p.l-len(p.p))
for i := 0; i < p.l-len(p.p); i++ {
percents = append(percents, x)
}
// generate data
urls, avgLen := randomNURL(len(percents))
eachRecords := (totalDataSize / nMapFiles) / avgLen
files := make([]string, 0, nMapFiles)
urlCount := make(map[string]int, len(urls))
accumulate := make([]float64, len(percents)+1)
accumulate[0] = 0
for i := range percents {
accumulate[i+1] = accumulate[i] + percents[i]
}
for i := 0; i < nMapFiles; i++ {
fpath := path.Join(dataFileDir, fmt.Sprintf("inputMapFile%d", i))
files = append(files, fpath)
f, buf := CreateFileAndBuf(fpath)
for i := 0; i < eachRecords; i++ {
x := rand.Float64()
idx := sort.SearchFloat64s(accumulate, x)
if idx != 0 {
idx--
}
str := urls[idx]
urlCount[str]++
WriteToBuf(buf, str, "\n")
}
SafeClose(f, buf)
}
rpath := path.Join(dataFileDir, "result")
genResult(rpath, urlCount)
return Case{
MapFiles: files,
ResultFile: rpath,
}
})
}
return gs
}
// CaseSingleURLPerFile .
func CaseSingleURLPerFile(dataFileDir string, totalDataSize, nMapFiles int) Case {
if FileOrDirExist(dataFileDir) {
files := make([]string, 0, nMapFiles)
for i := 0; i < nMapFiles; i++ {
fpath := path.Join(dataFileDir, fmt.Sprintf("inputMapFile%d", i))
files = append(files, fpath)
}
rpath := path.Join(dataFileDir, "result")
return Case{
MapFiles: files,
ResultFile: rpath,
}
}
urls, avgLen := randomNURL(nMapFiles)
eachRecords := (totalDataSize / nMapFiles) / avgLen
files := make([]string, 0, nMapFiles)
urlCount := make(map[string]int, len(urls))
for i := 0; i < nMapFiles; i++ {
fpath := path.Join(dataFileDir, fmt.Sprintf("inputMapFile%d", i))
files = append(files, fpath)
f, buf := CreateFileAndBuf(fpath)
for j := 0; j < eachRecords; j++ {
str := urls[i]
urlCount[str]++
WriteToBuf(buf, str, "\n")
}
SafeClose(f, buf)
}
rpath := path.Join(dataFileDir, "result")
genResult(rpath, urlCount)
return Case{
MapFiles: files,
ResultFile: rpath,
}
}
func genResult(rpath string, urlCount map[string]int) {
us, cs := TopN(urlCount, 10)
f, buf := CreateFileAndBuf(rpath)
for i := range us {
fmt.Fprintf(buf, "%s: %d\n", us[i], cs[i])
}
SafeClose(f, buf)
}
func randomNURL(n int) ([]string, int) {
length := 0
urls := make([]string, 0, n)
for i := 0; i < n; i++ {
url := wrapLikeURL(fmt.Sprintf("%d", i))
length += len(url)
urls = append(urls, url)
}
return urls, length / len(urls)
}
var urlPrefixes = []string{
"github.com/pingcap/tidb/issues",
"github.com/pingcap/tidb/pull",
"github.com/pingcap/tidb",
}
func wrapLikeURL(suffix string) string {
return path.Join(urlPrefixes[rand.Intn(len(urlPrefixes))], suffix)
}