-
Notifications
You must be signed in to change notification settings - Fork 47
/
test.go
221 lines (206 loc) · 7 KB
/
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
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
package main
import (
"context"
"fmt"
"math/rand"
"net/url"
"time"
"github.com/matrix-org/complement/b"
"github.com/matrix-org/complement/internal/docker"
"github.com/matrix-org/complement/internal/instruction"
)
func withSpan(spanName, desc string, snapshots []Snapshot, absStartTime time.Time, deployment *docker.Deployment, fn func() error) ([]Snapshot, error) {
startTime := time.Now()
if err := fn(); err != nil {
return nil, fmt.Errorf("withSpan %s: %s", spanName, err)
}
duration := time.Since(startTime)
absDuration := time.Since(absStartTime)
snapshots = append(snapshots, snapshotStats(spanName, desc, deployment, absDuration, duration)...)
return snapshots, nil
}
func runTest(testName string, builder *docker.Builder, deployer *docker.Deployer, seed int64) ([]Snapshot, error) {
randSource := rand.NewSource(seed)
rnd := rand.New(randSource)
// deploy a base image
if err := builder.ConstructBlueprintIfNotExist(b.BlueprintCleanHS); err != nil {
return nil, err
}
deployment, err := deployer.Deploy(context.Background(), b.BlueprintCleanHS.Name)
if err != nil {
return nil, err
}
defer deployment.Deployer.Destroy(deployment, false, testName, false)
snapshots := snapshotStats("startup", "clean homeserver with no users", deployment, 0, 0)
runner := instruction.NewRunner(testName, false, true)
absStartTime := time.Now()
numUsers := 10
snapshots, err = withSpan("create_users", fmt.Sprintf("creates %d users concurrently", numUsers), snapshots, absStartTime, deployment, func() error {
// make N users
users := make([]b.User, numUsers)
for i := 0; i < len(users); i++ {
users[i] = b.User{
Localpart: fmt.Sprintf("user-%d", i),
DisplayName: fmt.Sprintf("User %d", i),
}
}
return runner.Run(b.Homeserver{
Name: "hs1",
Users: users,
}, deployment.HS["hs1"].BaseURL)
})
if err != nil {
return nil, err
}
var userRoomJoins [][2]string // list of [user_id, roomRef] of joined users who can send messages
numRooms := 50
snapshots, err = withSpan("create_rooms", fmt.Sprintf("creates %d public rooms with different users", numRooms), snapshots, absStartTime, deployment, func() error {
// make M rooms
rooms := make([]b.Room, numRooms)
for i := 0; i < numRooms; i++ {
userI := int(randSource.Int63() % int64(numUsers))
userID := fmt.Sprintf("@user-%d:hs1", userI)
roomRef := fmt.Sprintf("ref-%d", i)
rooms[i] = b.Room{
Creator: userID,
CreateRoom: map[string]interface{}{
"preset": "public_chat",
},
Ref: roomRef,
}
userRoomJoins = append(userRoomJoins, [2]string{userID, roomRef})
}
return runner.Run(b.Homeserver{
Name: "hs1",
Rooms: rooms,
}, deployment.HS["hs1"].BaseURL)
})
if err != nil {
return nil, err
}
// normal distribution around numRooms/2 to join M rooms, P times
numJoins := 100
stddev := float64(numRooms) / 6.0
mean := (float64(numRooms) / 2.0)
snapshots, err = withSpan("join_rooms", fmt.Sprintf("issues %d /join requests according to a normal distribution (mean=%v,std-dev=%v)", numJoins, mean, stddev), snapshots, absStartTime, deployment, func() error {
var hs b.Homeserver
roomMap := make(map[string]b.Room)
for i := 0; i < numJoins; i++ {
// random user
userI := int(randSource.Int63() % int64(numUsers))
userID := fmt.Sprintf("@user-%d:hs1", userI)
// normal distribution so we get some large rooms
roomI := int(rnd.NormFloat64()*stddev + mean)
ref := fmt.Sprintf("ref-%d", roomI)
room := roomMap[ref]
room.Ref = ref
room.Events = append(room.Events, b.Event{
Type: "m.room.member",
StateKey: &userID,
Sender: userID,
Content: map[string]interface{}{
"membership": "join",
},
})
roomMap[ref] = room
userRoomJoins = append(userRoomJoins, [2]string{userID, ref})
}
for _, room := range roomMap {
hs.Rooms = append(hs.Rooms, room)
}
return runner.Run(hs, deployment.HS["hs1"].BaseURL)
})
if err != nil {
return nil, err
}
numEvents := 100
snapshots, err = withSpan("send_msgs", fmt.Sprintf("sends %d m.room.message text events into random rooms users have joined", numEvents), snapshots, absStartTime, deployment, func() error {
var hs b.Homeserver
roomMap := make(map[string]b.Room)
for i := 0; i < numEvents; i++ {
userAndRoom := userRoomJoins[int(randSource.Int63()%int64(len(userRoomJoins)))]
userID := userAndRoom[0]
ref := userAndRoom[1]
room := roomMap[ref]
room.Ref = ref
room.Events = append(room.Events, b.Event{
Sender: userID,
Type: "m.room.message",
Content: map[string]interface{}{
"body": fmt.Sprintf("message %d", i),
"msgtype": "m.text",
},
})
roomMap[ref] = room
}
for _, room := range roomMap {
hs.Rooms = append(hs.Rooms, room)
}
return runner.Run(hs, deployment.HS["hs1"].BaseURL)
})
if err != nil {
return nil, err
}
runOpts := instruction.RunOpts{
Concurrency: instruction.ConcurrencyTypePerUser,
HSURL: deployment.HS["hs1"].BaseURL,
StoreNamespace: "_syncs",
}
syncInstructions := make([]instruction.Instr, numUsers)
for i := range syncInstructions {
userID := fmt.Sprintf("@user-%d:hs1", i)
syncInstructions[i] = instruction.Instr{
UserID: userID,
Method: "GET",
Path: "/_matrix/client/v3/sync",
Queries: map[string]string{"timeout": "0"},
Store: map[string]string{
userID: ".next_batch",
},
}
}
snapshots, err = withSpan("initial_syncs", fmt.Sprintf("performs /sync with no since token and timeout=0 for all users"), snapshots, absStartTime, deployment, func() error {
return runner.RunInstructions(runOpts, syncInstructions)
})
if err != nil {
return nil, err
}
syncInstructions = make([]instruction.Instr, 0, numUsers)
for i := 0; i < numUsers; i++ {
userID := fmt.Sprintf("@user-%d:hs1", i)
syncInstructions = append(syncInstructions, instruction.Instr{
UserID: userID,
Method: "PUT",
Path: fmt.Sprintf("/_matrix/client/v3/profile/%s/displayname", url.PathEscape(userID)),
Body: map[string]interface{}{
"displayname": fmt.Sprintf("Updated User %d", i),
},
})
}
snapshots, err = withSpan("display_name_change", fmt.Sprintf("updates the displayname of all %d users concurrently", numUsers), snapshots, absStartTime, deployment, func() error {
return runner.RunInstructions(runOpts, syncInstructions)
})
if err != nil {
return nil, err
}
syncInstructions = make([]instruction.Instr, numUsers)
for i := 0; i < numUsers; i++ {
userID := fmt.Sprintf("@user-%d:hs1", i)
syncInstructions[i] = instruction.Instr{
UserID: userID,
Method: "GET",
Path: "/_matrix/client/v3/sync",
Queries: map[string]string{"since": runner.GetStoredValue(runOpts, userID)},
Store: map[string]string{
userID: ".next_batch",
},
}
}
snapshots, err = withSpan("incremental_sync", fmt.Sprintf("performs an incremental sync on all %d users with the since token from the initial sync", numUsers), snapshots, absStartTime, deployment, func() error {
return runner.RunInstructions(runOpts, syncInstructions)
})
if err != nil {
return nil, err
}
return snapshots, nil
}