-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.go
49 lines (42 loc) · 963 Bytes
/
util.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
package cowatchbenchmark
import (
"github.com/google/uuid"
"hash/crc32"
"math/rand"
)
const letterBytes = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890"
// generate a uuid v4
func getUUID() string {
return uuid.New().String()
}
// randStringBytes
func randStringBytes(n int) string {
b := make([]byte, n)
for i := range b {
b[i] = letterBytes[rand.Intn(len(letterBytes))]
}
return string(b)
}
// generate a tens digit as hostUid
func getHostId() int {
customHash := func(s string) int {
v := int(crc32.ChecksumIEEE([]byte(s)))
if v >= 0 {
return v
}
if -v >= 0 {
return -v
}
// v == MinInt
return 0
}
return customHash(getUUID())
}
func generateUserName(length int) string {
return randStringBytes(length)
}
// generate text message randomly for user
func generateMessage(r *RoomUnit) []byte {
msg := "42/" + r.roomName + ",[\"CMD:chat\",\"" + randStringBytes(r.msgLength) + "\"]"
return []byte(msg)
}