-
Notifications
You must be signed in to change notification settings - Fork 1
/
dataset.go
368 lines (276 loc) · 8.44 KB
/
dataset.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
package main
import (
"bytes"
"encoding/json"
"fmt"
"math/rand"
"net/http"
"net/http/cookiejar"
"os"
"sync"
"time"
)
type User struct {
Username string `json:"username"`
Password string `json:"password"`
Email string `json:"email"`
Firstname string `json:"firstname"`
Lastname string `json:"lastname"`
}
type Space struct {
DisplayName string `json:"displayName"`
Description string `json:"description"`
Visibility string `json:"visibility"`
Subscription string `json:"subscription"`
}
type SpaceActivity struct {
SpaceId int `json:"id"`
Activity Activity `json:"model"`
}
type Activity struct {
Title string `json:"title"`
}
type SpaceMembership struct {
User string `json:"user"`
Space string `json:"space"`
}
const (
SESSION_URI = "%s/rest/private/"
USERS_URI = "/rest/private/v1/social/users"
SPACES_URI = "/rest/private/v1/social/spaces"
SPACE_ACTIVITIES_URI = "%s/rest/private/v1/social/spaces/%d/activities"
SPACE_MEMBERSHIP_URL = "%s/rest/private/v1/social/spacesMemberships"
USER_PREFIX = "abcde"
USER_PASSWORD = "testtest123"
USER_EMAIL = "@test.com"
SPACE_PREFIX = "spacetestestc"
NB_USERS = 200
NB_SPACES = 100
NB_SPACES_ACTIVITIES = 1000
SPACE_ACTIVITY_LENGTH = 200
)
const letterBytes = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890 !@#$%^&*()_+-=[]\\{}|;':\",./?"
func RandStringBytes(n int) string {
b := make([]byte, n)
for i := range b {
b[i] = letterBytes[rand.Intn(len(letterBytes))]
}
return string(b)
}
func usage(arguments []string) {
fmt.Println(fmt.Sprintf("%s <base url> <user> <password>", arguments[0]))
os.Exit(1)
}
func createUser(wg *sync.WaitGroup, c <-chan string, h string) {
for {
name := <-c
t0 := time.Now()
newUser := User{Username: name, Password: USER_PASSWORD, Email: fmt.Sprintf("%s%s", name, USER_EMAIL), Firstname: name, Lastname: name}
json, _ := json.Marshal(newUser)
req, _ := http.NewRequest("POST", fmt.Sprintf("%s%s", h, USERS_URI), bytes.NewBuffer(json))
req.Header.Set("Content-Type", "application/json")
res, _ := client.Do(req)
req.Body.Close()
res.Body.Close()
t1 := time.Now()
fmt.Println("User ", name, " : "+res.Status, " in ", t1.Sub(t0))
wg.Done()
}
}
func createUsers(h string) {
var wg sync.WaitGroup
c := make(chan string)
////-------------
// Create any go routines here
go createUser(&wg, c, h)
go createUser(&wg, c, h)
go createUser(&wg, c, h)
go createUser(&wg, c, h)
go createUser(&wg, c, h)
go createUser(&wg, c, h)
go createUser(&wg, c, h)
go createUser(&wg, c, h)
go createUser(&wg, c, h)
go createUser(&wg, c, h)
t0 := time.Now()
for i := 0; i < NB_USERS; i++ {
wg.Add(1)
name := fmt.Sprintf("%s%d", USER_PREFIX, i)
c <- name
}
fmt.Println("Waiting for the threads to finish")
wg.Wait()
t1 := time.Now()
fmt.Println("All thread done")
fmt.Println("Users created in ", t1.Sub(t0))
}
func addUserToSpace(h string, space string, user string) {
t0 := time.Now()
membership := SpaceMembership{User: user, Space: space}
json, _ := json.Marshal(membership)
req, _ := http.NewRequest("POST", fmt.Sprintf(SPACE_MEMBERSHIP_URL, h), bytes.NewBuffer(json))
req.Header.Set("Content-Type", "application/json")
res, _ := client.Do(req)
req.Body.Close()
res.Body.Close()
t1 := time.Now()
fmt.Println("Add user", user, "to space", space, "in", t1.Sub(t0), res.Status)
}
func addUsersToSpaces(h string) {
function := func(wg *sync.WaitGroup, c <-chan *SpaceMembership) {
for {
m := <-c
addUserToSpace(h, m.Space, m.User)
wg.Done()
}
}
t0 := time.Now()
var wg sync.WaitGroup
// Updating a space is not thread safe
// directing same space update to the same channel to avoid
// concurrency
var channels []chan *SpaceMembership
channels = append(channels, make(chan *SpaceMembership))
channels = append(channels, make(chan *SpaceMembership))
channels = append(channels, make(chan *SpaceMembership))
channels = append(channels, make(chan *SpaceMembership))
channels = append(channels, make(chan *SpaceMembership))
channels = append(channels, make(chan *SpaceMembership))
channels = append(channels, make(chan *SpaceMembership))
channels = append(channels, make(chan *SpaceMembership))
channels = append(channels, make(chan *SpaceMembership))
channels = append(channels, make(chan *SpaceMembership))
for _, c := range channels {
go function(&wg, c)
}
for u := 0; u < NB_USERS; u++ {
for sid := 0; sid < NB_SPACES; sid++ {
u := fmt.Sprintf("%s%d", USER_PREFIX, u)
s := fmt.Sprintf("%s%d", SPACE_PREFIX, sid)
m := SpaceMembership{User: u, Space: s}
wg.Add(1)
pos := sid % len(channels)
channels[pos] <- &m
}
}
fmt.Println("Waiting for the threads to finish")
wg.Wait()
t1 := time.Now()
fmt.Println("All thread done")
fmt.Println("User attacted to spaces in ", t1.Sub(t0))
}
func createSpace(wg *sync.WaitGroup, c <-chan int, h string) {
// TODO cleary exit
for {
t0 := time.Now()
id := <-c
name := fmt.Sprintf("%s%d", SPACE_PREFIX, id)
newSpace := Space{DisplayName: name, Description: name, Visibility: "public", Subscription: "open"}
json, _ := json.Marshal(newSpace)
req, _ := http.NewRequest("POST", fmt.Sprintf("%s%s", h, SPACES_URI), bytes.NewBuffer(json))
req.Header.Set("Content-Type", "application/json")
res, _ := client.Do(req)
req.Body.Close()
res.Body.Close()
t1 := time.Now()
fmt.Println("Space ", name, " : "+res.Status, " in ", t1.Sub(t0))
wg.Done()
}
}
/*
curl -uroot:gtn -H'Content-Type: application/json' -X POST http://localhost:8080/rest/private/v1/social/spaces -d '{"displayName":"space1", "description":"space1", "visibility":"public", "subscription":"open"}'
*/
func createSpaces(h string, u string, p string) {
var wg sync.WaitGroup
sc := make(chan int)
////-------------
// Create any go routines here
// For the moment it's not possible
// to create more than one space at a time
// due to https://jira.exoplatform.org/browse/SOC-5697
go createSpace(&wg, sc, h)
t0 := time.Now()
for i := 0; i < NB_SPACES; i++ {
wg.Add(1)
sc <- i
}
fmt.Println("Waiting for the threads to finish")
wg.Wait()
t1 := time.Now()
fmt.Println("All thread done")
fmt.Println("Spaces created in ", t1.Sub(t0))
}
/*
* Create NB_ACTIVITIES on each space
*/
func createSpacesActivities(h string, u string, p string) {
t0 := time.Now()
for i := 1; i <= NB_SPACES_ACTIVITIES; i++ {
// for s := 1; s <= NB_SPACES; s++ {
s := 323
ta0 := time.Now()
title := RandStringBytes(SPACE_ACTIVITY_LENGTH)
// title = "test"
a := Activity{Title: title}
fmt.Print(fmt.Sprintf("Creating activity spaceId=%d actitivyCount=%d ...", s, i))
json, _ := json.Marshal(a)
req, _ := http.NewRequest("POST", fmt.Sprintf(SPACE_ACTIVITIES_URI, h, s), bytes.NewBuffer(json))
req.Close = true
req.Header.Set("Content-Type", "application/json")
res, _ := client.Do(req)
fmt.Print(res.Status)
req.Body.Close()
res.Body.Close()
ta1 := time.Now()
fmt.Println(" in ", ta1.Sub(ta0))
// }
}
t1 := time.Now()
fmt.Println("Activities created in ", t1.Sub(t0))
}
func createSpacesActivitiy(h string, id int, content string) {
ta0 := time.Now()
a := Activity{Title: content}
fmt.Print(fmt.Sprintf("Creating activity spaceId=%d ...", id))
json, _ := json.Marshal(a)
req, _ := http.NewRequest("POST", fmt.Sprintf(SPACE_ACTIVITIES_URI, h, id), bytes.NewBuffer(json))
req.Close = true
req.Header.Set("Content-Type", "application/json")
res, _ := client.Do(req)
fmt.Print(res.Status)
req.Body.Close()
res.Body.Close()
ta1 := time.Now()
fmt.Println(" in ", ta1.Sub(ta0))
}
func getSession(h string, u string, p string) {
req, _ := http.NewRequest("GET", fmt.Sprintf(SESSION_URI, h), nil)
req.Close = true
req.SetBasicAuth(u, p)
res, _ := client.Do(req)
defer res.Body.Close()
//fmt.Println(client.Jar)
fmt.Println("Session created " + res.Status)
}
var client http.Client
var sessionCookie string
func init() {
jar, _ := cookiejar.New(&cookiejar.Options{})
client = http.Client{Jar: jar}
}
func main() {
arguments := os.Args
if len(arguments) != 4 {
usage(arguments)
}
host := arguments[1]
user := arguments[2]
password := arguments[3]
fmt.Println(fmt.Sprintf("Using host %s and user %s", host, user))
getSession(host, user, password)
createUsers(host)
createSpaces(host, user, password)
addUsersToSpaces(host)
createSpacesActivities(host, user, password)
createSpacesActivitiy(host, 66, "fake identity to test")
}