forked from simagix/hatchet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bios.go
278 lines (251 loc) · 8.38 KB
/
bios.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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
/*
* Copyright 2022-present Kuei-chun Chen. All rights reserved.
* bios.go
*/
package hatchet
import (
"context"
"errors"
"fmt"
"log"
"math/rand"
"runtime"
"strings"
"sync"
"time"
fake "github.com/brianvoe/gofakeit/v6"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
)
const (
BioDBName = "hatchet"
BioCollName = "bios"
)
type Bio struct {
Age int `bson:"age" json:"age"`
Company string `bson:"company" json:"company"`
CreditCards []string `bson:"credit_cards" json:"credit_cards"`
Emails []string `bson:"emails" json:"emails"`
FirstName string `bson:"first_name" json:"first_name"`
Intro string `bson:"intro" json:"intro"`
LastName string `bson:"last_name" json:"last_name"`
Phones []string `bson:"phones" json:"phones"`
Title string `bson:"title" json:"title"`
SSN string `bson:"ssn" json:"ssn"`
State string `bson:"state" json:"state"`
URL string `bson:"url" json:"url"`
}
func generateBio() Bio {
firstName := fake.FirstName()
lastName := fake.LastName()
company := fake.Company()
email := strings.ToLower(firstName + "." + lastName + "@" + strings.ToLower(company) + ".com")
phoneNo := "+1 " + fake.Phone()
ssn := fmt.Sprintf("%d-%d-%d", rand.Intn(900)+100, rand.Intn(90)+10, rand.Intn(9000)+1000)
cardNo := fmt.Sprintf("%d-%d-%d-%d", rand.Intn(9000)+1000, rand.Intn(9000)+1000, rand.Intn(9000)+1000, rand.Intn(9000)+1000)
state := fake.State()
title := fake.JobTitle()
min := 22
max := 55
age := rand.Intn(max-min+1) + min
url := fmt.Sprintf("https://%s:%d", fake.DomainName(), rand.Intn(65535)+1024)
intro := fmt.Sprintf("Meet %s %s, a %d-year-old from %s. %s works as a %s at %s.", firstName, lastName, age, state, firstName, title, company)
// Create a Bio struct with the generated values
return Bio{
Age: age,
Company: company,
CreditCards: []string{cardNo},
Emails: []string{email},
FirstName: firstName,
Intro: intro,
Title: title,
LastName: lastName,
URL: url,
Phones: []string{phoneNo},
SSN: ssn,
State: state,
}
}
func insertBios(client *mongo.Client, wg *sync.WaitGroup, size int) error {
defer wg.Done()
if size < 1000 {
size = 1000
}
fmt.Println("Insert", size, "bios")
collection := client.Database(BioDBName).Collection(BioCollName)
// Insert the documents in batches of 1000
var err error
for i := 0; i < size; i += 1000 {
j := i + 1000
if j > size {
j = size
}
// Generate a slice of Bio documents for this batch
docs := make([]interface{}, j-i)
for k := 0; k < j-i; k++ {
docs[k] = generateBio()
}
// Insert the documents in this batch
_, err = collection.InsertMany(context.Background(), docs)
if err != nil {
return err
}
}
return nil
}
func InsertBiosIntoMongoDB(uri string, numDocuments int) error {
log.Println(uri, numDocuments)
client, err := mongo.NewClient(options.Client().ApplyURI(uri))
if err != nil {
return err
}
err = client.Connect(context.Background())
if err != nil {
return err
}
var wg sync.WaitGroup
// Divide the work among N goroutines
numThreads := 4
chunkSize := numDocuments / numThreads
for i := 0; i < numThreads; i++ {
wg.Add(1)
go insertBios(client, &wg, chunkSize)
}
wg.Wait()
err = client.Disconnect(context.Background())
if err != nil {
return err
}
return nil
}
func SimulateTests(test string, url string) error {
if test == "read" {
return SimulateReads(url)
} else if test == "write" {
return SimulateWrites(url)
}
return errors.New("unrecognized test type")
}
func SimulateReads(url string) error {
pipeline := mongo.Pipeline{}
client, err := mongo.Connect(context.Background(), options.Client().ApplyURI(url))
if err != nil {
fmt.Println("Failed to connect to MongoDB:", err)
return err
}
defer client.Disconnect(context.Background())
collection := client.Database(BioDBName).Collection(BioCollName)
wg := sync.WaitGroup{}
numThreads := runtime.NumCPU() * 4
if numThreads > 64 {
numThreads = 64
}
// Spawn n threads to read the aggregation pipeline
for i := 0; i < numThreads; i++ {
wg.Add(1)
go func(seq int) {
defer wg.Done()
start := time.Now()
fmt.Println(start, seq)
project := bson.D{{Key: "$project", Value: bson.D{{Key: "intro", Value: 1}, {Key: "emails", Value: 1}, {Key: "ssn", Value: 1}, {Key: "phones", Value: 1}, {Key: "credit_cards", Value: 1}}}}
match := bson.D{{Key: "$match", Value: bson.D{{Key: "state", Value: fake.State()}, {Key: "last_name", Value: fake.LastName()}, {Key: "first_name", Value: fake.FirstName()}}}}
pipeline = mongo.Pipeline{match, project}
for {
if seq < 2 {
ssn := fmt.Sprintf("%d-%d-%d", rand.Intn(900)+100, rand.Intn(90)+10, rand.Intn(9000)+1000)
cardNo := fmt.Sprintf("%d-%d-%d-%d", rand.Intn(9000)+1000, rand.Intn(9000)+1000, rand.Intn(9000)+1000, rand.Intn(9000)+1000)
match = bson.D{{Key: "$match", Value: bson.D{{Key: "ssn", Value: ssn}, {Key: "emails", Value: fake.Email()}, {Key: "credit_cards", Value: cardNo}}}}
pipeline = mongo.Pipeline{match, project}
} else if seq < 4 {
match = bson.D{{Key: "$match", Value: bson.D{{Key: "last_name", Value: fake.LastName()}, {Key: "first_name", Value: fake.FirstName()}}}}
pipeline = mongo.Pipeline{match, project}
} else if seq < 6 {
match = bson.D{{Key: "$match", Value: bson.D{{Key: "state", Value: fake.State()}, {Key: "last_name", Value: fake.LastName()}}}}
pipeline = mongo.Pipeline{match, project}
} else if seq < 8 {
match = bson.D{{Key: "$match", Value: bson.D{{Key: "state", Value: fake.State()}}}}
pipeline = mongo.Pipeline{match, project}
}
// Run the aggregation pipeline
cursor, err := collection.Aggregate(context.Background(), pipeline)
if err != nil {
log.Fatal("Failed to run aggregation pipeline:", err)
return
}
// Process the aggregation result
defer cursor.Close(context.Background())
time.Sleep(20 * time.Millisecond)
for cursor.Next(context.Background()) {
// Process the aggregation result here
}
if err := cursor.Err(); err != nil {
log.Fatal("Failed to read aggregation result:", err)
return
}
executionTime := time.Since(start)
if executionTime < 2*time.Minute {
time.Sleep(20 * time.Millisecond)
continue
} else {
break
}
}
}(i)
}
wg.Wait()
fmt.Println("completed SimulateReads")
return nil
}
func SimulateWrites(url string) error {
client, err := mongo.Connect(context.Background(), options.Client().ApplyURI(url))
if err != nil {
fmt.Println("Failed to connect to MongoDB:", err)
return err
}
defer client.Disconnect(context.Background())
collection := client.Database(BioDBName).Collection(BioCollName)
wg := sync.WaitGroup{}
numThreads := runtime.NumCPU() * 4
if numThreads > 64 {
numThreads = 64
}
for i := 0; i < numThreads; i++ {
wg.Add(1)
go func(seq int) {
defer wg.Done()
start := time.Now()
fmt.Println(seq, start)
updated := bson.D{{Key: "$inc", Value: bson.D{{Key: "updated", Value: 1}}}, {Key: "$set", Value: bson.D{{Key: "source_ip", Value: fake.IPv4Address()}}}}
match := bson.D{{Key: "state", Value: fake.State()}, {Key: "last_name", Value: fake.LastName()}, {Key: "first_name", Value: fake.FirstName()}}
for {
if seq < 2 {
ssn := fmt.Sprintf("%d-%d-%d", rand.Intn(900)+100, rand.Intn(90)+10, rand.Intn(9000)+1000)
cardNo := fmt.Sprintf("%d-%d-%d-%d", rand.Intn(9000)+1000, rand.Intn(9000)+1000, rand.Intn(9000)+1000, rand.Intn(9000)+1000)
match = bson.D{{Key: "ssn", Value: ssn}, {Key: "emails", Value: fake.Email()}, {Key: "credit_cards", Value: cardNo}}
} else if seq < 4 {
match = bson.D{{Key: "last_name", Value: fake.LastName()}, {Key: "first_name", Value: fake.FirstName()}}
} else if seq < 6 {
match = bson.D{{Key: "state", Value: fake.State()}, {Key: "last_name", Value: fake.LastName()}}
} else if seq < 8 {
match = bson.D{{Key: "state", Value: fake.State()}}
}
// Run updateMany
_, err := collection.UpdateMany(context.Background(), match, updated)
if err != nil {
log.Fatal("Failed to run update:", err)
}
executionTime := time.Since(start)
if executionTime < 2*time.Minute {
time.Sleep(20 * time.Millisecond)
continue
} else {
break
}
}
}(i)
}
wg.Wait()
fmt.Println("completed SimulateWrites")
return nil
}