/
main.go
446 lines (411 loc) · 11 KB
/
main.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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
// Tool to generate fake accounts, content, and interactions.
// Intended for development and benchmarking. Similar to 'stress' and could
// merge at some point.
package main
import (
"context"
"encoding/json"
"fmt"
"os"
"runtime"
comatproto "github.com/bluesky-social/indigo/api/atproto"
"github.com/bluesky-social/indigo/fakedata"
"github.com/bluesky-social/indigo/util/cliutil"
_ "github.com/joho/godotenv/autoload"
_ "go.uber.org/automaxprocs"
"github.com/carlmjohnson/versioninfo"
"github.com/urfave/cli/v2"
"golang.org/x/sync/errgroup"
)
func main() {
run(os.Args)
}
func run(args []string) {
app := cli.App{
Name: "fakermaker",
Usage: "bluesky fake account/content generator",
Version: versioninfo.Short(),
}
app.Flags = []cli.Flag{
&cli.StringFlag{
Name: "pds-host",
Usage: "method, hostname, and port of PDS instance",
Value: "http://localhost:4849",
EnvVars: []string{"ATP_PDS_HOST"},
},
&cli.StringFlag{
Name: "admin-password",
Usage: "admin authentication password for PDS",
Required: true,
EnvVars: []string{"ATP_AUTH_ADMIN_PASSWORD"},
},
&cli.IntFlag{
Name: "jobs",
Aliases: []string{"j"},
Usage: "number of parallel threads to use",
Value: runtime.NumCPU(),
},
}
app.Commands = []*cli.Command{
&cli.Command{
Name: "gen-accounts",
Usage: "create accounts (DID, handle, profile)",
Action: genAccounts,
Flags: []cli.Flag{
&cli.IntFlag{
Name: "count",
Aliases: []string{"n"},
Usage: "total number of accounts to create",
Value: 100,
},
&cli.IntFlag{
Name: "count-celebrities",
Usage: "number of accounts as 'celebrities' (many followers)",
Value: 10,
},
&cli.StringFlag{
Name: "domain-suffix",
Usage: "domain to register handle under",
Value: "test",
},
&cli.BoolFlag{
Name: "use-invite-code",
Usage: "create and use an invite code",
Value: false,
},
},
},
&cli.Command{
Name: "gen-profiles",
Usage: "creates profile records for accounts",
Action: genProfiles,
Flags: []cli.Flag{
&cli.StringFlag{
Name: "catalog",
Usage: "file path of account catalog JSON file",
Value: "data/fakermaker/accounts.json",
},
&cli.BoolFlag{
Name: "no-avatars",
Usage: "disable avatar image generation",
Value: false,
},
&cli.BoolFlag{
Name: "no-banners",
Usage: "disable profile banner image generation",
Value: false,
},
},
},
&cli.Command{
Name: "gen-graph",
Usage: "creates social graph (follows and mutes)",
Action: genGraph,
Flags: []cli.Flag{
&cli.StringFlag{
Name: "catalog",
Usage: "file path of account catalog JSON file",
Value: "data/fakermaker/accounts.json",
},
&cli.IntFlag{
Name: "max-follows",
Usage: "create up to this many follows for each account",
Value: 90,
},
&cli.IntFlag{
Name: "max-mutes",
Usage: "create up to this many mutes (blocks) for each account",
Value: 25,
},
},
},
&cli.Command{
Name: "gen-posts",
Usage: "creates posts for accounts",
Action: genPosts,
Flags: []cli.Flag{
&cli.StringFlag{
Name: "catalog",
Usage: "file path of account catalog JSON file",
Value: "data/fakermaker/accounts.json",
},
&cli.IntFlag{
Name: "max-posts",
Usage: "create up to this many posts for each account",
Value: 10,
},
&cli.Float64Flag{
Name: "frac-image",
Usage: "portion of posts to include images",
Value: 0.25,
},
&cli.Float64Flag{
Name: "frac-mention",
Usage: "of posts created, fraction to include mentions in",
Value: 0.50,
},
},
},
&cli.Command{
Name: "gen-interactions",
Usage: "create interactions between accounts",
Action: genInteractions,
Flags: []cli.Flag{
&cli.StringFlag{
Name: "catalog",
Usage: "file path of account catalog JSON file",
Value: "data/fakermaker/accounts.json",
},
&cli.Float64Flag{
Name: "frac-like",
Usage: "fraction of posts in timeline to like",
Value: 0.20,
},
&cli.Float64Flag{
Name: "frac-repost",
Usage: "fraction of posts in timeline to repost",
Value: 0.20,
},
&cli.Float64Flag{
Name: "frac-reply",
Usage: "fraction of posts in timeline to reply to",
Value: 0.20,
},
},
},
&cli.Command{
Name: "run-browsing",
Usage: "creates read-only load on service (notifications, timeline, etc)",
Action: runBrowsing,
Flags: []cli.Flag{
&cli.StringFlag{
Name: "catalog",
Usage: "file path of account catalog JSON file",
Value: "data/fakermaker/accounts.json",
},
},
},
}
all := fakedata.MeasureIterations("entire command")
app.RunAndExitOnError()
all(1)
}
// registers fake accounts with PDS, and spits out JSON-lines to stdout with auth info
func genAccounts(cctx *cli.Context) error {
// establish atproto client, with admin token for auth
xrpcc, err := cliutil.GetXrpcClient(cctx, false)
if err != nil {
return err
}
adminToken := cctx.String("admin-password")
if len(adminToken) > 0 {
xrpcc.AdminToken = &adminToken
}
countTotal := cctx.Int("count")
countCelebrities := cctx.Int("count-celebrities")
domainSuffix := cctx.String("domain-suffix")
if countCelebrities > countTotal {
return fmt.Errorf("more celebrities than total accounts!")
}
countRegulars := countTotal - countCelebrities
var inviteCode *string = nil
if cctx.Bool("use-invite-code") {
resp, err := comatproto.ServerCreateInviteCodes(context.TODO(), xrpcc, &comatproto.ServerCreateInviteCodes_Input{
UseCount: int64(countTotal),
ForAccounts: nil,
CodeCount: 1,
})
if err != nil {
return err
}
if len(resp.Codes) != 1 || len(resp.Codes[0].Codes) != 1 {
return fmt.Errorf("expected a single invite code")
}
inviteCode = &resp.Codes[0].Codes[0]
}
// call helper to do actual creation
var usr *fakedata.AccountContext
var line []byte
t1 := fakedata.MeasureIterations("register celebrity accounts")
for i := 0; i < countCelebrities; i++ {
if usr, err = fakedata.GenAccount(xrpcc, i, "celebrity", domainSuffix, inviteCode); err != nil {
return err
}
// compact single-line JSON by default
if line, err = json.Marshal(usr); err != nil {
return nil
}
fmt.Println(string(line))
}
t1(countCelebrities)
t2 := fakedata.MeasureIterations("register regular accounts")
for i := 0; i < countRegulars; i++ {
if usr, err = fakedata.GenAccount(xrpcc, i, "regular", domainSuffix, inviteCode); err != nil {
return err
}
// compact single-line JSON by default
if line, err = json.Marshal(usr); err != nil {
return nil
}
fmt.Println(string(line))
}
t2(countRegulars)
return nil
}
func genProfiles(cctx *cli.Context) error {
catalog, err := fakedata.ReadAccountCatalog(cctx.String("catalog"))
if err != nil {
return err
}
pdsHost := cctx.String("pds-host")
genAvatar := !cctx.Bool("no-avatars")
genBanner := !cctx.Bool("no-banners")
jobs := cctx.Int("jobs")
accChan := make(chan fakedata.AccountContext, len(catalog.Celebs)+len(catalog.Regulars))
eg := new(errgroup.Group)
for i := 0; i < jobs; i++ {
eg.Go(func() error {
for acc := range accChan {
xrpcc, err := fakedata.AccountXrpcClient(pdsHost, &acc)
if err != nil {
return err
}
if err = fakedata.GenProfile(xrpcc, &acc, genAvatar, genBanner); err != nil {
return err
}
}
return nil
})
}
for _, acc := range append(catalog.Celebs, catalog.Regulars...) {
accChan <- acc
}
close(accChan)
return eg.Wait()
}
func genGraph(cctx *cli.Context) error {
catalog, err := fakedata.ReadAccountCatalog(cctx.String("catalog"))
if err != nil {
return err
}
pdsHost := cctx.String("pds-host")
maxFollows := cctx.Int("max-follows")
maxMutes := cctx.Int("max-mutes")
jobs := cctx.Int("jobs")
accChan := make(chan fakedata.AccountContext, len(catalog.Celebs)+len(catalog.Regulars))
eg := new(errgroup.Group)
for i := 0; i < jobs; i++ {
eg.Go(func() error {
for acc := range accChan {
xrpcc, err := fakedata.AccountXrpcClient(pdsHost, &acc)
if err != nil {
return err
}
if err = fakedata.GenFollowsAndMutes(xrpcc, catalog, &acc, maxFollows, maxMutes); err != nil {
return err
}
}
return nil
})
}
for _, acc := range append(catalog.Celebs, catalog.Regulars...) {
accChan <- acc
}
close(accChan)
return eg.Wait()
}
func genPosts(cctx *cli.Context) error {
catalog, err := fakedata.ReadAccountCatalog(cctx.String("catalog"))
if err != nil {
return err
}
pdsHost := cctx.String("pds-host")
maxPosts := cctx.Int("max-posts")
fracImage := cctx.Float64("frac-image")
fracMention := cctx.Float64("frac-mention")
jobs := cctx.Int("jobs")
accChan := make(chan fakedata.AccountContext, len(catalog.Celebs)+len(catalog.Regulars))
eg := new(errgroup.Group)
for i := 0; i < jobs; i++ {
eg.Go(func() error {
for acc := range accChan {
xrpcc, err := fakedata.AccountXrpcClient(pdsHost, &acc)
if err != nil {
return err
}
if err = fakedata.GenPosts(xrpcc, catalog, &acc, maxPosts, fracImage, fracMention); err != nil {
return err
}
}
return nil
})
}
for _, acc := range append(catalog.Celebs, catalog.Regulars...) {
accChan <- acc
}
close(accChan)
return eg.Wait()
}
func genInteractions(cctx *cli.Context) error {
catalog, err := fakedata.ReadAccountCatalog(cctx.String("catalog"))
if err != nil {
return err
}
pdsHost := cctx.String("pds-host")
fracLike := cctx.Float64("frac-like")
fracRepost := cctx.Float64("frac-repost")
fracReply := cctx.Float64("frac-reply")
jobs := cctx.Int("jobs")
accChan := make(chan fakedata.AccountContext, len(catalog.Celebs)+len(catalog.Regulars))
eg := new(errgroup.Group)
for i := 0; i < jobs; i++ {
eg.Go(func() error {
for acc := range accChan {
xrpcc, err := fakedata.AccountXrpcClient(pdsHost, &acc)
if err != nil {
return err
}
t1 := fakedata.MeasureIterations("all interactions")
if err := fakedata.GenLikesRepostsReplies(xrpcc, &acc, fracLike, fracRepost, fracReply); err != nil {
return err
}
t1(1)
}
return nil
})
}
for _, acc := range append(catalog.Celebs, catalog.Regulars...) {
accChan <- acc
}
close(accChan)
return eg.Wait()
}
func runBrowsing(cctx *cli.Context) error {
catalog, err := fakedata.ReadAccountCatalog(cctx.String("catalog"))
if err != nil {
return err
}
pdsHost := cctx.String("pds-host")
jobs := cctx.Int("jobs")
accChan := make(chan fakedata.AccountContext, len(catalog.Celebs)+len(catalog.Regulars))
eg := new(errgroup.Group)
for i := 0; i < jobs; i++ {
eg.Go(func() error {
for acc := range accChan {
xrpcc, err := fakedata.AccountXrpcClient(pdsHost, &acc)
if err != nil {
return err
}
if err := fakedata.BrowseAccount(xrpcc, &acc); err != nil {
return err
}
}
return nil
})
}
for _, acc := range append(catalog.Celebs, catalog.Regulars...) {
accChan <- acc
}
close(accChan)
return eg.Wait()
}