forked from forewing/goldennum
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
208 lines (179 loc) · 4.28 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
package main
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"log"
"math/rand"
"net/http"
"sync"
"sync/atomic"
"time"
"golang.org/x/time/rate"
)
type user struct {
id int
name string
pass string
}
const (
authUser = "admin"
authPass = "password"
baseURL = "http://127.0.0.1:8080/"
roomURL = baseURL + "room/"
roomsURL = baseURL + "rooms/"
userURL = baseURL + "user/"
usersURL = baseURL + "users/"
syncURL = baseURL + "sync/"
roomInterval = 15
roomRounds = 60
userTotal = 400
)
var (
roomID int
userCount int64
limiter *rate.Limiter = rate.NewLimiter(80, 5)
)
func mustPostJSON(url string, body interface{}, auth bool) map[string]interface{} {
ctx, cancel := context.WithTimeout(context.Background(), roomInterval*time.Second/2)
defer cancel()
if err := limiter.Wait(ctx); err != nil {
log.Panicf("limiter.Wait failed:%v", err)
}
str, err := json.Marshal(body)
if err != nil {
log.Panicf("json.Marshal fail: %v", err)
}
req, _ := http.NewRequest("POST", url, bytes.NewBuffer([]byte(str)))
req.Header.Set("Content-Type", "application/json")
if auth {
req.SetBasicAuth(authUser, authPass)
}
resp, err := http.DefaultClient.Do(req)
if err != nil {
log.Panicf("http.Post fail, error: %v", err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
log.Panicf("http.Post fail, resp: %+v", resp)
}
jsonMap := make(map[string]interface{})
jsonStr, err := io.ReadAll(resp.Body)
if err != nil {
log.Panicf("io.ReadAll fail: %v", err)
}
err = json.Unmarshal([]byte(jsonStr), &jsonMap)
if err != nil {
return map[string]interface{}{}
}
return jsonMap
}
func roomCreate() {
body := map[string]interface{}{
"Interval": roomInterval,
"RoundTotal": roomRounds,
}
resp := mustPostJSON(roomURL, body, true)
roomID = int(resp["ID"].(float64))
}
func userRegister() user {
defer func() {
if err := recover(); err != nil {
log.Println("register failed: ", err)
}
}()
id := int(atomic.AddInt64(&userCount, 1))
u := user{
name: fmt.Sprintf("user-%v", id),
pass: fmt.Sprintf("password-%v", id),
}
body := map[string]interface{}{
"Username": u.name,
"Password": u.pass,
}
resp := mustPostJSON(usersURL+fmt.Sprintf("%v", roomID), body, false)
u.id = int(resp["ID"].(float64))
return u
}
func (u *user) submit() {
defer func() {
if err := recover(); err != nil {
log.Println("submit failed: ", err)
}
}()
submit1 := rand.Float64() * 100
submit2 := rand.Float64() * 100
body := map[string]interface{}{
"Password": u.pass,
"Submit1": submit1,
"Submit2": submit2,
}
mustPostJSON(userURL+fmt.Sprintf("%v", u.id), body, false)
log.Println(u.id, submit1, submit2)
}
func getRoomInfo() {
time.Sleep(time.Millisecond * time.Duration(rand.Intn(roomInterval*500)))
ctx, cancel := context.WithTimeout(context.Background(), roomInterval*time.Second/2)
defer cancel()
if err := limiter.Wait(ctx); err == nil {
resp, err := http.Get(fmt.Sprintf(roomURL+"%v", roomID))
if err == nil {
defer resp.Body.Close()
}
}
}
func getUserInfo(id int) {
time.Sleep(time.Millisecond * time.Duration(rand.Intn(roomInterval*500)))
ctx, cancel := context.WithTimeout(context.Background(), roomInterval*time.Second/2)
defer cancel()
if err := limiter.Wait(ctx); err == nil {
resp, err := http.Get(fmt.Sprintf(userURL+"%v", id))
if err == nil {
defer resp.Body.Close()
}
}
}
func getSync() {
time.Sleep(time.Millisecond * time.Duration(rand.Intn(roomInterval*500)))
ctx, cancel := context.WithTimeout(context.Background(), roomInterval*time.Second/2)
defer cancel()
if err := limiter.Wait(ctx); err == nil {
resp, err := http.Get(fmt.Sprintf(syncURL+"%v", roomID))
if err == nil {
defer resp.Body.Close()
}
}
}
func main() {
roomCreate()
users := make(map[int]*user)
usersLock := sync.Mutex{}
var usersWg sync.WaitGroup
for i := 0; i < userTotal; i++ {
usersWg.Add(1)
go func() {
defer usersWg.Done()
u := userRegister()
if u.id == 0 {
return
}
usersLock.Lock()
defer usersLock.Unlock()
users[u.id] = &u
log.Println(u)
}()
}
usersWg.Wait()
for i := 0; i < roomRounds; i++ {
next := time.Now().Add(roomInterval * time.Second)
for _, u := range users {
go u.submit()
go getRoomInfo()
go getUserInfo(u.id)
go getSync()
}
time.Sleep(time.Until(next))
}
}